2011-06-30 51 views
1

將某個2維數組直接移入數據庫(SQL Server)而無需將其轉換爲SQL語句是否可以?C#&SQL Server:將數組直接移動到SQL Server中

我知道在vb6.0中有某種直接連接,但我不記得它是如何完成的,也不是如果它仍然有可能。

+0

可能重複的[如何通過用戶定義的表型,如C#存儲Procedured參數](http://stackoverflow.com/questions/1030848/how-to-pass-user-defined-table-type -as-stored-procedured-parameter-in-c) – GSerg

+0

我會研究它。但這並不完全是我想到的。 – ralph

+0

嗯,當你的問題可以以多種方式理解時,你就會得到這樣的結果:) – GSerg

回答

0

在VB6中,您可以通過創建記錄集,插入行並調用.Update來避免明確的INSERT語句。如果Command模式爲adCmdTableDirect,這可能是「直接連接」。

在ADO.NET中,您也可以這樣做,但有許多方法,例如Linq To Sql或Entity Framework。如果你想要它簡單的低級香草VB6風格,那可能是DataAdapter.Update method

using (System.Data.SqlClient.SqlConnection c = new System.Data.SqlClient.SqlConnection("Data Source=...")) 
{ 
    using (System.Data.SqlClient.SqlDataAdapter a = new System.Data.SqlClient.SqlDataAdapter("select * from atable;", c)) 
    { 
     using (System.Data.SqlClient.SqlCommandBuilder b = new System.Data.SqlClient.SqlCommandBuilder(a)) 
     { 
      using (DataTable t = new DataTable("atable")) 
      { 
       a.FillSchema(t, SchemaType.Source); 

       t.Rows.Add(1, "foo"); 
       t.Rows.Add(2, "bar"); 

       a.Update(t); 

      } 
     } 
    } 
} 
+0

謝謝,這聽起來很正確! – ralph

+0

@ralph Nah,這是錯誤的。即將編輯。 – GSerg

+0

它確實指向了我正確的方向。你的代碼確實有幫助。 – ralph

相關問題