身份插入错误被抛出,无论我是否指定身份

时间:2012-05-31 20:52:39

标签: c# sql-server

我正在

  

“只有在使用列列表且IDENTITY_INSERT为ON时,才能指定表中标识列的显式值。”

我是否指定了身份,完全相同的错误。我不想指定身份,我不会将它插入任何我能看到的地方。这是我的(编辑,jcolebrand)“相关”C#代码:

SqlConnection con = new SqlConnection(strcon);
this.getControls(this.Controls);

con.Open();

/* jcolebrand moved the sql query text further down, it's long */

SqlCommand cmd = new SqlCommand(query, con);

cmd = getVals(cmd);

cmd.Parameters.AddWithValue("event_link", DBNull.Value);
cmd.Parameters.AddWithValue("event_approved", DBNull.Value);
cmd.Parameters.AddWithValue("event_in_csi", DBNull.Value);
cmd.Parameters.AddWithValue("event_in_website", DBNull.Value);
cmd.Parameters.AddWithValue("date_added", DateTime.Now.ToString());
cmd.Parameters.AddWithValue("event_start_time1", DBNull.Value);

cmd.Parameters.AddWithValue("event_end_time1", DBNull.Value);

cmd.Parameters.AddWithValue("program", DBNull.Value);
cmd.Parameters.AddWithValue("event_owner", DBNull.Value);


cmd.ExecuteNonQuery();

con.Close(); 

String query = @"INSERT INTO [rec_serv].[dbo].[recserv_event1] (
                      event_name
                    , event_category
                    , event_location
                    , event_instructor
                    , event_start_date
                    , event_end_date
                    , event_start_time
                    , event_end_time
                    , event_deadline
                    , member_fee
                    , non_member_fee
                    , email
                    , registration
                    , minimum_size
                    , maximum_size
                    , waiting_list
                    , event_detail
                    , monday
                    , tuesday
                    , wednesday
                    , thursday
                    , friday
                    , saturday
                    , sunday
                    , number_handout
                    , color_handout
                    , size_handout
                    , number_poster
                    , color_poster
                    , size_poster
                    , listserv
                    , news_release
                    , web_banner
                    , visix_ad
                    , desired_displayed_date
                    , other1
                    , other2
                    , comment
                    , event_link
                    , event_approved
                    , event_in_csi
                    , event_in_website
                    , date_added
                    , extracomment
                    , receiptinfo
                    , program
                    , event_owner
                    , event_start_time1
                    , event_end_time1
                ) VALUES (
                    @event_name, 
                    @event_category, 
                    @event_location, 
                    @event_instructor, 
                    @event_start_date, 
                    @event_end_date, 
                    @event_start_time, 
                    @event_end_time, 
                    @event_deadline, 
                    @member_fee, 
                    @non_member_fee, 
                    @email, 
                    @registration, 
                    @minimum_size, 
                    @maximum_size,
                    @waiting_list, 
                    @event_detail, 
                    @monday, 
                    @tuesday, 
                    @wednesday, 
                    @thursday, 
                    @friday, 
                    @saturday,
                    @sunday, 
                    @number_handout, 
                    @color_handout, 
                    @size_handout, 
                    @number_poster, 
                    @color_poster, 
                    @size_poster, 
                    @listserv, 
                    @news_release, 
                    @web_banner, 
                    @visix_ad, 
                    @desired_displayed_date, 
                    @other1, 
                    @other2, 
                    @comment, 
                    @event_link, 
                    @event_approved, 
                    @event_in_csi,
                    @event_in_website,
                    @date_added,
                    @extracomment,
                    @receiptinfo,
                    @program,
                    @event_owner,
                    @event_start_time1,
                    @event_end_time1
);"

4 个答案:

答案 0 :(得分:4)

您应该尽可能在SQL中使用显式列。因此,对于此查询,它会将查询的形式更改为:

INSERT INTO table (column1, column2, ...) VALUES (value1, value2...);

这一点也很重要,因为它更清楚地定义了应用程序和SQL数据库之间的契约,允许将来进行更改。

答案 1 :(得分:2)

正如Mithrandir所说,你没有指定列表。由于您的标识列是第一列,为什么要传递@event_name?这就是您收到错误的原因 - 您正在尝试将标识列的值设置为@event_name,这在几个级别上是不正确的。修复方法是确保在VALUES之前传递列列表。

答案 2 :(得分:1)

插入设置的字段不是由值确定的,而是由您在查询中指定的字段确定。由于您根本没有指定任何字段,因此默认为所有字段。

指定字段,以便您可以避开标识字段:

INSERT INTO table (
  event_name, event_category, event_in_website, date_added, extracomment,
  recipientinfo, program, event_owner, event_start_time1, event_end_time1
) VALUES (
  @event_name, @event_category, @event_in_website, @date_added, @extracomment,
  @receiptinfo, @program, @event_owner, @event_start_time1, @event_end_time1
)

答案 3 :(得分:0)

在执行查询之前,请执行 “SET IDENTITY_INSERT [dbo]。[recserv_event1] ON”

例如

con.Open(); 
var query =  "SET IDENTITY_INSERT [dbo].[recserv_event1] ON";
SqlCommand cmd = new SqlCommand(query, con); 
cmd.ExecuteNonQuery(); 

query = "Your insert statment.";
cmd.ExecuteNonQuery(); 

con.Close();