我试过这段代码,没有组合框,它就可以了。
在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);
}
}
答案 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
statement自动处理您的连接和命令,而不是手动调用Close
方法。AddWithValue
方法。 It may generate unexpected and surprising results sometimes。使用Add
方法重载来指定参数类型及其大小。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