如何将我的Windows窗体中的记录发布到我的数据库

时间:2017-04-12 21:39:59

标签: c#

美好的一天,感谢先前的协助。请尝试将记录从我的窗口表格发布到数据库,我遇到了挑战,我该怎么做? 下面是我用

编码的代码片段
     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;        
                }

1 个答案:

答案 0 :(得分:0)

如果要重新检索数据,则需要为 SqlDataAdapter 提供 SqlConnection 。否则,您可以删除以下4行代码。

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

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