简体   繁体   English

当 ID 自动递增时,如何在 Visual Studio 中将其他值插入到数据库中

[英]How to insert other values into database in Visual Studio when there is an ID auto incremented

Userid is auto incremented value and giving default as its value doesn't work Userid是自动递增的值并给出默认值,因为它的值不起作用

string inse = @"insert into registration (userid, firstname, lastname, day, 
                                          month, year, address, city, pincode, 
                                          state, country, phoneno, emailaddress) 
                values  (DEFAULT, @firstname, @lastname, @day, 
                         @month, @year, @address, @city, @pincode,
                         @state, @country, @phoneno, @emailaddress)";

SqlCommand cmd = new SqlCommand(inse, con);
cmd.Parameters.AddWithValue("@firstname", txtfname.Text);
cmd.Parameters.AddWithValue("@lastname", txtlname.Text);
.. 

etc等等

Just don't add it at all in the query, if it's auto-incremented then the database will handle the ID.只是不要在查询中添加它,如果它是自动递增的,那么数据库将处理 ID。

Query that should work:应该工作的查询:

string inse = @"insert into registration(firstname,lastname,day,month,year,
               address,city,pincode,state,country,phoneno,emailaddress) 
               values  
               (@firstname,@lastname,@day,@month,@year,@address,@city,@pincode,
               @state,@country,@phoneno,@emailaddress)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 id键自动递增时如何保存新记录 - How to save a new record when the id key is auto incremented 使用Entity Framework 5在MySQL中将特定ID插入自动递增字段 - Insert a specific id into auto incremented field in MySQL with Entity Framework 5 如何从Visual Studio将图像插入数据库 - How to insert image into database from Visual Studio Visual Studio,如何自动插入参数名称标签? - Visual Studio, how to auto insert parameter name labels? 带有 Prism MVVM 的实体框架:如何将自动递增的 id 获取到 CollectionViewSource? - Entity Framework with Prism MVVM: How to get an auto incremented id to a CollectionViewSource? 如何使用Peta Poco将自动递增的id设置为另一列 - How to set auto incremented id to another column with using Peta Poco 如何从Visual Studio中的数据库获得自动完成功能? - How do I get an auto completion from a database in visual studio ? 在 Visual Studio 中打开解决方案时如何自动运行 dotnet 命令 - How to auto run a dotnet command when opening a solution in visual studio 如何在Visual Studio中的其他项目中使用SQL Server数据库项目? - How to use SQL Server Database Project in other Projects in Visual Studio? 在Oracle DB中获取正确的自动递增ID - Getting correct auto incremented ID in Oracle DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM