欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法

 更新時(shí)間:2017年05月13日 10:02:27   作者:aparche  
這篇文章主要介紹了C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#讀取Excel表數(shù)據(jù)及導(dǎo)入Sql Server數(shù)據(jù)庫的具體操作步驟與相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法。分享給大家供大家參考,具體如下:

Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫的方法很多,這里只是介紹了其中一種:

1、首先,我們要先在test數(shù)據(jù)庫中新建一個(gè)my_test表,該表具有三個(gè)字段tid int類型, tname nvarchar類型, tt nvarchar類型
(注意:my_test表中的數(shù)據(jù)類型必須與Excel中相應(yīng)字段的類型一致)

2、 我們用SELECT * FROM  OPENROWSET( 'Microsoft.Jet.OLEDB.4.0 ', 'Excel  5.0;DatabASE=[Excel表.xsl文件的路徑];HDR=YES;IMEX=1 ', Sheet1來讀取Excel表中的數(shù)據(jù),讀出來的數(shù)據(jù)跟從數(shù)據(jù)庫中的表讀出的數(shù)據(jù)是一樣,也包括字段名和數(shù)據(jù)。當(dāng)然我們也可以用字段名列表來獲取 Excel表中的部門數(shù)據(jù)。SELECT 字段1, 字段2,字段3 [...] FROM  OPENROWSET( 'Microsoft.Jet.OLEDB.4.0 ', 'Excel  5.0;DatabASE=[Excel表.xsl文件的路徑];HDR=YES;IMEX=1 ', Sheet1

注意:HDR=Yes,這代表第一行是標(biāo)題,不做為數(shù)據(jù)使用;IMEX ( IMport EXport mode )設(shè)置
  IMEX 有三種模式:
  0 is Export mode
  1 is Import mode
  2 is Linked mode (full update capabilities)
  我這里特別要說明的就是 IMEX 參數(shù)了,因?yàn)椴煌哪J酱碇煌淖x寫行為:
  當(dāng) IMEX=0 時(shí)為“匯出模式”,這個(gè)模式開啟的 Excel 檔案只能用來做“寫入”用途。
  當(dāng) IMEX=1 時(shí)為“匯入模式”,這個(gè)模式開啟的 Excel 檔案只能用來做“讀取”用途。
  當(dāng) IMEX=2 時(shí)為“連結(jié)模式”,這個(gè)模式開啟的 Excel 檔案可同時(shí)支援“讀取”與“寫入”用途。
意義如下:
0 ---輸出模式;
1---輸入模式;
2----鏈接模式(完全更新能力)

3、Excel中第 一行是定義的是列名,從第2行開始才是數(shù)據(jù)。通過Sql語句從Excel中讀取到的數(shù)據(jù)也是從第二行開始的,二列名變成了字段名。如果你的第一行有定義列 名,那么從Excel中獲取的數(shù)據(jù)的各個(gè)字段的名稱就是Excel中的列名。如:從test.xls sheet表中獲取的數(shù)據(jù)的字段名分別是編號(hào) 姓名 備注。如果你定義的Excel表的第一行沒有定義列名,那么獲取后數(shù)據(jù)的字段名分別是F1、F2、F3...以此類推。如果你只是要獲取Excel表中部 分列的數(shù)據(jù),那么你可以就要用到上面的內(nèi)容。

4、在VS中新建一個(gè)web窗體(test.aspx,注:winform窗體也可以),往其中添加一個(gè)Button控件,一點(diǎn)擊該按鈕就執(zhí)行導(dǎo)入。雙擊該按鈕,定義事件處理函數(shù)。test.aspx.cs中的代碼如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_test : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  public SqlConnection con()
  {
    return new SqlConnection("server=localhost;uid=test;pwd=test;database=test");
    //這里的uid=test中的test必須是System Administrtor, 否則會(huì)出錯(cuò)
  }
  protected void Button1_Click1(object sender, EventArgs e)
  {
    SqlConnection mycon = con();
    string sqlstr = "insert into my_test select 編號(hào), 姓名, 備注 from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 5.0;HDR=YES;DATABASE=e:\\test.xls',sheet1$)";
/*這里可以用 * 代替 編號(hào), 姓名, 備注,這些表示excel中的列名  */
    SqlCommand cmd = new SqlCommand(sqlstr, mycon);
    mycon.Open();
    cmd.ExecuteNonQuery();
    mycon.Close();
  }
}

執(zhí)行上面的代碼可能會(huì)出現(xiàn)下面的問題:

SQL Server 阻止了對(duì)組件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的訪問,因?yàn)榇私M件已作為此服務(wù)器安全配置的一部分而被關(guān)閉。系統(tǒng)管理員可以通過使用 sp_configure 啟用 'Ad Hoc Distributed Queries'。有關(guān)啟用 'Ad Hoc Distributed Queries' 的詳細(xì)信息,請參閱 SQL Server 聯(lián)機(jī)叢書中的 "外圍應(yīng)用配置器"。

解決辦法:

/*啟用Ad Hoc Distributed Queries:*/
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
 
/*使用完成后,關(guān)閉Ad Hoc Distributed Queries:*/
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#操作Excel技巧總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論