我正在使用vs 2013,我正在使用服务基础数据库为我的基于Windows的应用程序我正在创建员工管理应用程序,同时插入数据它显示我已插入记录但数据正在数据库中更新,任何人都可以帮助我...
void metroButton1_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
con = new SqlConnection(cs.DBcon);
using (SqlCommand cmd = new SqlCommand("INSERT INTO tbl_employee VALUES(@Designation, @Date, @Employee_name,@Leave,@L_Reason,@Performance,@Payment,@Petrol,@Grand_Total)", con))
{
cmd.Parameters.AddWithValue("@Designation", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@Employee_name", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@Leave", dataGridView1.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@L_Reason", dataGridView1.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@Performance", dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@Payment", dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@Petrol", dataGridView1.Rows[i].Cells[7].Value);
cmd.Parameters.AddWithValue("@Grand_Total", dataGridView1.Rows[i].Cells[8].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
MessageBox.Show("Records inserted.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:0)
我建议在ExecuteNonQuery之前添加con.BeginTransaction(),在ExecuteNonQuery之后添加con.Commit()。
答案 1 :(得分:0)
您应该在创建命令实例之前打开连接,如下所示:
void metroButton1_Click(object sender, EventArgs e)
{
try
{
con = new SqlConnection(cs.DBcon);
con.Open(); //Open the connection
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tbl_employee VALUES(@Designation, @Date, @Employee_name,@Leave,@L_Reason,@Performance,@Payment,@Petrol,@Grand_Total)", con)) //Now create the command
{
cmd.Parameters.AddWithValue("@Designation", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@Employee_name", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@Leave", dataGridView1.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@L_Reason", dataGridView1.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@Performance", dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@Payment", dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@Petrol", dataGridView1.Rows[i].Cells[7].Value);
cmd.Parameters.AddWithValue("@Grand_Total", dataGridView1.Rows[i].Cells[8].Value);
cmd.ExecuteNonQuery();
}
}
con.Close();
MessageBox.Show("Records inserted.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 2 :(得分:0)
执行连接内的所有内容,如下所示
void metroButton1_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
using(SqlConnection connection = new SqlConnection(cs.DBcon))
{
connection.Open();
cmd.Parameters.AddWithValue("@Designation", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@Employee_name", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@Leave", dataGridView1.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@L_Reason", dataGridView1.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@Performance", dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@Payment", dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@Petrol", dataGridView1.Rows[i].Cells[7].Value);
cmd.Parameters.AddWithValue("@Grand_Total", dataGridView1.Rows[i].Cells[8].Value);
cmd.ExecuteNonQuery();
connection.Close();
}
}
MessageBox.Show("Records inserted.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}