首页 > 编程 > C# > 正文

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

2019-10-29 21:36:48
字体:
来源:转载
供稿:网友

这篇文章主要给大家介绍C#.net中如何批量插入大量数据到数据库中,本文涉及到C#.net中批量插入数据到数据库中方面的内容,对C#.net批量插入数据到数据库中感兴趣的朋友可以参考下本篇文章

在WEB项目开发过程中有时会碰到批量插入数据到数或者是将EXCEL文件据入到数据库中.为了方便实现可以先将EXCEL导入到GRIDVIEW中然后一次批量插入.实现代码如下:

前台代码

 

 
  1. <asp:GridView ID="dgBom" runat="server" AutoGenerateColumns="false" CellPadding="1" CellSpacing="2"
  2. <HeaderStyle BackColor="#ededed" /> 
  3. <Columns> 
  4. <asp:TemplateField HeaderText="学号"
  5. <ItemTemplate> 
  6. <asp:TextBox ID="studentnumber" runat="server" Text='<%#Eval("studentnumber") %>' ></asp:TextBox> 
  7. </ItemTemplate> 
  8. </asp:TemplateField> 
  9. <asp:TemplateField HeaderText="学生姓名"
  10. <ItemTemplate> 
  11. <asp:TextBox ID="studentname" runat="server" Text='<%#Eval("studentname") %>'></asp:TextBox> 
  12. </ItemTemplate> 
  13. </asp:TemplateField> 
  14. </Columns> 
  15. </asp:GridView> 
  16. <asp:FileUpload ID="FileUpload1" runat="server" Font-Italic="False" /> 
  17. <asp:Button ID="btn2" runat="server" OnClick="btn2_Click" Text="导入数据" /> 
  18. <asp:Button ID="btninsert" runat="server" OnClick="btninsert_Click" Text="插入到数据库中"/> 

后台代码:

 

 
  1. //首先在命名空间中加入以下两行 
  2. using System.Data.SqlClient; 
  3. using System.Data.OleDb; 
  4. protected void btn2_Click(object sender, EventArgs e) 
  5. string filepath = FileUpload1.PostedFile.FileName; 
  6. ReadExcel(filepath, dgBom); 
  7. public void ReadExcel(string sExcelFile, GridView dgBom) 
  8. DataTable ExcelTable; 
  9. DataSet ds = new DataSet(); 
  10. //Excel的连接 
  11. OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sExcelFile + ";" + "Extended Properties=Excel 8.0;"); 
  12. objConn.Open(); 
  13. DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); 
  14. string tableName = schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名,默认值是sheet1 
  15. string strSql = "select * from [" + tableName + "]"
  16. OleDbCommand objCmd = new OleDbCommand(strSql, objConn); 
  17. OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn); 
  18. myData.Fill(ds, tableName);//填充数据 
  19. dgBom.DataSource =ds; 
  20. dgBom.DataBind(); 
  21. objConn.Close(); 
  22. ExcelTable = ds.Tables[tableName]; 
  23. int iColums = ExcelTable.Columns.Count;//列数 
  24. int iRows = ExcelTable.Rows.Count;//行数 
  25. //定义二维数组存储 Excel 表中读取的数据 
  26. string[,] storedata = new string[iRows, iColums]; 
  27. for(int i=0;i<ExcelTable.Rows.Count;i++) 
  28. for (int j = 0; j < ExcelTable.Columns.Count; j++) 
  29. //将Excel表中的数据存储到数组 
  30. storedata[i, j] = ExcelTable.Rows[i][j].ToString(); 
  31. int excelBom = 0;//记录表中有用信息的行数,有用信息是指除去表的标题和表的栏目,本例中表的用用信息是从第三行开始 
  32. //确定有用的行数 
  33. for (int k = 2; k < ExcelTable.Rows.Count; k++) 
  34. if (storedata[k, 1] != ""
  35. excelBom++; 
  36. if (excelBom == 0) 
  37. Response.Write("<script language=javascript>alert('您导入的表格不合格式!')</script>"); 
  38. else 
  39. //LoadDataToDataBase(storedata,excelBom)//该函数主要负责将 storedata 中有用的数据写入到数据库中,在此不是问题的关键省略  
  40. protected void btninsert_Click(object sender, EventArgs e) 
  41. foreach (GridViewRow gv in dgBom.Rows)  
  42. //我的连接字符串是写在WEB.CONFIG中的. 
  43. string con = System.Configuration.ConfigurationManager.AppSettings["ConnectionString1"].ToString(); 
  44. SqlConnection conn = new SqlConnection(con); 
  45. SqlCommand cmd = conn.CreateCommand(); 
  46. cmd.CommandType = CommandType.Text; 
  47. cmd.CommandText = "insert into student (studentnumber,studentname) values(@studentnumber,@studentname)"
  48. cmd.Parameters.Add("@studentnumber", SqlDbType.NVarChar, 20); 
  49. cmd.Parameters.Add("@studentname", SqlDbType.NVarChar, 10); 
  50. cmd.Parameters["@studentname"].Value = ((TextBox)gv.FindControl("studentname")).Text; 
  51. cmd.Parameters["@studentnumber"].Value = ((TextBox)gv.FindControl("studentnumber")).Text; 
  52. try 
  53. conn.Open(); 
  54. cmd.ExecuteNonQuery(); 
  55. conn.Close(); 
  56. finally 
  57. if (conn != null
  58. conn.Dispose(); 

以上内容就是本文的全部叙述,希望对大家学习C#.NET中如何批量插入大量数据到数据库中有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表