【问题标题】:set id primary key from on table to another table as foreign key将表中的 id 主键设置为另一个表作为外键
【发布时间】:2015-08-17 09:08:17
【问题描述】:

我有三张桌子

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();

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 

我现在该怎么办?

【问题讨论】:

    标签: asp.net sql-server ado.net foreign-keys primary-key


    【解决方案1】:

    将您的 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();
    

    【讨论】:

      【解决方案2】:

      您的select 语句不正确,试试这个:

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

      【讨论】:

        【解决方案3】:

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

        SELECT LAST(column_name) FROM table_name
        

        【讨论】:

          猜你喜欢
          • 2013-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-27
          • 2012-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多