如何通过存储过程从DataGridView插入数据 [英] How to insert data from DataGridView by Store Procedure
本文介绍了如何通过存储过程从DataGridView插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码:
private void btnSave_Click(object sender, EventArgs e)
{
//MessageBox.Show(grStudentAttend.Rows.Count.ToString());
//int count = grStudentAttend.Rows.Count;
try
{
for (int i = 0; i < grStudentAttend.Rows.Count -1; i++)
{
db.command.Parameters.Add("@StudentName", SqlDbType.VarChar).Value =grStudentAttend.Rows[i].Cells[0].Value.ToString();
db.command.Parameters.Add("@Roll", SqlDbType.VarChar).Value = grStudentAttend.Rows[i].Cells[1].Value.ToString();
db.command.Parameters.Add("@Class", SqlDbType.VarChar).Value = comClass.Text.ToString();
db.command.Parameters.Add("@Shift", SqlDbType.VarChar).Value = cmShift.Text.ToString();
db.command.Parameters.Add("@Section", SqlDbType.VarChar).Value = cmSection.Text.ToString();
db.command.Parameters.Add("@Dated", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(dateTimePicker1.Text.ToString());
db.command.Parameters.Add("@YearAt", SqlDbType.VarChar).Value = dateTimeYear.Text.ToString();
db.command.Parameters.Add("@MonthAt", SqlDbType.VarChar).Value = comMonth.Text.ToString();
if (DBNull.Value.Equals(grStudentAttend.Rows[i].Cells[2].Value) || grStudentAttend.Rows[i].Cells[2].Value.ToString() == "false")
{
db.command.Parameters.Add("@Attend", SqlDbType.Bit).Value = 0;
//db.Adapter("sp_InsertStudentAttendance", true);
//MessageBox.Show("Keep Attendance complete");
}
else
db.command.Parameters.Add("@Attend", SqlDbType.Bit).Value = 1;
db.Adapter("sp_InsertStudentAttendance", true);
MessageBox.Show("Keep Attendance complete");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
问题:仅检查datagridview的checkbox-cell而不是成功插入,否则不插入.我正在尝试从datagridview保留正确和错误的记录,但仅插入真实值.如何插入两条记录?
Problem: Only checkbox-cell of datagridview is checked than insert successfully but, otherwise its not inserted. I am trying to keeping true and false record from datagridview but, only true value is inserted. how can I insert both record?
推荐答案
删除
if (DBNull.Value.Equals(grStudentAttend.Rows[i].Cells[2].Value) || grStudentAttend.Rows[i].Cells[2].Value.ToString() == "false")
条件以及仅注释else
.它将起作用.
condition from the code and also the comment only else
. It will work.
这篇关于如何通过存储过程从DataGridView插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文