2016-02-04 66 views
0

我有一種方法返回一個數據表值,我想將它插入到數據庫中的表中我應該怎麼做? 我了Methode:如何使用數據表添加一個.xls到數據庫

public DataTable Import_To_Grid(string FilePath, string Extension) 
{ 
    string conStr = ""; 
    switch (Extension) 
    { 
     case ".xls": //Excel 97-03 
      conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString; 
      break; 
     case ".xlsx": //Excel 07 
      conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString; 
      break; 
    } 
    conStr = String.Format(conStr, FilePath, "YES"); 
    OleDbConnection connExcel = new OleDbConnection(conStr); 
    OleDbCommand cmdExcel = new OleDbCommand(); 
    OleDbDataAdapter oda = new OleDbDataAdapter(); 
    DataTable dt = new DataTable(); 
    cmdExcel.Connection = connExcel; 

    //Get the name of First Sheet 
    connExcel.Open(); 
    DataTable dtExcelSchema; 
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
    string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); 
    connExcel.Close(); 

    //Read Data from First Sheet 
    connExcel.Open(); 
    cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
    oda.SelectCommand = cmdExcel; 
    oda.Fill(dt); 
    connExcel.Close(); 

    return dt; 
} 

我的數據庫

CREATE TYPE udt_MyTable AS TABLE 
(
    [CID]   BIGINT   IDENTITY (1, 1) NOT NULL, 
    [CEmail]  VARCHAR (250) NOT NULL, 
    [UID]   INT   NOT NULL, 
    [CName]  NVARCHAR (450) NULL, 
    [CorpName]  NVARCHAR (350) NULL, 
    [Password]  BINARY (150) NULL) 
CREATE PROCEDURE stp_MyProcedure 
(
    @TVP as dbo.udt_MyTable readonly -- NOTE: table valued parameteres must be readonly 
) 
AS 

INSERT INTO [dbo].[TBLCustomers] 

([CEmail] 
      ,[UID] 
      ,[CName] 
      ,[CorpName] 
      ,[Password]) 
SELECT 
[CEmail] 
      ,[UID] 
      ,[CName] 
      ,[CorpName] 
      ,[Password] 
FROM @TVP 

我的代碼:

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager. 
          ConnectionStrings["ZagmaDBConnectionString"].ConnectionString)) 
      { 
       FileManagement FM = new FileManagement(); 
       string ext = ".xls"; 

       dt = FM.Import_To_Grid(Server.MapPath(FU.FileName), ext); 
       var cmd = new SqlCommand("stp_MyProcedure", conn); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.Add("@TVP", SqlDbType.Structured).Value = dt; 
       cmd.ExecuteNonQuery(); 
      } 

我想知道如何爲工作做此方法插入到TBLCustomers

+0

你的數據表是否有固定的結構(我的意思是,總是有相同的列)?另外,你正在使用哪個版本的sql server? –

+0

1.yes我的表是固定的2.sql-server 2008 R2 – sadeq

回答

0

Sql server 2008推出了一項名爲Table valued parameters. 的功能。想法是,您創建一個user defined table type並將其用作存儲過程的參數,從而允許您將表格結構數據發送到該存儲過程。
.Net框架的ADO.Net完全支持這種參數類型,並且它很容易處理。

所以,這裏是一個非常簡單的例子,就如何做到這一點。

第一個,創建一個與您的數據表結構匹配的UDT。因爲它只是一個演示中,我將介紹一個簡單的表,只有2列:

CREATE TYPE udt_MyTable AS TABLE 
(
    @Id int, 
    @Text varchar(50) 
) 

然後你需要把該類型到一個存儲過程:

CREATE PROCEDURE stp_MyProcedure 
(
    @TVP as dbo.udt_MyTable readonly -- NOTE: table valued parameteres must be readonly 
) 
AS 

INSERT INTO dbo.ActualTable (Id, Text) 
SELECT Id, Text 
FROM @TVP 

GO 

現在你必須要做的是從你的C#代碼執行程序:

using(var con = new SqlConnection(...)) 
{ 
    var cmd = new SqlCommand("stp_MyProcedure", con); 
    cmd.CommandType = CommandType.StoredProcedure 
    cmd.Parameters.Add("@TVP", SqlDbType.Structured).value = dt; 
    cmd.ExecuteNonQuery(); 
} 

Note:表值參數是傳遞給存儲過程的.net數據表,如SqlDbType.Structured

+0

我有錯誤:在System.Data.dll中發生類型'System.IndexOutOfRangeException'的異常,但未在用戶代碼中處理 其他信息:有在這個行的方法0沒有行。'string SheetName = dtExcelSchema.Rows [0] [「TABLE_NAME」]。ToString();' – sadeq

+0

好吧,但是這個異常與我的答案有關嗎? –

+0

我不會低估你的問題 – sadeq

相關問題