2017-04-12 33 views
0

美好的一天,感谢以前的协助。请尝试将记录从我的窗口表单发送到数据库,我有问题,我该怎么做? 下面是代码片段我如何将记录从我的Windows窗体发布到我的数据库

 private void btnNext_Click(object sender, EventArgs e) 
    { 
     //Calling Window Work experience page 
     WorkExperience frm = new WorkExperience(); 
     frm.ShowDialog(); 


     string connectionString = @"Data Source=localhost;" + 
     "Initial Catalog=EmploymentDb;Integrated Security=true; User Instance=False"; 
     SqlConnection connection = new SqlConnection(connectionString); 
     SqlCommand command = new SqlCommand(); 

      command.Connection = connection; 

     //command.CommandText 
       string sql = "INSERT INTO EmploymentDb " + 
       "(Id,Title, LastName, FirstName, MiddleName, Gender, Address, Email, City, State, MobileNumber, DateOfBirth, HomePhone, DistchargeCertNumber, SchoolAttended, NYSCStatus, AgeLimit) VALUES " + 
       "(@Id, @Title, @LastName, @FirstName, @MiddleName, @Gender, @Address, @Email, @City, @State, @MobileNumber, @DateOfBirth, @HomePhone, @DistchargeCertNumber, @SchoolAttended, @NYSCStatus, @AgeLimit)"; 

       using (SqlConnection conn = new SqlConnection(connectionString)) 
       { 
        conn.Open(); 
        SqlCommand cmd = new SqlCommand(sql, conn); 
        cmd.Parameters.AddWithValue("@Id", txtID.Text); 
        cmd.Parameters.AddWithValue("@Title", comboBoxtTitle.Text); 
        cmd.Parameters.AddWithValue("@LastName", txtLastName.Text); 
        cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text); 
        cmd.Parameters.AddWithValue("@MiddleName", txtMiddleName.Text); 
        cmd.Parameters.AddWithValue("@Gender", comboBoxGender.Text); 
        cmd.Parameters.AddWithValue("@Address", txtAddress.Text); 
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text); 
        cmd.Parameters.AddWithValue("@City", comboBoxCity.Text); 
        cmd.Parameters.AddWithValue("@State", comboBoxState.Text); 
        cmd.Parameters.AddWithValue("@MobileNumber", txtMobileNo.Text); 
        cmd.Parameters.AddWithValue("@DateOfBirth", dateTimePickerDOB.Text); 
        cmd.Parameters.AddWithValue("@HomePhone", txtHomePhone.Text); 
        cmd.Parameters.AddWithValue("@DistchargeCertNumber", txtNYSCCertNumder.Text); 
        cmd.Parameters.AddWithValue("@SchoolAttended", txtSchoolAttended.Text); 
        cmd.Parameters.AddWithValue("@NYSCStatus", comboBoxNYSCStatus.Text); 
        cmd.Parameters.AddWithValue("@AgeLimit", cbxAgeLimit.Text); 

        int affectedRows = cmd.ExecuteNonQuery(); 
        MessageBox.Show(affectedRows + "Row inserted!"); 

        SqlDataAdapter da = new SqlDataAdapter(command); 

        DataSet ds = new DataSet(); 
        da.Fill(ds, "Employment"); 


        FillControls(); 

        btnNext.Enabled = true; 
        // btnPrevious.Enabled = true;   
       } 
+2

“我有挑战”不是问题。请具体说明。 – OldProgrammer

回答

0

编码它,你需要提供的SqlConnectionSqlDataAdapter的,如果你要检索的数据备份。否则,您可以删除以下4行代码。

var query = "SELECT Id,Title FROM EmploymentDb"; 
SqlDataAdapter da = new SqlDataAdapter(query, conn); 
              ^^^^^^ 

DataSet ds = new DataSet(); 
da.Fill(ds, "Employment"); 
+0

我相信Adapter使用传递给它的Command对象的Connection。但是在OP的代码中,'command' SqlCommand对象没有任何与它关联的SQL文本。无法填写。 – LarsTech

相关问题