如何插入到具有自动递增字段的表中?

编程入门 行业动态 更新时间:2024-10-17 11:21:40
本文介绍了如何插入到具有自动递增字段的表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我有一个采用几个参数的存储过程,我的目标是将这些值插入表中.我的表的自动递增字段为{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')

更多推荐

如何插入到具有自动递增字段的表中?

本文发布于:2023-10-27 10:58:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!