C#.NET中如何批量插入大量数据到数据库中

2019-12-26 14:13:19王旭
  • if (storedata[k, 1] != "")  excelBom++; 
  • if (excelBom == 0)  { 
  • Response.Write("<script language=javascript>alert('您导入的表格不合格式!')</script>");  } 
  • else  { 
  • //LoadDataToDataBase(storedata,excelBom)//该函数主要负责将 storedata 中有用的数据写入到数据库中,在此不是问题的关键省略   } 
  • }  protected void btninsert_Click(object sender, EventArgs e) 
  • {  foreach (GridViewRow gv in dgBom.Rows)  
  • {  //我的连接字符串是写在WEB.CONFIG中的. 
  • string con = System.Configuration.ConfigurationManager.AppSettings["ConnectionString1"].ToString();  SqlConnection conn = new SqlConnection(con); 
  • SqlCommand cmd = conn.CreateCommand();  cmd.CommandType = CommandType.Text; 
  • cmd.CommandText = "insert into student (studentnumber,studentname) values(@studentnumber,@studentname)";  cmd.Parameters.Add("@studentnumber", SqlDbType.NVarChar, 20); 
  • cmd.Parameters.Add("@studentname", SqlDbType.NVarChar, 10);  cmd.Parameters["@studentname"].Value = ((TextBox)gv.FindControl("studentname")).Text; 
  • cmd.Parameters["@studentnumber"].Value = ((TextBox)gv.FindControl("studentnumber")).Text;  try 
  • {  conn.Open(); 
  • cmd.ExecuteNonQuery();  conn.Close(); 
  • }  finally 
  • {  if (conn != null) 
  • conn.Dispose();  } 
  • }  }