主题:(C#)如何调用预编译SQL使它返回刚插入那行的标识列
public void AddUser(users user)
{
sql = "insert into users values(@LoginPwd,@Name)";
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@LoginPwd",user.LoginPwd),
new SqlParameter("@Name",user.Name)
};
dbHalper.ExecuteCommand(sql, para);
}
//users表里面有3列(id,LoginPwd,Name)
public int ExecuteCommand(string sql, params SqlParameter[] values)
{
try
{
OpenConnection();
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
finally
{
CloseConnection();
}
}
问:上面的代码怎么能在插入的同时让他返回标示列???
注:假如不用预编译的SQL用sql="Insert Into users (LoginPwdName,name) values ('" + LoginPwd + "','" + Name + "') SELECT @@IDENTITY"就可以实现
{
sql = "insert into users values(@LoginPwd,@Name)";
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@LoginPwd",user.LoginPwd),
new SqlParameter("@Name",user.Name)
};
dbHalper.ExecuteCommand(sql, para);
}
//users表里面有3列(id,LoginPwd,Name)
public int ExecuteCommand(string sql, params SqlParameter[] values)
{
try
{
OpenConnection();
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
finally
{
CloseConnection();
}
}
问:上面的代码怎么能在插入的同时让他返回标示列???
注:假如不用预编译的SQL用sql="Insert Into users (LoginPwdName,name) values ('" + LoginPwd + "','" + Name + "') SELECT @@IDENTITY"就可以实现