试一试
strSQL = "INSERT INTO tablename (name) VALUES (@name);SELECT @@Identity"
SQLCommand.CommandText = strSQL
Id = SQLCommand.ExecuteScalar()
strSQL = "INSERT INTO tablename (name) VALUES (@name);SELECT @@Identity"
SQLCommand.CommandText = strSQL
Id = SQLCommand.ExecuteScalar()
SELECT @@Identity是返回刚插入的记录的ID.
SqlCommand.CommandText = strCommand;
Integer Id = SqlCommand.ExecuteScalar();
我这么写编译总出错。帮忙看看错在哪,谢咯!
sql 编译错误,还是C#编译错误?
c:\inetpub\wwwroot\infor1\WebForm1.aspx.cs(228): 非静态的字段、方法或属性“System.Data.SqlClient.SqlCommand.CommandText”要求对象引用c:\inetpub\wwwroot\infor1\WebForm1.aspx.cs(229): 找不到类型或命名空间名称“Integer”(是否缺少 using 指令或程序集引用?)
top(1) desc
max(id)
基本insert语句都忘了
INSERT INTO 表名称 VALUES (值1, 值2,....)
首先没有实例化SqlCommand在一个是直接用 int 类型接收值,哪来的Integer类型
insert into 不是 insert infor。
.................
SqlCommand com = new SqlCommand();
com.CommandText = strCommand;
int Id =(int)com.ExecuteScalar();
不建议用@@Identity 建议用SCOPE_IDENTITY http://hi.baidu.com/qxyywy/item/dbcc133fe1914a26b2c0c521 以前上一家公司也出现过类似问题