如何通过C#将值插入SQL中的主键标识列?

时间:2013-12-03 13:51:46

标签: c# asp.net sql-server ado.net

(我的问题解决了。几乎所有人都说我必须删除@OB_ID并将其留给SQL来分配值。)

我想通过C#代码将新记录插入SQL数据库的表中。主键是身份。如何让Visual Studio了解该列是标识,因此SQL Server必须设置该值。这是相关的代码:

SqlConnection sqlc = new SqlConnection();
sqlc.ConnectionString = "Data Source=. ; Database=LDatabase; Integrated Security=true;";

SqlCommand cmd2 = new SqlCommand("INSERT INTO Order_Book VALUES(@OB_ID, @OB_Title, @OB_Author, @OB_TranslatedBy, @OB_Publisher)", sqlc);//@OB_ID is indentity primary key
            cmd2.Parameters.AddWithValue("@OB_ID", );//What should I assign to it?
            cmd2.Parameters.AddWithValue("@OB_Title", "%" + txtboxbook.Text + "%");
            cmd2.Parameters.AddWithValue("@OB_Author", "%" + txtboxAuthor.Text + "%");
            cmd2.Parameters.AddWithValue("@OB_TranslatedBy", "%" + txtboxTranslator.Text + "%");
            cmd2.Parameters.AddWithValue("@OB_Publisher", "%" + txtboxPublisher.Text + "%");
            sqlc.Open();
            cmd2.ExecuteNonQuery();
            sqlc.Close();

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

您可以忽略IDENTITY列,因为它会插入by default

试试这个

SqlConnection sqlc = new SqlConnection();
sqlc.ConnectionString = "Data Source=. ; Database=LDatabase; Integrated Security=true;";
SqlCommand cmd2 = new SqlCommand("INSERT INTO Order_Book VALUES(@OB_Title, @OB_Author, @OB_TranslatedBy, @OB_Publisher)", sqlc);//@OB_ID is indentity primary key

cmd2.Parameters.AddWithValue("@OB_Title", "%" + txtboxbook.Text + "%");
cmd2.Parameters.AddWithValue("@OB_Author", "%" + txtboxAuthor.Text + "%");
cmd2.Parameters.AddWithValue("@OB_TranslatedBy", "%" + txtboxTranslator.Text + "%");
cmd2.Parameters.AddWithValue("@OB_Publisher", "%" + txtboxPublisher.Text + "%");
sqlc.Open();
cmd2.ExecuteNonQuery();
sqlc.Close();

答案 1 :(得分:0)

您不需要添加任何内容,因为SQL Server将自动增加并分配Identity字段。因此,只需删除@OB_ID,SQL Server就会知道按顺序添加下一个键,所以......

SqlConnection sqlc = new SqlConnection();
sqlc.ConnectionString = "Data Source=. ; Database=LDatabase; Integrated Security=true;";

SqlCommand cmd2 = new SqlCommand("INSERT INTO Order_Book VALUES(@OB_Title, @OB_Author,     @OB_TranslatedBy, @OB_Publisher)", sqlc);
        cmd2.Parameters.AddWithValue("@OB_Title", "%" + txtboxbook.Text + "%");
        cmd2.Parameters.AddWithValue("@OB_Author", "%" + txtboxAuthor.Text + "%");
        cmd2.Parameters.AddWithValue("@OB_TranslatedBy", "%" + txtboxTranslator.Text + "%");
        cmd2.Parameters.AddWithValue("@OB_Publisher", "%" + txtboxPublisher.Text + "%");
        sqlc.Open();
        cmd2.ExecuteNonQuery();
        sqlc.Close();

答案 2 :(得分:0)

您不应将ID放入您的请求中。 SQL Server将在插入时添加良好的标识。

SqlConnection sqlc = new SqlConnection();
sqlc.ConnectionString = "Data Source=. ; Database=LDatabase; Integrated Security=true;";

SqlCommand cmd2 = new SqlCommand("INSERT INTO Order_Book VALUES( @OB_Title,    @OB_Author, @OB_TranslatedBy, @OB_Publisher)", sqlc);//@OB_ID is indentity primary key
        cmd2.Parameters.AddWithValue("@OB_Title", "%" + txtboxbook.Text + "%");
        cmd2.Parameters.AddWithValue("@OB_Author", "%" + txtboxAuthor.Text + "%");
        cmd2.Parameters.AddWithValue("@OB_TranslatedBy", "%" + txtboxTranslator.Text +  "%");
        cmd2.Parameters.AddWithValue("@OB_Publisher", "%" + txtboxPublisher.Text + "%");
        sqlc.Open();
        cmd2.ExecuteNonQuery();
        sqlc.Close();

答案 3 :(得分:0)

替换以下

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


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