从gridview向数据库插入多行 [英] insert multiple rows from gridview to database

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

问题描述

我有一个购物车的表必须将这些值插入另一个表,但它只取最后一个值

我的代码如下

i have one table for cart have to insert those values into another table but its takes only the last value
my code as follows

if (Convert.ToDecimal(Label1.Text) > 0)
       {
         if (Convert.ToDecimal(Label1.Text) > 0)
        {
            string proid = Application["id"].ToString();
            con.Open();
            da = new SqlDataAdapter("select tpd.productid,tpd.productname,tpd.ProductImage,tpd.price,tpd.qty,tpd.totalcost,tpd.cdate from rsa_addtocart tpd where tpd.productid=" + proid + " ", con);
            ds = new DataSet();
            da.Fill(ds, "tbl_tpd");
            if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
           {

               using (var command = new SqlCommand("insert into rsa_Orderconfirmation(UserId, productid, productname, ProductImage, price, qty, totalcost, cdate) values (@UserId, @productid, @productname, @ProductImage, @price, @qty, @totalcost, @cdate)", con))
               {
                   for (int z = 0; z < this.Gridcart.Rows.Count; z++)
                   {
                       command.Parameters.Clear();
                       command.Parameters.AddWithValue("@UserId", Session["users"]);
                       command.Parameters.AddWithValue("@productid", ds.Tables[0].Rows[0][0]);
                       command.Parameters.AddWithValue("@productname", ds.Tables[0].Rows[0][1]);
                       command.Parameters.AddWithValue("@ProductImage", ds.Tables[0].Rows[0][2]);
                       command.Parameters.AddWithValue("@price", ds.Tables[0].Rows[0][3]);
                       command.Parameters.AddWithValue("@qty", ds.Tables[0].Rows[0][4]);
                       command.Parameters.AddWithValue("@totalcost", ds.Tables[0].Rows[0][5]);
                       command.Parameters.AddWithValue("@cdate", DateTime.Now);

                       if (con.State != ConnectionState.Open)
                       {
                           con.Open();
                           try
                           {
                               command.ExecuteNonQuery();
                           }
                           finally
                           {
                               con.Close();
                           }
                       }
                       else
                       {
                           command.ExecuteNonQuery();
                       }
                   }

                       Response.Redirect("~/BillingDetails.aspx");
                       con.Close();
                   }
            }
            }



只插入我要插入的最后一个值在购物车表中的值是什么

如何才能做到有人可以帮助我?


only its inserts the last value i want to insert what are the values in cart table
how it can be done someone can help me?

推荐答案

你正在使用行[0]而不是行[z]。你不是在迭代数据,而是每次都设置相同...

和....

你在将任何数据发送到数据库之前循环使用你的fisnished。难怪结果令人失望;)
You are using Rows[0] in stead of Rows[z]. You are not iterating the data but setting the same each time...
and....
you are fisnished looping the for before sending any data to the db. No wonder the result is disappointing ;)


直接在存储过程中传递DataTable。

您可以在SQL中使用UserDefine Table(UDT)并直接使用查询

条件是UDT必须与DataTable具有相同的列,并且要传递给Sql程序。



Ex。在程序中



插入TableName select * from UDT
Directly pass DataTable in stored procedure.
You can use UserDefine Table (UDT) in SQL and directly use query
condition is UDT must have same column as DataTable have which you are passing to Sql procedure.

Ex. in procedure

Insert into TableName select * from UDT


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

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