插入一条新数据,如何返回主键值id,id是自动增长的

wqm20110 2014-08-22 02:53:20
插入一条新数据,如何返回主键值id,id是自动增长的。
具体的代码实现。。。。
...全文
41521 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
changshenglugu 2016-12-19
  • 打赏
  • 举报
回复
引用 20 楼 qxyywy 的回复:
[quote=引用 1 楼 findcaiyzh 的回复:] 试一试

strSQL = "INSERT INTO tablename (name) VALUES (@name);SELECT @@Identity"
SQLCommand.CommandText = strSQL
Id = SQLCommand.ExecuteScalar()
不建议用@@Identity 建议用SCOPE_IDENTITY http://hi.baidu.com/qxyywy/item/dbcc133fe1914a26b2c0c521 以前上一家公司也出现过类似问题[/quote]多谢提醒!SCOPE_IDENTITY()只返回插入到当前作用域中的值,而@@IDENTITY不受限于当前作用域。
qq_35388566 2016-06-23
  • 打赏
  • 举报
回复
引用 1 楼 findcaiyzh 的回复:
试一试

strSQL = "INSERT INTO tablename (name) VALUES (@name);SELECT @@Identity"
SQLCommand.CommandText = strSQL
Id = SQLCommand.ExecuteScalar()
但是现在有一个问题,要是批量上传数据怎么办,就是 strSQL = "INSERT INTO tablename (name) VALUES (@name),(@name),(@name),(@name)"
qxyywy 2014-08-25
  • 打赏
  • 举报
回复
引用 1 楼 findcaiyzh 的回复:
试一试

strSQL = "INSERT INTO tablename (name) VALUES (@name);SELECT @@Identity"
SQLCommand.CommandText = strSQL
Id = SQLCommand.ExecuteScalar()
不建议用@@Identity 建议用SCOPE_IDENTITY http://hi.baidu.com/qxyywy/item/dbcc133fe1914a26b2c0c521 以前上一家公司也出现过类似问题
chen870201 2014-08-24
  • 打赏
  • 举报
回复
SQL Server可以这么弄
wind_cloud2011 2014-08-24
  • 打赏
  • 举报
回复

.................
            SqlCommand com = new SqlCommand();
            com.CommandText = strCommand;
            int Id =(int)com.ExecuteScalar();

qq_19977619 2014-08-23
  • 打赏
  • 举报
回复
帮顶
ZhongGuanYao 2014-08-22
  • 打赏
  • 举报
回复
1楼代码验证通过
mlqxj35674 2014-08-22
  • 打赏
  • 举报
回复
先说说什么数据库吧,不是所有的数据库都支持复合语句的
smthgdin_020 2014-08-22
  • 打赏
  • 举报
回复
引用 4 楼 u012366951 的回复:
string strCommand=" insert infor values('"+TextBox2.Text+" ','"+TextBox3.Text+" ','"+TextBox4.Text+" ','"+TextBox5.Text+" ') ;SELECT @@Identity;"; SqlCommand.CommandText = strCommand; Integer Id = SqlCommand.ExecuteScalar(); 我这么写编译总出错。帮忙看看错在哪,谢咯!
insert into 不是 insert infor。
E次奥 2014-08-22
  • 打赏
  • 举报
回复
首先没有实例化SqlCommand 在一个是直接用 int 类型接收值,哪来的Integer类型
我叫小菜菜 2014-08-22
  • 打赏
  • 举报
回复
引用 11 楼 u010349035 的回复:
[quote=引用 4 楼 u012366951 的回复:] string strCommand=" insert infor values('"+TextBox2.Text+" ','"+TextBox3.Text+" ','"+TextBox4.Text+" ','"+TextBox5.Text+" ') ;SELECT @@Identity;"; SqlCommand.CommandText = strCommand; Integer Id = SqlCommand.ExecuteScalar(); 我这么写编译总出错。帮忙看看错在哪,谢咯!
infor,叼炸天[/quote] 基本insert语句都忘了。。。 INSERT INTO 表名称 VALUES (值1, 值2,....)
-烟花雨季 2014-08-22
  • 打赏
  • 举报
回复
引用 4 楼 u012366951 的回复:
string strCommand=" insert infor values('"+TextBox2.Text+" ','"+TextBox3.Text+" ','"+TextBox4.Text+" ','"+TextBox5.Text+" ') ;SELECT @@Identity;"; SqlCommand.CommandText = strCommand; Integer Id = SqlCommand.ExecuteScalar(); 我这么写编译总出错。帮忙看看错在哪,谢咯!
infor,叼炸天
  • 打赏
  • 举报
回复
string strCommand=" insert into values('"+TextBox2.Text+" ','"+TextBox3.Text+" ','"+TextBox4.Text+" ','"+TextBox5.Text+" ') ;SELECT @@Identity;";
gucangen 2014-08-22
  • 打赏
  • 举报
回复
你那自增加是指SQLSERVER数据库 top(1) desc max(id)
  • 打赏
  • 举报
回复
insert into 表(字段) OUTPUT INSERTED.自增ID values(字段)
wqm20110 2014-08-22
  • 打赏
  • 举报
回复
错误提示: c:\inetpub\wwwroot\infor1\WebForm1.aspx.cs(228): 非静态的字段、方法或属性“System.Data.SqlClient.SqlCommand.CommandText”要求对象引用 c:\inetpub\wwwroot\infor1\WebForm1.aspx.cs(229): 找不到类型或命名空间名称“Integer”(是否缺少 using 指令或程序集引用?)
宝_爸 2014-08-22
  • 打赏
  • 举报
回复
引用 4 楼 u012366951 的回复:
string strCommand=" insert infor values('"+TextBox2.Text+" ','"+TextBox3.Text+" ','"+TextBox4.Text+" ','"+TextBox5.Text+" ') ;SELECT @@Identity;"; SqlCommand.CommandText = strCommand; Integer Id = SqlCommand.ExecuteScalar(); 我这么写编译总出错。帮忙看看错在哪,谢咯!
sql 编译错误,还是C#编译错误?
宝_爸 2014-08-22
  • 打赏
  • 举报
回复
少了into insert into infor
wqm20110 2014-08-22
  • 打赏
  • 举报
回复
string strCommand=" insert infor values('"+TextBox2.Text+" ','"+TextBox3.Text+" ','"+TextBox4.Text+" ','"+TextBox5.Text+" ') ;SELECT @@Identity;"; SqlCommand.CommandText = strCommand; Integer Id = SqlCommand.ExecuteScalar(); 我这么写编译总出错。帮忙看看错在哪,谢咯!
宝_爸 2014-08-22
  • 打赏
  • 举报
回复
自增类型的id不是真能Integer吗。 INSERT INTO tablename (name) VALUES (@name)换成你自己的插入语句。 SELECT @@Identity是返回刚插入的记录的ID.
加载更多回复(2)

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧