我正在使用一个有两列的表格,customer_id
并且customer_name
.
customer_name
是一个简单的varchar
,customer_id
是一个自动递增的主键。
我想使用我的 C# 应用程序插入customer_name
,并检索customer_id
.
只需插入即可通过使用解决
using (SqlConnection connection = new SqlConnection(AppConstants.ConnectionString))
{
using (SqlCommand command = new SqlCommand("INSERT INTO custom_customer (customer_name) VALUES (@name);"))
{
command.Parameters.Add(new SqlParameter("name", customer));
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
我找到了该语句的一些文档,OUTPUT
可用于检索值。
我认为正确的语法是
INSERT INTO custom_customer (customer_name)
OUTPUT Inserted.customer_id
VALUES (@name);
我想我需要使用
command.ExecuteReader();
但后来我被困住了。我应该如何从查询中获取值?