![](/img/trans.png)
[英]Trying to take the values from the primary key column of a table and enter it in the foreign key column of another table
[英]set id primary key from on table to another table as foreign key
我有三張桌子
表Customers
:
customerid LastName FirstName Phone
表class
:
classid numofstud numofstud totalprice
表Orderlist
:
orderid customerid (FK_order_customer) classid (FK_order_class])
我想將他們表中的最后一個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
和cmd2
正常工作,但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
我現在應該怎么辦?
將您的cmd3命令文本更改為:
insert into orderlist (customerid,classid) SELECT MAX(customerid),MAX(classid) FROM customer, class
或者您可以使用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();
您的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.