如何把gridview中数据导入SQL数据库?
基本需求如下,通过查询,从一个数据表中查出数据在gridview1中显示出来,包括id,name,content三项目,我希望点保存按钮时,能遍历gridview1把数据导入另一数据表Table中,请高手指点,写出详细的代码,谢谢!请包括遍历gridview1的语句和添加数据的语句!
------解决方案--------------------
string strConnection = ConfigurationManager.ConnectionStrings["decoraconn"].ConnectionString;
SqlConnection con=new SqlConnection(strConnection );
con.Open();
if (GridView1.rows.cout > 0)
{
foreach (GridViewRow row in GridView1)
{
string strId = row.Cells[0].Text.ToString();
string strName = row.Cells[1].Text.ToString();
string strContent = row.Cells[2].Text.ToString();
string strSQL = "insert into Table1(id,name,content) values('"+ strId +"','"+strName +"','"+strContent +"')";
SqlCommand com = new SqlCommand(strSQL, con);
com.ExecuteNonQuery();
}
}
con.Close();
------解决方案--------------------
C# code
foreach(DataRow dr in dt.Rows)
{
//写你的插入别一个表的逻辑
// sql = "insert into table2 (col1,col2,col3) values('{0}','{1}','{2}')";
// SqlCommand cmd = new SqlCommand(string.Format(sql,dr[0].ToString(),dr[1].ToString(),dr[2].ToString()),conn);
// cmd.exe....执行命令
}