简体   繁体   English

在ASP.NET Web表单中使用SQL Server在一个表中使用两个外键

[英]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表& CarIDSubmitAdd表。

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通过查询字符串比较进账IDCarID

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. 但是,当我单独使用它们,比如我只用IDRegBidTable作为外键,它完美的作品。

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.

相关问题 在一个表中引用两个外键-SQL Server C# - Referencing two foreign keys in one table - SQL Server C# 如何将两个表中的两个外键转发到第三个表中? ASP.NET 内核和 C# - How to forward two foreign keys from two tables into third table? ASP.NET Core and C# 在ASP.Net中通过循环在一个Web窗体中使用多个相同的用户控件 - Using multiple same usercontrol in one webform by a loop in ASP.Net 在asp.net样板中自动映射一对一外键 - Auto mapping one to one foreign keys in asp.net boilerplate SQL在Access中运行,但不在ASP.NET Web窗体中运行 - SQL runs in Access but not in ASP.NET webform ASP.NET/SQL-使用外键从多个表中检索数据 - Asp.net/SQL - Retrieving data from multiple tables using foreign keys asp.net mvc-使用实体框架6在第三个表中插入多个具有父级外键的记录 - asp.net mvc - Inserting multiple records in the third table having foreign keys to parent using entity framework 6 使用asp.net在运行时创建SQL Server表 - Create SQL Server table at runtime using asp.net 带有ASP.Net后端的Azure移动应用程序使用外键将数据发布到SQL Server表 - Azure Mobile App with ASP.Net backend posting data to a SQL Server table with a foreign key Asp.net WebForm-在标记中使用RouteUrl - Asp.net WebForm - Using RouteUrl in Markup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM