Hello! Help solve the problem. Created a project in Visual Studio 2010
. I connected to the MS SQL Server 2008 database (by visual means of the first), but when adding a record to the database tables, the auto-incremental (key) fields do not automatically increase. That is, Visual sends NULL in the key field, but in the database it does not increase automatically, which is why the error occurs. How to solve it? On the Internet did not find.
|
1 answer
Such an error often occurs if PK is not installed (Primary Key) on the table. Check it out.
UPDATE:
Do you explicitly pass null
or not? The problem is most likely that when the field has an Identity
, then there is no need to pass any value for the key field, add only the values ​​for the other fields. For example, a table with fields: ID
(PK, Identity)
, name
. That request should be done like this:
INSERT INTO dbo.MyTable(name) VALUES('value');
- In the table (MS SQL Server) Prima code is, and autoincrement is. - Deniska
- Show better the query that performs the addition to the database. - sharok
- insert into dbo.postavil (recv_post, cont_post) values ​​('value1', 'value2') made in the fields (id, recv_post, cont_post) such an entry (1, value1, value2) to make a request through the means of MS SQL itself, add an entry and auto-increment works, the error when working from the application is just writing in C # language for me is new - Deniska
- The problem is clear. Now show your C # code. - sharok
- That's it, there is no code ... I connect via visual tools. I think if I sent a pure SQL request, I could simply skip the id field in the Insert field and the insert would be implemented. but here I won’t put my mind where I didn’t put a tick. And another question along the way: If you just make a SQL query for fetching data, how can you get fields from the resulting record separately? for example, to calculate the amount of goods at the price per piece and its quantity. - Deniska
|
SQl Server Management Studio
to request isert into tablename (pole1, pole2) values ​​('value1', 2) and see if a new value is being set or not. If not, then the error is NOT in the program code. Then look at the table properties. - Anton Mukhin