1
我想從使用SQL輸出條款數據庫新插入記錄的ID,即獲得一個插入的行的ID
insert into table1(forename, surname) output inserted.id values(@forename, @surname)
,我試圖捕捉到這樣的標識:
Int32 id = (int)command.ExecuteScalar;
這是給我的錯誤信息:
Compiler Error Message: CS0428: Cannot convert method group 'ExecuteScalar' to non-delegate type 'int'. Did you intend to invoke the method?
不知道我做錯了。
這裏是一個大的提取物中的現有代碼:
using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
{
using (command = new SqlCommand("insert into table1(forename, surname) OUTPUT INSERTED.ID values(@forename, @surname)", connection))
{
command.Parameters.Add("@forename", SqlDbType.VarChar, 255).Value = forename;
command.Parameters.Add("@surname", SqlDbType.VarChar, 255).Value = surname;
command.Connection.Open();
id = (int)command.ExecuteScalar;
}
}