-5

我在用着:

string insertQ = "insert into Customer(Name, CNIC, Address, Balance) values(@name, @cnic, @address, @balance); SELECT CAST(scope_identity() AS int);";

SqlCommand insertCmd1 = new SqlCommand(insertQ, con);

Int32 newId = (Int32)insertCmd1.ExecuteScalar();

“必须声明标量变量”

异常来了ExecuteScalar

请告诉我如何解决它...

4

1 回答 1

5

您需要传入参数值:

string insertQ = "insert into Customer(Name, CNIC, Address, Balance) values(@name, @cnic, @address, @balance); SELECT CAST(scope_identity() AS int);";

SqlCommand insertCmd1 = new SqlCommand(insertQ, con); 
insertCmd.Parameters.Add("name", SqlDbType.VarChar).Value = "name";     
insertCmd.Parameters.Add("cnic", SqlDbType.VarChar).Value = "cnic"; 
insertCmd.Parameters.Add("address", SqlDbType.VarChar).Value = "address";     
insertCmd.Parameters.Add("balance", SqlDbType.VarChar).Value = "balance"; 


Int32 newId = (Int32)insertCmd1.ExecuteScalar();

SqlDbTypes 必须与您的方案匹配。

于 2013-08-13T07:36:34.410 回答