本来用存储过程很好实现的,但现在想要直接写sqlServer语句.
这样好像不行:
public string ExecuteInsert(string sqlString)
{
SqlCommand command = new SqlCommand(sqlString,connection);
connection.Open();
int i = command.ExecuteNonQuery();
connection.Close();
SqlDataReader dr;
connection.Open();
string insertsql = "SELECT @@IDENTITY as insertID";
command = new SqlCommand(insertsql,connection);
dr = command.ExecuteReader();
string insertID = "";
if(dr.Read())
{
insertID =dr["insertID"].ToString();
}
connection.Close();
return insertID;
}
{
SqlCommand command = new SqlCommand(sqlString,connection);
connection.Open();
int i = command.ExecuteNonQuery();
connection.Close();
SqlDataReader dr;
connection.Open();
string insertsql = "SELECT @@IDENTITY as insertID";
command = new SqlCommand(insertsql,connection);
dr = command.ExecuteReader();
string insertID = "";
if(dr.Read())
{
insertID =dr["insertID"].ToString();
}
connection.Close();
return insertID;
}
有人建议我用sqlServer的MAX函数查询,不知道这样是否有效?