国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#.NET中如何批量插入大量數據到數據庫中

2020-01-24 01:24:37
字體:
來源:轉載
供稿:網友

在WEB項目開發過程中有時會碰到批量插入數據到數或者是將EXCEL文件據入到數據庫中.為了方便實現可以先將EXCEL導入到GRIDVIEW中然后一次批量插入.實現代碼如下:

前臺代碼

<asp:GridView ID="dgBom" runat="server" AutoGenerateColumns="false" CellPadding="1" CellSpacing="2"><HeaderStyle BackColor="#ededed" />  <Columns>   <asp:TemplateField HeaderText="學號">    <ItemTemplate>     <asp:TextBox ID="studentnumber" runat="server" Text='<%#Eval("studentnumber") %>' ></asp:TextBox>    </ItemTemplate>   </asp:TemplateField>   <asp:TemplateField HeaderText="學生姓名">    <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="導入數據" />  <asp:Button ID="btninsert" runat="server" OnClick="btninsert_Click" Text="插入到數據庫中"/>

后臺代碼:

//首先在命名空間中加入以下兩行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 的表名,默認值是sheet1    string strSql = "select * from [" + tableName + "]";    OleDbCommand objCmd = new OleDbCommand(strSql, objConn);    OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);    myData.Fill(ds, tableName);//填充數據    dgBom.DataSource =ds;    dgBom.DataBind();    objConn.Close();    ExcelTable = ds.Tables[tableName];    int iColums = ExcelTable.Columns.Count;//列數    int iRows = ExcelTable.Rows.Count;//行數    //定義二維數組存儲 Excel 表中讀取的數據    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表中的數據存儲到數組        storedata[i, j] = ExcelTable.Rows[i][j].ToString();      }    int excelBom = 0;//記錄表中有用信息的行數,有用信息是指除去表的標題和表的欄目,本例中表的用用信息是從第三行開始    //確定有用的行數    for (int k = 2; k < ExcelTable.Rows.Count; k++)      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();      }    }  }

以上內容就是本文的全部敘述,希望對大家學習C#.NET中如何批量插入大量數據到數據庫中有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通化市| 阿尔山市| 沐川县| 娄烦县| 湾仔区| 宕昌县| 八宿县| 汶上县| 马鞍山市| 新田县| 上饶县| 宁夏| 板桥市| 沧州市| 湘西| 连平县| 大港区| 甘谷县| 正安县| 武胜县| 安宁市| 洪湖市| 苗栗市| 济宁市| 龙胜| 专栏| 宝丰县| 敖汉旗| 广灵县| 闵行区| 舞阳县| 江口县| 清远市| 安丘市| 富阳市| 原阳县| 民丰县| 壤塘县| 阳泉市| 朝阳市| 阳江市|