各位大师,请教返回自增值的问题,请大师们指教!
我的后台代码:
public void Save()
{
SqlParameter[] SavePar = new SqlParameter[] { new SqlParameter("@ID", this.id.ToString()), new SqlParameter("@GOODZQMBID", this.GOODZQMBID), new SqlParameter("@goodsid", this.goodsid), new SqlParameter("@mcorder", this.mcorder), new SqlParameter("@zqmbid", this.zqmbid), new SqlParameter("@width", this.width), new SqlParameter("@height", this.height), new SqlParameter("@AddTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) };
DataProvider SaveCon = new DataProvider();;
if (this.id == 0)
{
SaveCon.ExecuteNonQuery("insert into Mc_GoodsZQMB (goodsid,zqmbid,width,height,AddTime,mcorder) values (@goodsid,@zqmbid,@width,@height,@AddTime,@mcorder);select @GOODZQMBID=@@IDENTITY;", SavePar);
GOODZQMBID = ???//把自增值给GOODZQMBID ,我应该如何写?(ExecuteScalar()是无效的,它要求加入string,即ExecuteScalar(string commandtext)这样的格式)
}
我的后台代码:
public void Save()
{
SqlParameter[] SavePar = new SqlParameter[] { new SqlParameter("@ID", this.id.ToString()), new SqlParameter("@GOODZQMBID", this.GOODZQMBID), new SqlParameter("@goodsid", this.goodsid), new SqlParameter("@mcorder", this.mcorder), new SqlParameter("@zqmbid", this.zqmbid), new SqlParameter("@width", this.width), new SqlParameter("@height", this.height), new SqlParameter("@AddTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) };
DataProvider SaveCon = new DataProvider();;
if (this.id == 0)
{
SaveCon.ExecuteNonQuery("insert into Mc_GoodsZQMB (goodsid,zqmbid,width,height,AddTime,mcorder) values (@goodsid,@zqmbid,@width,@height,@AddTime,@mcorder);select @GOODZQMBID=@@IDENTITY;", SavePar);
GOODZQMBID = ???//把自增值给GOODZQMBID ,我应该如何写?(ExecuteScalar()是无效的,它要求加入string,即ExecuteScalar(string commandtext)这样的格式)
}
解决方案 »
- DataList里嵌套一个ImageButton和label ,点击ImageButton如何取label的值?
- 怎么把本机的.bak数据库文件上传到远程服务器?
- 帮忙想想办法啦,怎么实现页面统计功能,以及数据库的设计。
- 正在开发的项目要用ajax,在用户控件中用updatepanel的问题,请大家帮帮忙
- 求助一个智能化的Text.text值
- ASP.NET连接SQL用什么接口好?
- DataGrid的一个问题……标题说不清楚……
- 关于VS2003中排版的问题 ,请问各位高手如何解决?
- 一段delete txt里空白的小代码
- 请教:asp点击数据列表字段链接,弹出div再显示字段内容。
- HiddenField控件后台获取ID.(93,93) 求助
- 求懂asp和asp.net的大神把这段asp帮忙翻译成asp.net的类
SaveCon.ExecuteNonQuery(select @GOODZQMBID=@@IDENTITY);
这样是吗?然后怎么写啊?
GOODZQMBID = ???
如何取得值啊?
无法将object 转化为int
对象不能从 DBNull 转换为其他类型
if(obj !=null)
{
GOODZQMBID = Convert.ToInt32(SaveCon.ExecuteScalar("SELECT @@IDENTITY"));
}
对象不能从 DBNull 转换为其他类型。
源错误: GOODZQMBID = Convert.ToInt32(SaveCon.ExecuteScalar("SELECT @@IDENTITY"));
{
conn.ConnectionString = "server=.;uid=sa;pwd=123456;database=test;";
conn.Open(); SqlCommand cmd = new SqlCommand("insert into test(name) values('hello21')", conn);
cmd.ExecuteNonQuery(); cmd.CommandText = "select @@IDENTITY";
object obj = cmd.ExecuteScalar();
if (obj != null)
{
Console.WriteLine(obj.ToString());
}
}