如何从gridview填充数据库表? [英] How to populate database table from a gridview?

查看:107
本文介绍了如何从gridview填充数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题取决于我需要从gridview填充数据库中的表。我从ViewState的数据表中填充了gridview。有人可以帮帮我吗?



我尝试过的事情:



< pre lang =c#> string str = ConfigurationManager.ConnectionStrings [ 构造]的ConnectionString。
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand( 选择TOP 1 *来自预订,con);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
bookingId = Convert.ToInt32(reader [ Id]);
}

Booking_Tax(bookingId); // 这里我有多个条目具有相同bookingId的税

解决方案

您好,



要将记录插入数据库,您需要通过插入记录来读取gridview中的每一行。



这里!一个例子。



  private   void  Booking_Tax( Int32  id)
{
string str = ConfigurationManager.ConnectionStrings [ ConStr]。ConnectionString;
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;

// 读取gridview中的每一行
foreach (GridViewRow gvrow in this .grd_result.Rows )
{
// 插入查询
cmd.CommandText = INSERT INTO mytable(id,tax1,taxt2)VALUE( + id.ToString()+ < span class =code-string> + gvrow.Cells [ 0 ] .Text + + gvrow.Cells [ 2 ]。文本+ ;

// 执行数据库中插入记录的查询
cmd.ExecuteNonQuery();
}
}


my question depends upon my need to populate a table in database, from a gridview. I've populated that gridview from a datatable from ViewState. Can anybody help me?

What I have tried:

string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
                SqlConnection con = new SqlConnection(str);
                con.Open();
                SqlCommand cmd = new SqlCommand("Select TOP 1 * from Bookings", con);
                SqlDataReader reader = cmd.ExecuteReader();
                if(reader.Read())
                {
                    bookingId = Convert.ToInt32(reader["Id"]);
                }

                Booking_Tax(bookingId);//Here I have multiple entries for taxes with same bookingId

解决方案

Hi,

To you insert the records into the database, you need to read each row in the gridview by inserting the records.

Here! An example.

private void Booking_Tax(Int32 id)
        {
            string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;

            //read each row in your gridview
            foreach (GridViewRow gvrow in this.grd_result.Rows)
            { 
                //insert query
                cmd.CommandText = "INSERT INTO mytable (id, tax1, taxt2) VALUE (" + id.ToString() + ", " + gvrow.Cells[0].Text + ", " + gvrow.Cells[2].Text + ")";

                //execute the query for insert record in database
                cmd.ExecuteNonQuery();
            }
        }


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

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