从C#中的datagridview在SQL数据库中插入多行 [英] Insert multiple rows in SQL database from datagridview in C#

查看:135
本文介绍了从C#中的datagridview在SQL数据库中插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在datagridview中保存多个新行到数据库表点击btnsave请帮帮我



我尝试过:



尝试

{



for(int i = 0; i< dataGridView1.Rows.Count; i ++)

{



SqlCommand cmd = new SqlCommand(INSERT INTO [Purchase_Order](id ,vcno,supid,cashcred,date,billno,supnm,prebal,totbal)VALUES(@ val1,@ val2,@ val3,@ val4,@ val5,@ val6,@ val7,@ val8,@ val9),con) ;

cmd.Parameters.AddWithValue(@ val1,dataGridView1.Rows [i] .Cells [0] .Value);

cmd.Parameters.AddWithValue( @ val2,dataGridView1.Rows [i] .Cells [1] .Value);

cmd.Parameters.AddWithValue(@ val3,dataGridView1.Rows [i] .Cells [2]。值);

cmd.Parameters.AddWithValue(@ val4,dataGridView1.Rows [i] .Cells [3] .Value);

cmd.Parameters.AddWithValue(@ val5,dataGridView1.Rows [i] .Cells [4] .Value);

cmd.Parameters.AddWithValue(@ val6,dataGridView1 .Rows [i] .Cells [5] .Value);

cmd.Parameters.AddWithValue(@ val7,dataGridView1.Rows [i] .Cells [6] .Value);

cmd.Parameters.AddWithValue(@ val8,dataGridView1.Rows [i] .Cells [7] .Value);

cmd.Parameters.AddWithValue(@ val9 ,dataGridView1.Rows [i] .Cells [8] .Value);

lblsaved.Text =已保存的成功;



con .Open();

cmd.ExecuteNonQuery();



con.Close();

< br $>
}

}

catch(System.Data.SqlClient.SqlException es)

{

MessageBox.Show(Eror!+ es.Messag e,错误);

}

解决方案

你的代码看起来很好,但是数据库中有很多东西可能会出错值,约束等,所以确切的错误信息会有所帮助。



这是一篇有趣的文章可能会对你有所帮助:

< a href =http://www.codeproject.com/Articles/12846/Auto-SavingforeGGridView-Rows-to-a-SQL-Server-Data>自动保存DataGridView行到SQL Server数据库 [ ^ ]

I want to save multiple new rows from datagridview to database table at on click on btnsave please help me

What I have tried:

try
{

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{

SqlCommand cmd = new SqlCommand("INSERT INTO [Purchase_Order] (id,vcno,supid,cashcred,date,billno,supnm,prebal,totbal) VALUES (@val1,@val2,@val3,@val4,@val5,@val6,@val7,@val8,@val9)", con);
cmd.Parameters.AddWithValue("@val1", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@val2", dataGridView1.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@val3", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@val4", dataGridView1.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@val5", dataGridView1.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@val6", dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@val7", dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@val8", dataGridView1.Rows[i].Cells[7].Value);
cmd.Parameters.AddWithValue("@val9", dataGridView1.Rows[i].Cells[8].Value);
lblsaved.Text = "Saved Success";

con.Open();
cmd.ExecuteNonQuery();

con.Close();

}
}
catch (System.Data.SqlClient.SqlException es)
{
MessageBox.Show("Eror!" + es.Message, "Error");
}

解决方案

Your code seems fine, but lots of things can go wrong in the database with null values, constraints etc., so the exact error message would be helpful.

Here is an interesting article that might help you:
Auto Saving DataGridView Rows to a SQL Server Database[^]


这篇关于从C#中的datagridview在SQL数据库中插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆