How to insert value into primary key identity column in SQL through C#?

前端 未结 4 1143
花落未央
花落未央 2021-01-17 05:46

(My problem solved. As almost everyone said I had to remove @OB_ID and left it to SQL to assign value.)

I want to insert a new record into a table in SQL database th

4条回答
  •  没有蜡笔的小新
    2021-01-17 06:14

    Replace Following

    SqlCommand cmd2 = new SqlCommand("INSERT INTO Order_Book VALUES( @OB_Title,    @OB_Author, @OB_TranslatedBy, @OB_Publisher)", sqlc);
    //@OB_ID is indentity primary key
    


    With

    SqlCommand cmd2 = new SqlCommand("SET IDENTITY_INSERT Order_Book ON;INSERT INTO Order_Book VALUES( @OB_Title,    @OB_Author, @OB_TranslatedBy, @OB_Publisher);SET IDENTITY_INSERT Order_Book OFF;", sqlc);
     //@OB_ID is indentity primary key
    

提交回复
热议问题