private void Insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"server=(local)\sqlexpress;Integrated Security=True;" + "Database=pubs");
con.Open();
string sql_Insert = "insert into jobs value('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"')";
SqlCommand command = new SqlCommand(sql_Insert, con);
command.ExecuteNonQuery();
MessageBox.Show("插入成功!");
SqlDataAdapter da = new SqlDataAdapter("select * from jobs",con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
}
为什么运行时会提示"仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'jobs'中的标识列指定显式值。"
请高手帮助
{
SqlConnection con = new SqlConnection(@"server=(local)\sqlexpress;Integrated Security=True;" + "Database=pubs");
con.Open();
string sql_Insert = "insert into jobs value('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"')";
SqlCommand command = new SqlCommand(sql_Insert, con);
command.ExecuteNonQuery();
MessageBox.Show("插入成功!");
SqlDataAdapter da = new SqlDataAdapter("select * from jobs",con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
}
为什么运行时会提示"仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'jobs'中的标识列指定显式值。"
请高手帮助
例如:insert into 表名 (列1,列2,...) values(值1,值2,...)
意味着将4个值按顺序插到jobs的字段中,前4个字段中,肯定有一个是IDENTITY,所以就报错。