sql插入行,但从该行返回一个项目

时间:2016-01-29 14:29:32

标签: c# html sql asp.net razor

使用c#,visual studio,razor html和asp.net,我有一个订单表,在ecustoemr_id中保存一个客户编号,然后自动增加order_id字段,我需要选择行的订单ID我刚刚插入。我不能只从该客户编号中选择所有客户编号,因为客户会有多个订单ID。下面是我如何将客户ID保存到数据库,我需要从刚刚创建的行中的订单ID打印到变量中。 我花了几个小时谷歌搜索,并找到了PHP的答案,但没有使用c#和asp.net 提前谢谢

import os
string = input("Please type the input ") 
directory = "c://files//python" 
for file in os.listdir(directory): 
    if file.endswith(".txt"): 
        filecontent = open(file, "r")
        if string in filecontent.read():
            print("The file that matches the input was found at" + file)

2 个答案:

答案 0 :(得分:0)

SCOPE_IDENTITY的替代方法是OUTPUT。类似的东西:

DECLARE @OrderOutput TABLE (ID INT NOT NULL)
insert into orders (User_ID) 
OUTPUT Inserted.OrderId INTO @OrderOutput(ID)
values ('" + Request["UserID"] + "')"
SELECT ID from @OrderOutput

答案 1 :(得分:0)

尝试这样的事情:

string query = "INSERT INTO [Table] (id) OUTPUT INSERTED.ID VALUES (id value)";
SqlCommand com = new SqlCommand(query, dbconnection);
string value;
value = com.ExecuteScalar().ToString();

请在此处阅读:https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx