insert into TCaseContent(aaa)values(1)select @@IDENTITY 在net中怎样接收他返回的值? 不需要存储过程
用SqlCommand.ExecuteScalar就行了,它是执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
Int32 newProdID = 0;
string sql =
"insert into TCaseContent(aaa)values(1); " + "select @@IDENTITY";
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
newProdID = (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
insert into TCaseContent(aaa)values(1);select @@IDENTITY;
直接使用SqlCommand的ExecuteScalar()