-3

我越来越

“只有在使用列列表并且 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

4 回答 4

4

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

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

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

于 2012-05-31T21:00:27.843 回答
2

正如 Mithrandir 提到的,您没有指定列列表。既然您的身份列是您的第一列,那么您为什么要传入@event_name?这就是您收到错误的原因 - 您正在尝试将标识列的值设置为@event_name,这在多个级别上是不正确的。修复方法是确保您在VALUES.

于 2012-05-31T21:00:34.927 回答
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
)
于 2012-05-31T21:02:02.023 回答
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();
于 2012-05-31T22:03:05.310 回答