读书人

事务添加返回值的有关问题

发布时间: 2013-01-22 10:23:54 作者: rapoo

事务添加返回值的问题。


StringBuilder strSql = new StringBuilder();
strSql.Append("insert into Photo(");
strSql.Append("FocusImg)");
strSql.Append(" values (");
strSql.Append("@FocusImg)");
strSql.Append(";set @ReturnValue= @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@FocusImg", SqlDbType.NVarChar,250),
new SqlParameter("@ReturnValue",SqlDbType.Int,4)
};
parameters[0].Value = model.FocusImg;
parameters[1].Direction = ParameterDirection.Output;

List<CommandInfo> sqllist = new List<CommandInfo>();
CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
sqllist.Add(cmd);

if (model.PhotoAlbumS != null)
{
StringBuilder strSql2;
foreach (Model.PhotoAlbum models in model.PhotoAlbumS)
{
strSql2 = new StringBuilder();

strSql2.Append("insert into PhotoAlbum(");
strSql2.Append("PhotoID,BigImg)");
strSql2.Append(" values (");
strSql2.Append("@PhotoID,@BigImg)");
SqlParameter[] parameters2 = {
new SqlParameter("@PhotoID", SqlDbType.Int,4),
new SqlParameter("@BigImg", SqlDbType.NVarChar,255)};
parameters2[0].Value = ParameterDirection.InputOutput;


parameters2[1].Value = models.BigImg;
cmd = new CommandInfo(strSql2.ToString(), parameters2);
sqllist.Add(cmd);
}
}

DbHelperSQL.ExecuteSqlTranWithIndentity(sqllist);
return (int)parameters[1].Value;


return (int)parameters[1].Value; 这句都能正确返回刚添加的ID,而插入到PhotoAlbum表中的PhotoID字段值一直为3,并不会写入最新的Photo表ID值,这是什么原因
谢谢
[解决办法]
确实蛮嗦 应该就是这里parameters2[0].Value = ParameterDirection.InputOutput; 鬼晓得你怎么赋值的

读书人网 >asp.net

热点推荐