如何解决它的错误? [英] how to solve its error?

查看:92
本文介绍了如何解决它的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C#和SQL来使用Insert,delete,update,search ...将值文本框保存到sql的方法?我在Google上搜索了很多代码,并尝试使用与许多网站中相同的代码进行插入,但该代码在cmd.ExecuteNonQuery()上指出了错误;表示未处理SQLEXCEPTION(当IDENTITY_INSERT设置为OFF时,无法在表"book"中为身份列插入显式值.)...您是否可以帮助我解决此错误...我非常需要它.

许多thanx


hi , i need using C# and SQL for using Insert,delete,update,search ... which how to save value textbox into sql ? i searched alot on google and tried by this code which is same as in many website for insert first but it points an error on cmd.ExecuteNonQuery(); which says SQLEXCEPTION was unhandled (Cannot insert explicit value for identity column in table ''book'' when IDENTITY_INSERT is set to OFF.) ... may u help me to solve this error ... i need it very much.

many thanx


private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source =PERSIAN-PC\\SQLEXPRESS;Initial catalog=library;Integrated Security=True");
            
    SqlCommand cmd = new SqlCommand("INSERT INTO book (ide, name, year) VALUES (@a,@b,@c)", con);
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@a", textBox1.Text);
    cmd.Parameters.AddWithValue("@b", textBox2.Text);
    cmd.Parameters.AddWithValue("@c", textBox3.Text);
    con.Open();
    cmd.ExecuteNonQuery(); // here is the error????
    con.Close();
}

推荐答案

(当IDENTITY_INSERT设置为OFF时,无法为表"book"中的身份列插入显式值.)
看来您的ID列已设置为数据库中的IDENTITY列.
2个选项:
1.设置此列以允许通过查询OR
插入值 2.不要通过/尝试插入ID值.当您插入一本新书时,它会自动填充.

标识列通常是主键列,并且每次插入时都设置为自递增-这有助于保持自动生成ID的正确性.我建议遵循选项2,并且完全不要传递ID.让DB自行处理.

做:
(Cannot insert explicit value for identity column in table ''book'' when IDENTITY_INSERT is set to OFF.)
It looks like your ID column is set as IDENTITY column in your database.
2 options:
1. Either set this column to allow having values inserted by query OR
2. Don''t pass/try to insert ID value. It will automatically get filled when you insert a new book.

Identity columns are generally primary key column and are set to self increment with every insert - this helps in maintaining a unqiue automatic ID generation. I would suggest to follow option 2 and don''t pass ID at all. Let DB handle it by itself.

Do:
SqlCommand cmd = new SqlCommand("INSERT INTO book (name, year) VALUES (@b,@c)", con);
cmd.Parameters.AddWithValue("@b", textBox2.Text);
cmd.Parameters.AddWithValue("@c", textBox3.Text);


创建Book表时,已将ide列创建为IDENTIY列.
如果有以下两件事,您可以执行任何操作

1.重新创建没有ide列的identity关键字的书表
2.使用以下语句修改书本表以接受标识列的插入
When you created the Book table you have created the ide column as IDENTIY column.
You can do any if the below two things

1.Recreate the book table without the identity keyword for ide column
2.Modify the book table to accept insert for identity column using the below statement
SET IDENTITY_INSERT book ON


为了将其插入包含标识列的表中,请设置IDENTITY INSERT``''tablename''''On.

http://sqlserverplanet.com/sql-server/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity_insert-is-set-to-off [
In order to insert into a table containing an identity column Set IDENTITY INSERT ''''tablename'''' On.

http://sqlserverplanet.com/sql-server/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity_insert-is-set-to-off[^]


这篇关于如何解决它的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆