C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中
在WEB項目開發(fā)過程中有時會碰到批量插入數(shù)據(jù)到數(shù)或者是將EXCEL文件據(jù)入到數(shù)據(jù)庫中.為了方便實現(xiàn)可以先將EXCEL導(dǎo)入到GRIDVIEW中然后一次批量插入.實現(xiàn)代碼如下:
前臺代碼
<asp:GridView ID="dgBom" runat="server" AutoGenerateColumns="false" CellPadding="1" CellSpacing="2"> <HeaderStyle BackColor="#ededed" /> <Columns> <asp:TemplateField HeaderText="學(xué)號"> <ItemTemplate> <asp:TextBox ID="studentnumber" runat="server" Text='<%#Eval("studentnumber") %>' ></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="學(xué)生姓名"> <ItemTemplate> <asp:TextBox ID="studentname" runat="server" Text='<%#Eval("studentname") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:FileUpload ID="FileUpload1" runat="server" Font-Italic="False" /> <asp:Button ID="btn2" runat="server" OnClick="btn2_Click" Text="導(dǎo)入數(shù)據(jù)" /> <asp:Button ID="btninsert" runat="server" OnClick="btninsert_Click" Text="插入到數(shù)據(jù)庫中"/>
后臺代碼:
//首先在命名空間中加入以下兩行 using System.Data.SqlClient; using System.Data.OleDb; protected void btn2_Click(object sender, EventArgs e) { string filepath = FileUpload1.PostedFile.FileName; ReadExcel(filepath, dgBom); } public void ReadExcel(string sExcelFile, GridView dgBom) { DataTable ExcelTable; DataSet ds = new DataSet(); //Excel的連接 OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sExcelFile + ";" + "Extended Properties=Excel 8.0;"); objConn.Open(); DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); string tableName = schemaTable.Rows[0][2].ToString().Trim();//獲取 Excel 的表名,默認(rèn)值是sheet1 string strSql = "select * from [" + tableName + "]"; OleDbCommand objCmd = new OleDbCommand(strSql, objConn); OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn); myData.Fill(ds, tableName);//填充數(shù)據(jù) dgBom.DataSource =ds; dgBom.DataBind(); objConn.Close(); ExcelTable = ds.Tables[tableName]; int iColums = ExcelTable.Columns.Count;//列數(shù) int iRows = ExcelTable.Rows.Count;//行數(shù) //定義二維數(shù)組存儲 Excel 表中讀取的數(shù)據(jù) string[,] storedata = new string[iRows, iColums]; for(int i=0;i<ExcelTable.Rows.Count;i++) for (int j = 0; j < ExcelTable.Columns.Count; j++) { //將Excel表中的數(shù)據(jù)存儲到數(shù)組 storedata[i, j] = ExcelTable.Rows[i][j].ToString(); } int excelBom = 0;//記錄表中有用信息的行數(shù),有用信息是指除去表的標(biāo)題和表的欄目,本例中表的用用信息是從第三行開始 //確定有用的行數(shù) for (int k = 2; k < ExcelTable.Rows.Count; k++) if (storedata[k, 1] != "") excelBom++; if (excelBom == 0) { Response.Write("<script language=javascript>alert('您導(dǎo)入的表格不合格式!')</script>"); } else { //LoadDataToDataBase(storedata,excelBom)//該函數(shù)主要負(fù)責(zé)將 storedata 中有用的數(shù)據(jù)寫入到數(shù)據(jù)庫中,在此不是問題的關(guān)鍵省略 } } 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(); } } }
以上內(nèi)容就是本文的全部敘述,希望對大家學(xué)習(xí)C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中有所幫助。
上一篇:C#中using指令的幾種用法
欄 目:C#教程
本文標(biāo)題:C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6891.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
- 01-10C#中DataGridView常用操作實例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#利用反射技術(shù)實現(xiàn)去掉按鈕選中時的邊框效果
- 01-10C#中查找Dictionary中的重復(fù)值的方法
- 01-10C#中TreeView實現(xiàn)適合兩級節(jié)點的選中節(jié)點方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什