请高手门来看看,这个问题该怎么解决!
- 提问者网友:龅牙恐龙妹
- 2021-07-31 06:05
第二步 如何用asp.net 代码获取第一步执行后 自动增长列 的值 ?
- 五星知识达人网友:妄饮晩冬酒
- 2021-07-31 07:29
像数据库插入一条数据的sql是
insert into 表明(表中的列1,列2) values(值1,值2)
若想在asp.net中将,此次插入的id列值带出来可以这样写这条sql语句
insert into 表明(表中的列1,列2) values(值1,值2);select @@identity;
将这条语句当做一条语句来执行。就可以了
如下是部分代码(供参考)
public static AccessoryFile AddAccessoryFile(AccessoryFile accessoryFile)
{
string sql =
"INSERT AccessoryFile (FileNo, AccessoryName, AccessorySize, AccessoryType, CreateDate, AccessoryPath)" +
"VALUES (@FileNo, @AccessoryName, @AccessorySize, @AccessoryType, @CreateDate, @AccessoryPath)";
sql += " ; SELECT @@IDENTITY";
try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@FileNo", DBHelper.GetValueByWhetherNull(accessoryFile.FileNo.Id)), //FK
new SqlParameter("@AccessoryName", DBHelper.GetValueByWhetherNull(accessoryFile.AccessoryName)),
new SqlParameter("@AccessorySize", DBHelper.GetValueByWhetherNull(accessoryFile.AccessorySize)),
new SqlParameter("@AccessoryType", DBHelper.GetValueByWhetherNull(accessoryFile.AccessoryType)),
new SqlParameter("@CreateDate", DBHelper.GetValueByWhetherNull(accessoryFile.CreateDate)),
new SqlParameter("@AccessoryPath", DBHelper.GetValueByWhetherNull(accessoryFile.AccessoryPath))
};
int newId = DBHelper.GetScalar(sql, para);
return GetAccessoryFileById(newId);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}
- 1楼网友:独钓一江月
- 2021-07-31 08:46
dao层插入数据后,继续执行查询语句select @@identity from 表名,就可以了。保存这次查询到的值,再返回。