[英]Two foreign keys in one table using SQL Server in asp.net webform
I've created BidTable
in which I've used two foreign keys ie ID from Reg
table & CarID
from SubmitAdd
table. 我创建
BidTable
中,我已经用了两个外键即从ID Reg
表& CarID
从SubmitAdd
表。
ID from Reg
table is fetched by comparison of username with current session's username while CarID
is fetched by comparing query string ID
with CarID
. 从ID
Reg
而表是与当前会话的用户名用户名的比较牵强CarID
通过查询字符串比较进账ID
与CarID
。
The problem is when I use both of them in BidTable
as foreign keys, BidTable
can not fetch both of their data. 问题是当我在
BidTable
中将它们BidTable
用作外键时, BidTable
无法获取它们的两个数据。 But when I use them separately, for example I use only ID
from Reg
in BidTable
as a foreign key, it works perfectly. 但是,当我单独使用它们,比如我只用
ID
从Reg
在BidTable
作为外键,它完美的作品。
Here's the code: 这是代码:
string cs = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmdd2 = new SqlCommand("select * from SubmitAdd where CarID='"+ Request.QueryString["id"].ToString() + "'", con);//taking CarID from QueryString
con.Open();
SqlDataAdapter sdda = new SqlDataAdapter(cmdd2);
DataTable dtt = new DataTable();
sdda.Fill(dtt);
SqlCommand cmdd = new SqlCommand("select * from Reg where username='"+ Session["USERNAME"] + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmdd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dtt.Rows.Count != 0 && dt.Rows.Count != 0)
{
int id = Convert.ToInt32(dt.Rows[0][0]); // Get Registration ID from Reg Table
int CarID = Convert.ToInt32(dtt.Rows[0][0]);// Get CarID from SubmitAdd Table
string ss = "insert into BidTable values('" + id + "','" + CarID + "','" + TextBid.Text + "')";
SqlCommand cmd = new SqlCommand(ss, con);
SqlDataAdapter sda2 = new SqlDataAdapter(cmd);
DataTable dt2 = new DataTable();
sda2.Fill(dt2);
}
}
Please check join while fetching records, if you dont have either category id or userid and if you are using inner join then records will be ommited. 在获取记录时,请检查联接,如果您没有类别ID或用户ID,并且如果您使用内部联接,则记录将被省略。 Use left join.
使用左联接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.