protected DataTable GetDataFromGrid()
{
DataTable dt1 = new DataTable("table1");
dt1.Columns.Add("byss");
dt1.Columns.Add("sxwss");
dt1.Columns.Add("zss");
dt1.Columns.Add("nj1");
dt1.Columns.Add("nj2");
dt1.Columns.Add("nj3");
dt1.Columns.Add("nj4");
dt1.Columns.Add("nj5");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow gRow = GridView1.Rows[i];
DataRow newRow = dt1.NewRow();
newRow[0] =((TextBox)gRow.FindControl("TextBox1")).Text;
newRow[1] = ((TextBox)gRow.FindControl("TextBox2")).Text;
newRow[2] = ((TextBox)gRow.FindControl("TextBox3")).Text;
newRow[3] = ((TextBox)gRow.FindControl("TextBox4")).Text;
newRow[4]= ((TextBox)gRow.FindControl("TextBox5")).Text;
newRow[5] = ((TextBox)gRow.FindControl("TextBox6")).Text;
newRow[6] = ((TextBox)gRow.FindControl("TextBox7")).Text;
newRow[7] = ((TextBox)gRow.FindControl("TextBox8")).Text;
dt1 .Rows .Add (newRow);
}
dt1 .AcceptChanges ();
return dt1 ; } protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
for (int i = 0; i < dt.Rows.Count;i++ )
{
string byss = dt.Rows[i][0].ToString ();
string sxwss =dt.Rows[i][1].ToString ();
string zss = dt.Rows[i][2].ToString ();
string nj1 = dt.Rows[i][3].ToString ();
string nj2 = dt.Rows[i][4].ToString ();
string nj3 = dt.Rows[i][5].ToString ();
string nj4 = dt.Rows[i][6].ToString ();
string nj5 = dt.Rows[i][7].ToString ();
SqlConnection con = DBConn.createCon();
SqlCommand cmd=new SqlCommand ("insert into fcfn(byss,sxwss,zss,nj1,nj2,nj3,nj4,nj5) values (@byss,@sxwss,@zss,@nj1,@nj2,@nj3,@nj4,@nj5)",con);
SqlParameter paramter = new SqlParameter("@byss", SqlDbType.VarChar);
paramter.Value = byss;
SqlParameter paramter1 = new SqlParameter("@sxwss", SqlDbType.VarChar);
paramter1.Value = sxwss;
SqlParameter paramter2 = new SqlParameter("@zss", SqlDbType.VarChar);
paramter2.Value = zss;
SqlParameter paramter3= new SqlParameter("@nj1", SqlDbType.VarChar);
paramter3.Value = nj1;
SqlParameter paramter4 = new SqlParameter("@nj2", SqlDbType.VarChar);
paramter4.Value = nj2;
SqlParameter paramter5 = new SqlParameter("@nj3", SqlDbType.VarChar);
paramter5.Value = nj3;
SqlParameter paramter6 = new SqlParameter("@nj4", SqlDbType.VarChar);
paramter6.Value = nj4;
SqlParameter paramter7 = new SqlParameter("@nj5", SqlDbType.VarChar);
paramter7.Value = nj5;
con.Open();
cmd.Parameters.Add(paramter);
cmd.Parameters.Add(paramter1);
cmd.Parameters.Add(paramter2);
cmd.Parameters.Add(paramter3);
cmd.Parameters.Add(paramter4);
cmd.Parameters.Add(paramter5);
cmd.Parameters.Add(paramter6);
cmd.Parameters.Add(paramter7);
cmd.ExecuteNonQuery();
this.GridView1.DataBind(); }以上是我写的程序,不知道是哪里的问题,麻烦大家帮忙看下,谢谢!
{
DataTable dt1 = new DataTable("table1");
dt1.Columns.Add("byss");
dt1.Columns.Add("sxwss");
dt1.Columns.Add("zss");
dt1.Columns.Add("nj1");
dt1.Columns.Add("nj2");
dt1.Columns.Add("nj3");
dt1.Columns.Add("nj4");
dt1.Columns.Add("nj5");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow gRow = GridView1.Rows[i];
DataRow newRow = dt1.NewRow();
newRow[0] =((TextBox)gRow.FindControl("TextBox1")).Text;
newRow[1] = ((TextBox)gRow.FindControl("TextBox2")).Text;
newRow[2] = ((TextBox)gRow.FindControl("TextBox3")).Text;
newRow[3] = ((TextBox)gRow.FindControl("TextBox4")).Text;
newRow[4]= ((TextBox)gRow.FindControl("TextBox5")).Text;
newRow[5] = ((TextBox)gRow.FindControl("TextBox6")).Text;
newRow[6] = ((TextBox)gRow.FindControl("TextBox7")).Text;
newRow[7] = ((TextBox)gRow.FindControl("TextBox8")).Text;
dt1 .Rows .Add (newRow);
}
dt1 .AcceptChanges ();
return dt1 ; } protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
for (int i = 0; i < dt.Rows.Count;i++ )
{
string byss = dt.Rows[i][0].ToString ();
string sxwss =dt.Rows[i][1].ToString ();
string zss = dt.Rows[i][2].ToString ();
string nj1 = dt.Rows[i][3].ToString ();
string nj2 = dt.Rows[i][4].ToString ();
string nj3 = dt.Rows[i][5].ToString ();
string nj4 = dt.Rows[i][6].ToString ();
string nj5 = dt.Rows[i][7].ToString ();
SqlConnection con = DBConn.createCon();
SqlCommand cmd=new SqlCommand ("insert into fcfn(byss,sxwss,zss,nj1,nj2,nj3,nj4,nj5) values (@byss,@sxwss,@zss,@nj1,@nj2,@nj3,@nj4,@nj5)",con);
SqlParameter paramter = new SqlParameter("@byss", SqlDbType.VarChar);
paramter.Value = byss;
SqlParameter paramter1 = new SqlParameter("@sxwss", SqlDbType.VarChar);
paramter1.Value = sxwss;
SqlParameter paramter2 = new SqlParameter("@zss", SqlDbType.VarChar);
paramter2.Value = zss;
SqlParameter paramter3= new SqlParameter("@nj1", SqlDbType.VarChar);
paramter3.Value = nj1;
SqlParameter paramter4 = new SqlParameter("@nj2", SqlDbType.VarChar);
paramter4.Value = nj2;
SqlParameter paramter5 = new SqlParameter("@nj3", SqlDbType.VarChar);
paramter5.Value = nj3;
SqlParameter paramter6 = new SqlParameter("@nj4", SqlDbType.VarChar);
paramter6.Value = nj4;
SqlParameter paramter7 = new SqlParameter("@nj5", SqlDbType.VarChar);
paramter7.Value = nj5;
con.Open();
cmd.Parameters.Add(paramter);
cmd.Parameters.Add(paramter1);
cmd.Parameters.Add(paramter2);
cmd.Parameters.Add(paramter3);
cmd.Parameters.Add(paramter4);
cmd.Parameters.Add(paramter5);
cmd.Parameters.Add(paramter6);
cmd.Parameters.Add(paramter7);
cmd.ExecuteNonQuery();
this.GridView1.DataBind(); }以上是我写的程序,不知道是哪里的问题,麻烦大家帮忙看下,谢谢!
放在for循环外面
try catch 一把看看
做插入的时候, DataTable有多少行数据你就向数据库操作多少次, 性能不是很高,
建议lz 做一个多条记录的插入,那样只操作一次就可以全部进行插入