我是linq to sql的新手。我的查询是使用搜索按钮获取另外两行和一个站点号。使用外键的表之间有一个链接。 让我清楚看看的数据库。
您好,
我有四个表tbl_user,tbl_Site,tbl_usersite和tbl_credentials。
表格列
tbl_Site:SiteId,SiteName,SiteNumb
tbl_Credentials:CredId,用户名,密码
tbl_User:UserId,CredIdFK,Forename,Surname
tbl_Usersite:UsersiteID,Site_IdFK,User_IdFk
表关系:
(tbl_Usersite)表是链接表(tbl_User和tbl_Site)
在tbl_user表中,Cred_Id是FK
网站示例数据:
SiteId,SiteName,SiteNumb
凭据示例数据:
CredId,用户名,密码
用户样本数据:
UserId,CredId,Forename,Surname
用户样本数据:
UsersiteId,SiteId,UserId
1,10,187
2,10,196
3,10,195
4,11,197
5,12,198
客户端在textBox中输入SiteNumber,然后单击搜索按钮。他们应该用户名,密码,UserId,ForeName和姓氏。和按钮点击事件中的示例代码是
protected void btnsearch_Click(object sender, EventArgs e)
{
string Site = txtsitenumb.Text;
SiteDataContext sitenumb = new SiteDataContext();
try
{
var siteID = (from sn in sitenumb.tbl_sites
where sn.site_number == Site
select sn.site_id_PK).First();
var UserID = (from sn2 in sitenumb.tbl_usersites
**where sn2.usersite_site_id_FK == siteID
select sn2.usersite_user_id_FK).First();**
// Where the Star Marked area doesnt give the result
if there is an access for more than one Site for the single User
var CredID = (from sn3 in sitenumb.tbl_users
where sn3.user_id_PK == UserID
select sn3.user_credentials_id_FK).First();
var UserName = (from sn4 in sitenumb.tbl_credentials
where sn4.credentials_id_PK == CredID
select sn4.credentials_username).First();
var PassWord = (from sn5 in sitenumb.tbl_credentials
where sn5.credentials_id_PK == CredID
select sn5.credentials_password).First();
var FirstName = (from sn6 in sitenumb.tbl_users
where sn6.user_id_PK == UserId
select sn6.user_forename).First();
var Lastname = (from sn7 in sitenumb.tbl_users
where sn7.user_id_PK == UserId
select sn7.user_surname).First();
fields s = new fields();
s.UserName = UserName;
s.Password = PassWord;
s.UserId = UserID .ToString();
s.FirstName = FirstName;
s.LastName = Lastname;
data.Add(s);
ViewState["idata"] = data;
gridview1.DataSource = data;
gridview1.DataBind();
}
catch (Exception)
{
Label1.Text = "File Not Found";
}
protected void Page_Load(object sender, EventArgs e)
{
this.Form.DefaultButton = this.btnsearch.UniqueID;
data = ViewState["idata"] as List<fields>;
if (data == null)
data = new List<fields>();
}
[Serializable]
public class fields
{
public string UserName { get; set; }
public string Password { get; set; }
public string UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string TimeZone { get; set; }
}
如果单用户是单站点访问,则此代码可以正常工作。请帮我显示所有网站的用户关联。
**例如:**
如果用户输入SiteNumb 1234(Siteid 10)。它必须显示所有三个(UserId:187,196,195)用户详细信息..请尽快回复..
非常感谢
答案 0 :(得分:0)
这可能会有所帮助......
var UserID = (from sn2 in sitenumb.tbl_usersites
where sn2.usersite_site_id_FK == siteID
select sn2.usersite_user_id_FK).First();
//Here you will get only single UserId
var UserIds = (from sn2 in sitenumb.tbl_usersites
where sn2.usersite_site_id_FK == siteID
select sn2.usersite_user_id_FK).ToList()
// Here you will get all the UserIds of that particular site
答案 1 :(得分:0)
当您显示数据库条目时,可能有多个用户在特定网站上拥有访问权限。.First()
返回单行。因此,您应该使用{{1修改代码以获取列表方法。
例如,您可以修改代码以使特定网站ID的所有用户都为: -
ToList()
您可以相应地修改您的代码