我正在尝试将多个textboxes
和datatable
复选框值插入SQL database
。但是,我的数据未插入数据库中。按照下面的代码,弹出“将数据插入数据库时出错!” 。谁能帮助我确定为什么会这样?
private void button1_Click(object sender, EventArgs e)
{
var result = this.dataGridView1[1, 0].Value; //checkbox at row 1
var result2 = this.dataGridView1[1, 1].Value;//checkbox at row 2
var result3 = this.dataGridView1[1, 2].Value;//
var result4 = this.dataGridView1[1, 3].Value;
string mainconn = ConfigurationManager.ConnectionStrings["Test.Properties.Settings.TransitiondayConnectionString"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "INSERT INTO [dbo].[Infotable] (Busnumber,Busmodel,Dt1check1,Dt1check2,Dt1check3,Dt1check4)";
sqlquery+= "values(@Busnumber,@Busmodel,@Dt1check1, @Dt1check2, @Dt1check3, @Dt1check4)";
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
sqlcomm.CommandType = CommandType.Text;
sqlcomm.Parameters.Add("@Busnumber", SqlDbType.Text).Value =label1.ToString();
sqlcomm.Parameters.Add("@Busmodel", SqlDbType.Text).Value = label7.ToString();
sqlcomm.Parameters.Add("@Dt1check1", SqlDbType.Bit).Value = result;
sqlcomm.Parameters.Add("@Dt1check2", SqlDbType.Bit).Value = result2;
sqlcomm.Parameters.Add("@Dt1check3", SqlDbType.Bit).Value = result3;
sqlcomm.Parameters.Add("@Dt1check4", SqlDbType.Bit).Value = result4;
sqlconn.Open();
int test = sqlcomm.ExecuteNonQuery();
if (test != 0)
{
MessageBox.Show("Error inserting data into Database!");
}
else
{
MessageBox.Show("OK!");
}
sqlconn.Close();
}