关于返回最新ID的问题
private string SaveBase()
{
string rID = null;
using(SqlConnection conn = new SqlConnection(conStr))
{
using(SqlCommand cmd = new SqlCommand( "insert into HouseInfo (H_Tag,H_Address,H_Remark,H_Method) values (@H_Tag,@H_Address,@H_Remark,@H_Method) ",conn))
{
conn.Open();
cmd.Parameters.Add( "@H_Tag ",SqlDbType.VarChar);
cmd.Parameters[ "@H_Tag "].Value = H_Tag;
cmd.Parameters.Add( "@H_Address ",SqlDbType.VarChar);
cmd.Parameters[ "@H_Address "].Value = H_Address;
cmd.Parameters.Add( "@H_Remark ",SqlDbType.VarChar);
cmd.Parameters[ "@H_Remark "].Value = H_Remark;
cmd.Parameters.Add( "@H_Method ",SqlDbType.VarChar);
cmd.Parameters[ "@H_Method "].Value = H_Method;
try
{
cmd.ExecuteNonQuery();
cmd.CommandText = "select SCOPE_IDENTITY() as NID ";
rID = Convert.ToString(cmd.ExecuteScalar());
}
catch
{
return "0 ";
}
finally
{
conn.Close();
}
}
}
return rID;
}
请问各位大侠,为什么每次rID返回都是空呢?
[解决办法]
你的把ID定义为返回类型阿
sqlCmd.Parameters.Add( "@H_id ", SqlDbType.Int).Direction = ParameterDirection.Output
XXX = Fix(sqlCmd.Parameters( "@H_id ").Value)
在把值赋给个变量就OK了
[解决办法]
你已经分两次执行了
应该同一次执行,用分号分开