如何将组合框数据保存到SQL Server 2012数据库中
本文关键字:Server SQL 2012 数据库 保存 组合 数据 | 更新日期: 2023-09-27 18:08:09
我尝试了这个代码,没有组合框它工作。
添加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);
}
}
您在查询中输入了错误的@ strbloodgroup。改成@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
你打错字了
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
您在命令中定义的参数名称与AddWithValue
不匹配。您在命令中将其定义为strBloodGrourp
,但在AddWithValue
中将其声明为strBloodGroup
。
将命令中的参数名称更改为;
@strGender, @strBloodGroup, @dtDOB
还有几件事;
- 使用
using
语句自动处理连接和命令,而不是手动调用Close
方法。 - 不要使用
AddWithValue
方法。它有时可能产生意想不到的和令人惊讶的结果。使用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.
}