我正在使用vs 2013,我正在使用基于Windows的应用程序的服务基础数据库我正在创建员工管理应用程序,同时插入数据它显示我记录插入但数据正在数据库中更新,任何人都可以帮我...数据不在数据库中插入c#winform
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);
}
}
什么是错误讯息? – CodeNotFound
其实你有什么错误? – Jamil
您正在尝试创建没有vaild SQL连接的SQLCommand。在创建命令实例之前打开连接。 – robot9706