2012-10-31 26 views
0

我做了一個數據集,並需要使用IN條款,如下面的例子如何在visual c#2010數據集中使用IN子句?

這裏是我的查詢:

select field1,field2 from tablename where id **IN** (@ids) 

ID參數的數量不固定,每一次變化對於上述查詢

現在我想在數據集中使用它。

我不知道如何使用它

我需要在Visual Studio 2010中的數據集的環境中使用IN。

我想知道如何將@ids替換爲1,2,3,4,...?

回答

3

您可以使用LINQ to DataTable

var list = new[] { 1, 2, 3, 4, 5 }; 
var result = dataTable.AsEnumerable() 
         .Where(row => list.Contains(row.Field<int>("Id")); 
+0

這不是爲我好。我需要使查詢做到這一點 我有數以百萬計和更多的數據庫記錄,我不需要所有這些被抓取。 – ahmadii

+1

@AliAhmadi:所以你需要更新你的問題更清晰。 –

+0

我聲明一個拆分函數並將其作爲字符串傳遞並解決 – ahmadii

0

試試這個辦法

 public DataSet GetDataSet(string ConnectionString, string SQL) 
{ 
    SqlConnection conn = new SqlConnection(ConnectionString); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    SqlCommand cmd = conn.CreateCommand(); 
    cmd.CommandText = "YOU SQL QUERY WITH WHERHE"; 
    da.SelectCommand = cmd; 
    DataSet ds = new DataSet(); 

    conn.Open(); 
    da.Fill(ds); 
    conn.Close(); 

    return ds; 
} 
相關問題