[英]C# fetch data using Linq to Sql
我是C# Linq
新手。 我運行一個RESTful服務,並為我需要實現的功能,之后,使用該功能,我想打電話給我的Webservice
和WebClient
為好,
我想使用數據庫Student ,請告訴我一些建議或函數代碼,以便我可以從SQL提取數據,
我必須在服務項目中添加LinqtoSQl
文件,或者我可以使用Just Function來實現我的目標。
請幫忙,
我的學生表具有以下列,
ID
FirstName
LastName
Email
DOB
Religion
Studentof
ContactNumber
Address
AdmissionDate
為簡單起見:您可以為以下SQL查詢編寫代碼,
選擇* from Student其中FirstName =“ Myname”;
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "linq/?id={id}")]
string linq(string id);
public string linq(string id)
{
return "This function should return Linq to SQL Result" + id;
}
在解決方案資源管理器上,右鍵單擊您的項目名稱,然后添加>新建項。。在打開的窗口中,選擇創建一個新的LINQ to SQL CLASSES,並命名為DataContext,然后單擊確定。
現在,您應該從剛剛創建的解決方案資源管理器中打開DataContext.dbml。 然后,您必須從數據庫中拖放要使用的表並保存更改。
從這里您可以關注Ali Baghdadi的回答。
或者,如果您想使用select語句來獲取數據,例如:
DataContext DbGet = new DataContext();
var qGet = from q in DbGet.tblStudent where q.ID == this.txtID.Text select Name = q.Name , LastName = q.lastname;
this.Datagridview.DataSource = qGet.ToList;
//也:
foreach (object l in qGet) {
this.txtName.Text = l.Name;
this.txtLastName.text = l.LastName;
}
這是您如何使用Linq到Sql獲取數據
希望有幫助!
using(Var db = new DataContext())
{
Var studentes = db.Students.Where(s => s.FirstName == "MyName").ToList();
}
其中DataContext是放置類的上下文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.