如何插入到具有自动递增字段的表中? [英] how to insert into a table that has an auto increment field?

查看:92
本文介绍了如何插入到具有自动递增字段的表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个采用几个参数的存储过程,我的目标是将这些值插入表中.我的表的自动递增字段为{int identity(1,1)},由于没有这样的列,因此我尝试插入数据,因为在互联网上有这样的例子.尽管没有错误并且try catch块没有捕获任何内容,但是在程序成功完成后,没有插入数据.为什么???请帮助

这是我的桌子:

Hi all,

I have a stored procedure that takes several parameters and my aim is to insert these values into a table. My table has an auto increment field as {int identity(1,1)}, I tried to insert data as there is no such a column since on internet there were some examples like that. Although there is no error and try catch block does not catch anything, after succesful finishing of program, data is not inserted. Why??? help please

here is my table:

mytable([productcode] [varchar](100) NULL,
	[productid] [varchar](30) NULL,
	[mydate] [smalldatetime] NULL,
	[Id] [int] IDENTITY(1,1) NOT NULL
)



这是我的sp:



here is my sp:

procedure mysp( @procode varchar(100),@pid varchar(30),@mydate smalldatetime)
as begin
insert into mytable(productcode,productid,mydate) values(@procode,@pid,@mydate)
end



这是我的代码:



here is my code:

#region Database
Database db = DatabaseFactory.CreateDatabase("sample.Properties.Settings.mydbConnectionString");
DbCommand cmd = db.GetStoredProcCommand("mysp");
db.AddInParameter(cmd, "@procode", DbType.String, pcode);
db.AddInParameter(cmd, "@pid", DbType.String, pid);
db.AddInParameter(cmd, "@mydate", DbType.DateTime, dt);
#endregion
try{  
   db.ExecuteDataSet(cmd);
}
catch (Exception ex){
   Console.WriteLine(ex.Message);
}




在此先感谢




Thanks in advance

推荐答案

据我所知,自动增量字段可以忽略. 因此,假设您有一个像这样的表:
ID(int)<-增量
名称(varchar(50))

您可以编写如下所示的插入内容:
As far as I know the auto increment field can be ignored.
So lets assume you have a table looking like this:
ID(int) <- increment
Name(varchar(50))

You could write an insert that looks like this:
insert into MyTable
(Name)
values
('Naerling')


这将成功将记录插入到带有Naerling名称的表中. ID会自动增加并添加.
希望有帮助.

在上面的示例中,您甚至不必指定表,因为只有一个(除ID之外)已填写.因此,以下内容也将起作用:


This will successfully insert a record into your table with the Name Naerling. The ID is automatically incremented and added.
Hope that helps.

In the above example you do not even have to specify the tables, since there is only one (besides the ID) which is filled. So the following would also work:

insert into MyTable
values ('Naerling')


这篇关于如何插入到具有自动递增字段的表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆