将Datagridview保存到SQL表 [英] Saving a Datagridview into SQL Table

查看:71
本文介绍了将Datagridview保存到SQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以帮我保存
所有datagridview行都放入SQL表.

我尝试使用for循环保存它,但仅
保存第一行.如果我插入多个
行它根本不会保存它.

这是我尝试过的:

Hi,

Can someone please help me to save
all the datagridview rows into a SQL table.

I tried saving it using a for loop, but it only
saves the first rows. If I insert more than one
row it simply doesn''t save it.

This is what I have tried:

private void SavedataGridTable()
        {
            ConnLibrary conn = new ConnLibrary();

            using (SqlCommand sqlcomm = new SqlCommand())
            {
                for (int i = 0; i < dgrLoadGrid.Rows.Count - 1; i++)
                {

                    strNameS = dgrLoadGrid[0, i].Value.ToString();
                    intPrice = Convert.ToInt32(dgrLoadGrid[1, i].Value);
                    intQuantity = Convert.ToInt32(dgrLoadGrid[2, i].Value);
                    dteNow = dateTimePicker1.Value;



                    sqlcomm.Connection = conn.Connection;
                    sqlcomm.CommandType = CommandType.StoredProcedure;
                    sqlcomm.CommandText = "usp_SaveSale";

                    sqlcomm.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = strNameS;
                    sqlcomm.Parameters.Add("@Price", SqlDbType.Int).Value = intPrice;
                    sqlcomm.Parameters.Add("@Quantity", SqlDbType.Int).Value = intQuantity;
                    sqlcomm.Parameters.Add("@Date", SqlDbType.Date).Value = dteNow;
                    sqlcomm.ExecuteNonQuery();
                    MessageBox.Show("Sale Done");
                }
            }
        }

推荐答案

尝试:
for (int i = 0; i < dgrLoadGrid.Rows.Count - 1; i++)
{
 using (SqlCommand sqlcomm = new SqlCommand())
 {
   ... // the code as it is



我会删除MessageBox.Show("Sale Done"); :)

最好的问候
Espen Harlinn



I would remove the MessageBox.Show("Sale Done"); though :)

Best regards
Espen Harlinn


这篇关于将Datagridview保存到SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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