ado.net,怎么把我刚刚插入的行返回????我的代码如下:
OleDbConnection cn = IConnection.Open();
string sql = "insert into tbUser([Name],[NickName],[Password],[Sex]) values( ";
sql += " ' "+ user.Name + " ', ' " + user.NickName + " ', ' " + user.Password + " ', " + user.Sex + ") ";
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.ExecuteNonQuery();
User u = cmd.ExecuteScalar() as User;
if (u == null)
{
return -1;
}
else
{
return u.Id;
}
但是User u = cmd.ExecuteScalar() as User;没有返回任何东西。
需要关闭后重新打开吗?
------解决方案--------------------executescalar传说中返回第一行第一列。insert成功后返回的貌似不是你插入的对象啊。
你以前这样做过吗?
------解决方案--------------------string sql = "insert into tbUser([Name],[NickName],[Password],[Sex]) values( ";
sql += " ' "+ user.Name + " ', ' " + user.NickName + " ', ' " + user.Password + " ', " + user.Sex + ") ";
sql+= ";select @@identity ";
如果是SQL的话这样可以
------解决方案--------------------string sql= "insert into tbuser(([Name],[NickName],[Password],[Sex]) values(user.name,user.nickname,userpassword,user.sex);select @@IDENTITY as 'identity ' "
------解决方案--------------------ExecuteScalar()返回第一行第一列内容~~~
ExecuteNonQuery()执行insert,update,delete语句,并返回被执行的记录的行数~~~
------解决方案--------------------这个帖子已经不少,楼主搜搜,很多都已经解决.