我已閱讀this不錯的教程,但是當我嘗試連接到SQL Server數據庫時它不起作用。如何查詢SQL Server數據庫?
我只需要做一個簡單的查詢,如SELECT * FROM Table
,我需要處理該查詢的結果值。
這裏是我的代碼,但它不會打印出任何東西到控制檯窗口,我想不通爲什麼:
namespace LinqConsoleApplication
{
[Table(Name = "Central")]
public class Central
{
private string _UserID;
[Column(IsPrimaryKey = true, Storage = "_UserID")]
public string UserID
{
get
{
return this._UserID;
}
set
{
this._UserID = value;
}
}
private string _Email;
[Column(Storage = "_Email")]
public string Email
{
get
{
return this._Email;
}
set
{
this._Email = value;
}
}
} // end of class Central
class Program
{
static void Main(string[] args)
{
// Use a connection string.
DataContext db = new DataContext("Server=aaaaaaa; Database=bbbbbbbbb; uid=ccccccccc; pwd=ddddddddd; Trusted_Connection=True;");
// Get a typed table to run queries.
Table<Central> Centrals = db.GetTable<Central>();
IQueryable<Central> custQuery =
from central in Centrals
select central;
foreach (Central rowin custQuery)
{
Console.WriteLine("Email={0}", row.Email);
}
Console.ReadKey();
}
}
} // end of namespace
表中有多少行? – Backs
這一個更容易:http://www.dotnetperls.com/sqldataadapter – 2016-07-07 14:25:51
這將是更容易解決自己調試它?你是否加入了代碼? – Liam