如何将多个记录插入到输入到数据网格的数据库表中。 [英] How to insert multiple records into a database table that are entered into a datagrid.

查看:74
本文介绍了如何将多个记录插入到输入到数据网格的数据库表中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个记录插入到输入到数据网格的数据库表中。只需一次保存就可以使用C#更新数据网格中的所有记录。任何人都可以在这方面帮助我。



请告诉我解决方案,因为我已经浪费了大约一整天的时间。我没有在谷歌搜索中找到帮助。



任何帮助表示

How to insert multiple records into a database table that are entered into a datagrid. With a single shot of Save all the records in the datagrid should be updated using C# . Can any one help me in this regard.

Please tell me the solution as i have wasted near about whole day on this. I did not find help in google search.

Any help appreciated

推荐答案

试试这个: -

Try this :-
foreach(DataGridViewRow row in dg.Rows)
{
  string mSQL= "Insert Into Table_Name (Field1,Field2....) Values                                                        ('"+row.Cells["Col1.Name"].Value+"','"+row.Cells["Col2.Name"].Value+"'...)";
            SqlConnection mConn = new SqlConnection(connString);
            SqlCommand mCmd = new SqlCommand(mSQL, mConn);
            int mReturnVal;
            Exception ex = new Exception();

            try
            {
                mConn.Open();
                mReturnVal = mCmd.ExecuteNonQuery();
            }

            catch
            {
                throw new Exception(ex.Message, ex);
            }

            finally
            {
                mConn.Close();
            }
 
}





希望这会对你有所帮助。



Hope this will help you.


INSERT INTO `tablename`(column1,column2,column3) VALUES
('aaa','aaa','aaa'),
('bbb','bbb','bbb'),
('ccc','ccc','ccc');


这篇关于如何将多个记录插入到输入到数据网格的数据库表中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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