Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Windows窗体如何将Datagridview转换为数据库(Sql Server)?_C#_Sql_Sql Server_Sql Server 2008_C# 4.0 - Fatal编程技术网

C#Windows窗体如何将Datagridview转换为数据库(Sql Server)?

C#Windows窗体如何将Datagridview转换为数据库(Sql Server)?,c#,sql,sql-server,sql-server-2008,c#-4.0,C#,Sql,Sql Server,Sql Server 2008,C# 4.0,我需要将Datagridview中的数据插入Sql Server 2008中的数据库。但是不要工作 “.Rows(i)”消息错误: “非发票成员”System.Windows.Forms.DataGridView.Rows“无法 像一种方法一样使用。” 代码C#(Windows窗体) SqlConnection Conn=newsqlconnection(); SqlCommand cmd=新的SqlCommand(); StringBuilder sb=新的StringBuilder(); s

我需要将Datagridview中的数据插入Sql Server 2008中的数据库。但是不要工作

“.Rows(i)”消息错误:

“非发票成员”System.Windows.Forms.DataGridView.Rows“无法 像一种方法一样使用。”

代码C#(Windows窗体)

SqlConnection Conn=newsqlconnection();
SqlCommand cmd=新的SqlCommand();
StringBuilder sb=新的StringBuilder();
sqltr;
私有void testExcel_加载(对象发送方,事件参数e)
{
字符串appConn=ConfigurationManager.ConnectionString[“connDB”].ConnectionString;
Conn=newsqlconnection();
if(Conn.State==ConnectionState.Open)
{
康涅狄格州关闭();
}
Conn.ConnectionString=appConn;
Conn.Open();
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
tr=连接开始时的状态();
sb.删除(0,sb.长度);
sb.追加(“插入TableSet(Id、AsId、AsRefid、Invid、TypeName)”;
sb.Append(“值(@Id、@AsId、@AsRefid、@Invid、@TypeName)”);
字符串sqlSave=sb.ToString();
对于(int i=0;i

非常感谢您抽出时间。:)

您应该使用行[i]而不是单元格:

dataGridView1.Rows[i].Cells[0].Value

看看

用方括号替换圆括号即可

dataGridView1.Rows[i].Cells[0].Value;

我建议使用SqlBulkCopy。您可以轻松获得DataTable格式的GridView数据,并且可以将DataTable传递给SqlBulkCopy WriteToServer 您可以检查这个示例。

尝试
。行[i]
单元格[n]
(这是转换后的VB代码吗?)请在答案中写下受影响的代码,不要只引用指向解决方案的链接。
dataGridView1.Rows[i].Cells[0].Value;