怎样将DataGridView中显示的数据一次性保存到数据库中

颜挺帅 2010-08-04 04:50:53
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\板材切割\Part.mdb";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
List<string> sqls = new List<string>();
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
Part_ID = dataGridView1.Rows[i].Cells[0].Value.ToString();
Length = Convert.ToSingle(dataGridView1.Rows[i].Cells[1].Value.ToString());
Width = Convert.ToSingle(dataGridView1.Rows[i].Cells[2].Value.ToString());
Depth = Convert.ToSingle(dataGridView1.Rows[i].Cells[3].Value.ToString());
Num = Convert.ToInt16(dataGridView1.Rows[i].Cells[4].Value.ToString());
Depict = dataGridView1.Rows[i].Cells[5].Value.ToString();



//string sql = string.Format("insert into Part values('{0}')", dataGridView1[0, i].Value .ToString());
string sql = "insert into Part(Part_ID,Length,Width,Depth,Num,Depict) values('" + Part_ID + "' ," + Length + " ," + Width + " ," + Depth + " ," + Num + " ,'" + Depict + "');";

//string sql = "insert into Part(Part_ID,Length,Width,Depth,Num,Depict) values('" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "' ," + Convert.ToSingle(dataGridView1.Rows[i].Cells[1].Value.ToString()) + " ," + Convert.ToSingle(dataGridView1.Rows[i].Cells[2].Value.ToString()) + " ," + Convert.ToSingle(dataGridView1.Rows[i].Cells[3].Value.ToString()) + " ," + Convert.ToInt16(dataGridView1.Rows[i].Cells[4].Value.ToString()) + " ,'" + dataGridView1.Rows[i].Cells[5].Value.ToString() + "');";
sqls.Add(sql);
}
}
catch (NullReferenceException ne)
{ MessageBox.Show(ne.Message.ToString()); }

//connection=new SqlConnection(...);
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
//connection.Open();
OleDbTransaction transaction = conn.BeginTransaction();
command.Transaction = transaction;//创建事务
try
{
foreach (string sql in sqls)
{
command.CommandText = sql;
command.ExecuteNonQuery();
}
//command.Transaction.Commit();
transaction.Commit();//提交事务
}
catch (Exception)
{
transaction.Rollback();//事务回滚,如果出错,恢复到操作前的状态
MessageBox.Show("未能保存");
}
finally
{
conn.Close();
}
}
我这个方法不对吗,为什么总是提示“未将对象引用设置到对象实例”
...全文
449 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
haiwenbt 2010-08-04
  • 打赏
  • 举报
回复
太复杂了
tyi20 2010-08-04
  • 打赏
  • 举报
回复
把放据放到一个datatable中
a511900703 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxm3630478 的回复:]
未将对象引用设置到对象实例 这个异常很容易找,不是方法不对,是某个变量出现null值...

错误出在哪行,在哪行打断点.....然后调试 看那个变量的值为null
[/Quote]
对头,这个错一般都是某个变量为空了。建议楼主检查下 程序中的变量!!
sxiaohui8709253 2010-08-04
  • 打赏
  • 举报
回复
索引都对上没 数据里头是不是有空值啊
wxm3630478 2010-08-04
  • 打赏
  • 举报
回复
看你的代码 最有可能出现null值的代码可能就是这句啦:

dataGridView1.Rows[i].Cells[1].Value.ToString()

红色部分...自己检查 ..........
jointan 2010-08-04
  • 打赏
  • 举报
回复
你的网格中可能某行某列是空数据

我现在这个论坛的人全爱用dataGridView1.Rows[i].Cells[j]这样的方式取数,效率又低,代码又长,为什么不直接从dataGridView.DataSource中取数呢?
如果非要从单元格取,建议:
dataGridView1.Rows[i].Cells[j].FormattedValue.ToString();
wxm3630478 2010-08-04
  • 打赏
  • 举报
回复
未将对象引用设置到对象实例 这个异常很容易找,不是方法不对,是某个变量出现null值...

错误出在哪行,在哪行打断点.....然后调试 看那个变量的值为null

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧