2011-03-30 61 views

回答

0

我喜歡使用Microsoft Enterprise Library進行數據庫訪問。一旦將項目庫包含在項目中,並在app.config中定義連接字符串,就可以輕鬆地運行簡單的查詢並將它們映射到列表;

的app.config

<connectionStrings> 
    <add name="Default" connectionString="server=LOCALHOST;database=MyDb; integrated security=SSPI" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

C#

class MyTable 
{ 
    public string Column1 { get; set; } 
    public string Column2 { get; set; } 
} 

var db = DatabaseFactory.CreateDatabase("Default"); 
var genericList = db.ExecuteSqlStringAccessor<MyTable>("select * from mytable").ToList(); 
+0

你不會以'ArrayList'結束,你最終會得到一個'List' – BlackICE 2011-03-30 23:19:13

+0

抱歉,我的錯誤。如果你真的想要'ArrayList',你可以做'var arrayList = new ArrayList(genericList);' – cander 2011-03-30 23:37:01

0
實測值

通過猜測該溶液,並檢查。任何人都可以驗證這個代碼是相對健全的嗎?

SqlConnection con = new SqlConnection(constr); 
     con.Open(); 

     SqlCommand com = new SqlCommand(@"SELECT * FROM compsTickers", con); 

     SqlDataReader reader = com.ExecuteReader(); 

     while (reader.Read()) 
     { 
      tickerList.Add(reader.GetString(0)); 

     } 
     reader.Close(); 
     con.Close(); 
相關問題