最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中

 更新時(shí)間:2015年10月21日 12:02:55   投稿:mrr  
這篇文章主要給大家介紹C#.net中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中,本文涉及到C#.net中批量插入數(shù)據(jù)到數(shù)據(jù)庫(kù)中方面的內(nèi)容,對(duì)C#.net批量插入數(shù)據(jù)到數(shù)據(jù)庫(kù)中感興趣的朋友可以參考下本篇文章

在WEB項(xiàng)目開(kāi)發(fā)過(guò)程中有時(shí)會(huì)碰到批量插入數(shù)據(jù)到數(shù)或者是將EXCEL文件據(jù)入到數(shù)據(jù)庫(kù)中.為了方便實(shí)現(xiàn)可以先將EXCEL導(dǎo)入到GRIDVIEW中然后一次批量插入.實(shí)現(xiàn)代碼如下:

前臺(tái)代碼

<asp:GridView ID="dgBom" runat="server" AutoGenerateColumns="false" CellPadding="1" CellSpacing="2">
<HeaderStyle BackColor="#ededed" />
  <Columns>
   <asp:TemplateField HeaderText="學(xué)號(hào)">
    <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ù)庫(kù)中"/>

后臺(tái)代碼:

//首先在命名空間中加入以下兩行
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ù)組存儲(chǔ) 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ù)存儲(chǔ)到數(shù)組
        storedata[i, j] = ExcelTable.Rows[i][j].ToString();
      }
    int excelBom = 0;//記錄表中有用信息的行數(shù),有用信息是指除去表的標(biāo)題和表的欄目,本例中表的用用信息是從第三行開(kāi)始
    //確定有用的行數(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ù)寫(xiě)入到數(shù)據(jù)庫(kù)中,在此不是問(wèn)題的關(guān)鍵省略 
    }
  }
  protected void btninsert_Click(object sender, EventArgs e)
  {
    foreach (GridViewRow gv in dgBom.Rows) 
    {
      //我的連接字符串是寫(xiě)在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)容就是本文的全部敘述,希望對(duì)大家學(xué)習(xí)C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中有所幫助。

相關(guān)文章

  • C#實(shí)現(xiàn)壓縮pdf文件的示例代碼

    C#實(shí)現(xiàn)壓縮pdf文件的示例代碼

    PDF 文件如果文件太大則會(huì)影響傳輸效果同時(shí)也會(huì)占用過(guò)多磁盤(pán)空間,所以這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)有效地壓縮 PDF 文件,需要的可以參考下
    2023-11-11
  • 聊一聊C#接口問(wèn)題 新手速來(lái)圍觀

    聊一聊C#接口問(wèn)題 新手速來(lái)圍觀

    聊一聊C#接口問(wèn)題,新手速來(lái)圍觀,一個(gè)通俗易懂的例子幫助大家更好的理解C#接口問(wèn)題,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 在C#使用字典存儲(chǔ)事件示例及實(shí)現(xiàn)自定義事件訪問(wèn)器

    在C#使用字典存儲(chǔ)事件示例及實(shí)現(xiàn)自定義事件訪問(wèn)器

    這篇文章主要介紹了在C#使用字典存儲(chǔ)事件示例及實(shí)現(xiàn)自定義事件訪問(wèn)器的方法,是C#事件編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-02-02
  • c#讀取文件詳談

    c#讀取文件詳談

    你平時(shí)是怎么讀取文件的?使用流讀取。是的沒(méi)錯(cuò),C#給我們提供了非常強(qiáng)大的類庫(kù)(又一次吹捧了.NET一番)
    2013-09-09
  • C#判斷某個(gè)軟件是否已安裝實(shí)現(xiàn)代碼分享

    C#判斷某個(gè)軟件是否已安裝實(shí)現(xiàn)代碼分享

    這篇文章主要介紹了C#判斷某個(gè)軟件是否已安裝實(shí)現(xiàn)代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-06-06
  • C# HTTP認(rèn)證方式詳解與代碼實(shí)現(xiàn)

    C# HTTP認(rèn)證方式詳解與代碼實(shí)現(xiàn)

    在C#中,HTTP認(rèn)證是客戶端與服務(wù)器之間進(jìn)行身份驗(yàn)證的一種機(jī)制,常見(jiàn)的HTTP認(rèn)證方式包括:Basic認(rèn)證、Digest認(rèn)證、OAuth、Bearer Token等,下面我們將從工作原理、優(yōu)缺點(diǎn)對(duì)比、代碼實(shí)現(xiàn)、案例實(shí)戰(zhàn)四個(gè)方面詳細(xì)介紹這些認(rèn)證方式,需要的朋友可以參考下
    2025-03-03
  • C#圖像重新著色的方法

    C#圖像重新著色的方法

    這篇文章主要介紹了C#圖像重新著色的方法,涉及C#中SetRemapTable方法替換顏色的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)

    C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)

    這篇文章主要介紹了C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)介紹,非常具有參考借鑒價(jià)值,特此分享到腳本之家平臺(tái)供大家學(xué)習(xí)
    2016-05-05
  • C#設(shè)計(jì)模式之單例模式實(shí)例講解

    C#設(shè)計(jì)模式之單例模式實(shí)例講解

    這篇文章主要介紹了C#設(shè)計(jì)模式之單例模式實(shí)例講解,本文講解了單例模式的定義、單例模式的優(yōu)缺點(diǎn),需要的朋友可以參考下
    2014-10-10
  • 詳解C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用

    詳解C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用

    這篇文章主要介紹了C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用,注意區(qū)分一下簡(jiǎn)單工廠模式、工廠方法模式和抽象工廠模式概念之間的區(qū)別,需要的朋友可以參考下
    2016-02-02

最新評(píng)論

应城市| 鄂托克旗| 浮山县| 长子县| 蕉岭县| 普安县| 台湾省| 清镇市| 翁源县| 布拖县| 布拖县| 金山区| 高密市| 平和县| 肇源县| 张家川| 永平县| 岑溪市| 黑山县| 丘北县| 专栏| 苍溪县| 略阳县| 红原县| 教育| 木兰县| 越西县| 泽库县| 前郭尔| 九台市| 古蔺县| 兴城市| 横峰县| 固镇县| 勃利县| 临邑县| 桐乡市| 交城县| 九龙城区| 酉阳| 晋中市|