C# 关于Insert后,返回所添加数据的ID的问题
从网上找了些方法,但ID根本取不出来。//----------DAL----------publicstaticintInsertData(stringstrName,s...
从网上找了些方法,但ID根本取不出来。
//----------DAL----------
public static int InsertData(string strName, string strPass)
{
SqlParameter[] prms = new SqlParameter[2];
prms[0] = new SqlParameter("@A_name", strName);
prms[1] = new SqlParameter("@A_pass", intPass);
return Convert.ToInt32((new SqlHelper()).ExecuteScalar("A_Insert", prms));
}
//----------Sql----------
ALTER procedure A_Insert
@A_name varchar(20),
@A_pass varchar(20),
@A_id int output
AS
insert into [File]
(F_nName,F_oName,F_size)
values
(@F_nName,@F_oName,@F_size)
begin tran
select @F_id = @@identity
commit tran
RETURN
//----------SqlHelper----------
public object ExecuteScalar(string strSql,params SqlParameter[] parameters)
{
conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter pa in parameters)
cmd.Parameters.Add(pa);
conn.Open();
object result = cmd.ExecuteScalar();
conn.Close();
return result;
} 展开
//----------DAL----------
public static int InsertData(string strName, string strPass)
{
SqlParameter[] prms = new SqlParameter[2];
prms[0] = new SqlParameter("@A_name", strName);
prms[1] = new SqlParameter("@A_pass", intPass);
return Convert.ToInt32((new SqlHelper()).ExecuteScalar("A_Insert", prms));
}
//----------Sql----------
ALTER procedure A_Insert
@A_name varchar(20),
@A_pass varchar(20),
@A_id int output
AS
insert into [File]
(F_nName,F_oName,F_size)
values
(@F_nName,@F_oName,@F_size)
begin tran
select @F_id = @@identity
commit tran
RETURN
//----------SqlHelper----------
public object ExecuteScalar(string strSql,params SqlParameter[] parameters)
{
conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter pa in parameters)
cmd.Parameters.Add(pa);
conn.Open();
object result = cmd.ExecuteScalar();
conn.Close();
return result;
} 展开
若以下回答无法解决问题,邀请你更新回答
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询