将多个值从datagridview保存到数据库 [英] Save multiple value from datagridview to database

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

问题描述

我在datagridview中有一些行。



我想将它们一起保存在数据库中...



请帮我这样做

先谢谢

I have some rows in datagridview.

I want to save them together in database...

Please help me to do that
Thanks in Advance

推荐答案

下面是示例代码试试这个:





Here is sample code try this:


protected void savedatafromgv()
       {

           foreach (DataGridRows g1 in dg_AgentSFR.Rows)
            {

                SqlConnection con = new SqlConnection(strConnString);
                SqlCommand cmd = con.CreateCommand();
                cmd = new SqlCommand("INSERT INTO TB_TransAgenSeaFreightRate(POL,POD,FORWARDER,FORWARDER REFERENCE,SHIPPING LINE,CONTAINER TYPE,CONTAINER SIZE,VALIDITY FROM,VALIDITY TO,BASIC RATE,PAF,CAF,PSS,TOTAL AMOUNT,REE DAYS,CREDIT DAYS,NIT DEPOSIT,COMPANYID,ISACTIVE) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "','" + g1.Cells[10].Text + "','" + g1.Cells[11].Text + "','" + g1.Cells[12].Text + "','" + g1.Cells[13].Text + "','" + g1.Cells[14].Text + "','" + g1.Cells[15].Text + "','" + g1.Cells[16].Text + "',1,'" + TXTCompanyID.Text + "')", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            Response.Write("Records inserted successfully");

       }





避免sql注入使用cmd.parameter.add ....... ...........



to avoid sql injection use "cmd.parameter.add .................."


参考: insert-all-data-of-datagridview-to-database-at-once [ ^ ]

但是,您应该将循环中的sql代码更改为使用参数化查询来防止SQL Server中的SQL注入攻击 [ ^ ]
Refer: insert-all-data-of-a-datagridview-to-database-at-once[^]
However, you should change the sql code in the loop to Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]


Refer



如何在sql数据库中保存datagridview多行 [ ^ ]
Refer

how to save datagridview multiple row in sql database[^]


这篇关于将多个值从datagridview保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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