如何在数据库中存储datagridview数据 [英] how to datagridview data store in database

查看:105
本文介绍了如何在数据库中存储datagridview数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 protected void Button4_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                con.Open();
                string ins = "insert into update (Regid,Rollno,Name,Course,Noofbooks,Bookid,Bookname) Values(@Regid,@Rollno,@Name,@Course,@Noofbooks,@Bookid,@Bookname)";
                SqlCommand com = new SqlCommand(ins, con);
                com.Parameters.AddWithValue("@Regid", GridView1.Rows[i].Cells[0].Text);
                com.Parameters.AddWithValue("@Rollno", GridView1.Rows[i].Cells[1].Text);
                com.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[2].Text);
                com.Parameters.AddWithValue("@Course", GridView1.Rows[i].Cells[3].Text);
                com.Parameters.AddWithValue("@Noofbooks", GridView1.Rows[i].Cells[4].Text);
                com.Parameters.AddWithValue("@Bookid", GridView1.Rows[i].Cells[5].Text);
                com.Parameters.AddWithValue("@Bookname", GridView1.Rows[i].Cells[6].Text);
                com.ExecuteNonQuery();
                con.Close();
            }
            Label1.Text = "Added successfully!";
        }
    }
}

推荐答案

UPDATE 是SQL中的保留关键字,你不能命名表更新 !!!

永远不要使用保留关键字作为表/ COLUMN NAME !!!
UPDATE is a reserved keyword in SQL and you can not name a table update!!!
NEVER USE RESERVED KEYWORD AS TABLE/COLUMN NAME!!!


你的创建表名称为update,它是一个关键字,所以使用你的表名这样[更新]它不会显示错误



请按照以下代码:



your create table name as update, its a keyword, so use your table name like this [update] it wont show error

Follow the below code:

"
string ins = "insert into [update] (Regid,Rollno,Name,Course,Noofbooks,Bookid,Bookname) Values(@Regid,@Rollno,@Name,@Course,@Noofbooks,@Bookid,@Bookname)";


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

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