简体   繁体   English

将表上的ID主键设置为另一个表作为外键

[英]set id primary key from on table to another table as foreign key

I have three tables 我有三张桌子

Table Customers : Customers

customerid    LastName    FirstName    Phone

Table class : class

classid    numofstud    numofstud    totalprice

Table Orderlist : Orderlist

orderid    customerid (FK_order_customer)    classid (FK_order_class])

I want to set the last customerid and classid from their tables into orderlist table.I use identity increment for them. 我想将他们表中的最后一个customerid和classid设置为orderlist表。我使用身份增量。

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());
SqlCommand cmd = new SqlCommand("insert into customer (firstname,lastname,phone) values (@firstname,@lastname,@phone)", con);
cmd.Parameters.AddWithValue("firstname", firstnametxt.Text);
cmd.Parameters.AddWithValue("lastname", lastnametxt.Text);
cmd.Parameters.AddWithValue("phone", phonetxt.Text);
con.Open();
 cmd.ExecuteNonQuery();
con.Close();

SqlCommand cmd2 = new SqlCommand("insert into class (numofstud,numofclass,totalprice) values (@numofstud,@numofclass,@totalprice)", con);
cmd2.Parameters.AddWithValue("numofclass", Session["Numofclass"]);
cmd2.Parameters.AddWithValue("numofstud", Session["Numofstud"]);
cmd2.Parameters.AddWithValue("totalprice", Session["totalprice"]);
con.Open();
 cmd2.ExecuteNonQuery();
con.Close();

SqlCommand cmd3 = new SqlCommand("insert into orderlist (customerid,classid) values (SELECT MAX(customerid) FROM customer,SELECT MAX(classid) FROM class)", con);

con.Open();
cmd3.ExecuteNonQuery();
con.Close();

cmd and cmd2 are working correctly but cmd3 has an error cmdcmd2正常工作,但cmd3出现错误

ncorrect syntax near the keyword 'SELECT'.
Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near ')'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near ')'.

Source Error: 


Line 48:         con.Open();
Line 49:         cmd3.ExecuteNonQuery();
Line 50:         con.Close();
Line 51: 

Source File: c:\Users\Me.Groot\Documents\Visual Studio 2013\WebSites\miztahrirtest2\account.aspx.cs    Line: 49 

what should I do now? 我现在应该怎么办?

Change your cmd3 command text to: 将您的cmd3命令文本更改为:

insert into orderlist (customerid,classid) SELECT MAX(customerid),MAX(classid) FROM customer, class

or you can use SCOPE_IDENTITY() to get last identity so your code would be: 或者您可以使用SCOPE_IDENTITY()获得最后的身份,因此您的代码将是:

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());
SqlCommand cmd = new SqlCommand("insert into customer (firstname,lastname,phone) values (@firstname,@lastname,@phone);SELECT SCOPE_IDENTITY()", con);
cmd.Parameters.AddWithValue("firstname", firstnametxt.Text);
cmd.Parameters.AddWithValue("lastname", lastnametxt.Text);
cmd.Parameters.AddWithValue("phone", phonetxt.Text);
con.Open();
 Int32 customerID = (Int32) cmd.ExecuteScalar();
con.Close();

SqlCommand cmd2 = new SqlCommand("insert into class (numofstud,numofclass,totalprice) values (@numofstud,@numofclass,@totalprice);SELECT SCOPE_IDENTITY()", con);
cmd2.Parameters.AddWithValue("numofclass", Session["Numofclass"]);
cmd2.Parameters.AddWithValue("numofstud", Session["Numofstud"]);
cmd2.Parameters.AddWithValue("totalprice", Session["totalprice"]);
con.Open();
 Int32 classID = (Int32) cmd2.ExecuteScalar();
con.Close();

SqlCommand cmd3 = new SqlCommand("insert into orderlist (customerid,classid) values (@customerId,@classId)", con);
cmd3.Parameters.AddWithValue("customerId", customerID );
cmd3.Parameters.AddWithValue("classId",classID );

con.Open();
cmd3.ExecuteNonQuery();
con.Close();

Your select statement is incorrect, try this: 您的select语句不正确,请尝试以下操作:

SELECT
    MAX(cust.customerid),
    MAX(cls.classid)
FROM customer cust, class cls

要从表中获取last value ,可以使用last()函数

SELECT LAST(column_name) FROM table_name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从表的主键列中获取值并将其输入到另一个表的外键列中 - Trying to take the values from the primary key column of a table and enter it in the foreign key column of another table 从链接到许多表的另一个表的主键中获取外键字段 - Get foreign key field from primary key in another table that linked to many table 使用asp.net将主键表的单选按钮列表的值获取到另一个外键表 - fetch values of radiobutton list of primary key table to another foreign key table using asp.net 设计数据库,其中表的主键也是另一个表的外键 - Design DB where primary key of table is also foreign key of another table 无法在表格中设置外键 - Cannot set foreign key in the table 插入到两个表中,其中一个表具有自动增量主键,并且同一键用作另一表中的外键 - Insert into two tables with one table having an auto increment primary key and that same key is used as a foreign key in another table 获取最近添加的记录的主键,并作为外键插入到另一个表中 - get primary key of recently added record and insert into another table as foreign key 从SQL插入查询中获取新记录主键ID以将相同值插入另一个表中? - Get the new record primary key ID from SQL insert query to insert same value into another table? 实体框架代码优先设置的外键和同一表中的主键 - Entity Framework code-first set foreign key with primary key in same table 根据主键表的主键获取外键表的行数 - Getting row count of foreign key table's rows on the basis of Primary key table's Primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM