如何将组合框数据保存到SQL Server 2012数据库?

时间:2015-08-17 07:19:36

标签: c# sql sql-server

我试过这段代码,没有组合框,它就可以了。

combobox(comboBloodGroup)添加8个所选项目并且所选数据类型为nvchar(50)后,我收到错误

如何编码将组合框数据插入sql数据库。

我的新代码是

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {         
            string strConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\USERS\MYPC\DOCUMENTS\LOGIN.MDF";
            SqlConnection cn = new SqlConnection(strConnectionString);
            cn.Open();
            int iId = Convert.ToInt32(txtId.Text.Trim());
            string strFName = txtFName.Text.Trim();
            string strLName = txtLName.Text.Trim();
            int iAge = Convert.ToInt32(txtAge.Text.Trim());
            string stremail = txtemail.Text.Trim();
            string strMobile = txtMobile.Text.Trim();
            string strAddress = txtAddress.Text.Trim();
            string strCity = txtCity.Text.Trim();
            int iPinCode = Convert.ToInt32(txtPinCode.Text.Trim());
            string strGender;
            if (rbMale.Checked)
                strGender = "Male";
            else
                strGender = "Female";
            string strBloodGroup = comboBloodGroup.SelectedItem.ToString();                
            DateTime dtDOB = dtPickerDOB.Value;
            string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, @strBloodGrourp, @dtDOB)";
            SqlCommand InsertCommand = new SqlCommand(query, cn);
            InsertCommand.Connection = cn;
            InsertCommand.Parameters.AddWithValue("@iId", iId);
            InsertCommand.Parameters.AddWithValue("@strFName", strFName);
            InsertCommand.Parameters.AddWithValue("@strLName", strLName);
            InsertCommand.Parameters.AddWithValue("@iAge", iAge);
            InsertCommand.Parameters.AddWithValue("@stremail", stremail);
            InsertCommand.Parameters.AddWithValue("@strMobile", strMobile);
            InsertCommand.Parameters.AddWithValue("@strAddress", strAddress);
            InsertCommand.Parameters.AddWithValue("@strCity", strCity);
            InsertCommand.Parameters.AddWithValue("@iPinCode", iPinCode);
            InsertCommand.Parameters.AddWithValue("@strGender", strGender);
            InsertCommand.Parameters.AddWithValue("@strBloodGroup", strBloodGroup);
            InsertCommand.Parameters.AddWithValue("@dtDOB", dtDOB);
            InsertCommand.ExecuteNonQuery();             
            MessageBox.Show("New Patient's Data has been added successfully");
            cn.Close();
        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.Message);
        }
    }

3 个答案:

答案 0 :(得分:0)

您在查询中输错了@strBloodGrourp。改为@strBloodGroup。

string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, **@strBloodGrourp**, @dtDOB)";

然后添加参数 @strBloodGroup

答案 1 :(得分:0)

您在命令中定义的参数名称与AddWithValue不匹配。您在命令中将其定义为strBloodGrourp,但您在strBloodGroup中将其声明为AddWithValue

在命令中将参数名称更改为;

@strGender, @strBloodGroup, @dtDOB

还有一些事情;

using(var cn = new SqlConnection(strConnectionString))
using(var InsertCommand = cn.CreateCommand())
{
    // Set your CommandText property with parameter names.
    // Define your parameter names and it's values. Add them to your command.
    // Open your connection.
    // Execute your query.
}

答案 2 :(得分:0)

你有一个错字

string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, @strBloodGrourp, @dtDOB)";

应该阅读

string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, @strBloodGroup, @dtDOB)";

血型中有额外的R