如何将值插入visual studio 2008中的sqlserver数据库 [英] how do I insert the values into the sqlserver database in visual studio 2008

查看:76
本文介绍了如何将值插入visual studio 2008中的sqlserver数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,每当我在Visual Studio 2008中尝试将值插入SQLServer数据库时出现错误,我得到了Unhandled Sqlexception,错误是表格中的标识列的显式值 '只能在使用列列表并且IDENTITY_INSERT为ON时指定。。所以请帮助解决我的这个问题。下面是代码



 private void button1_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(Data Source = PS201 \\SQLEXPRESS; Initial Catalog = EmployeeInfo; Integrated Security = True; Pooling =假);
con.Open();
SqlCommand sc = new SqlCommand(插入Emp值(+ textBox1.Text +,'+ textBox2.Text +','+ textBox3.Text +',+ textBox4.Text + ,'+ textBox5.Text +');,con);
int o = sc.ExecuteNonQuery();
MessageBox.Show(o +Insert value successfully ..);
con.Close();


}

public static void main(string [] arg)
{
Application.Run(new Form1());
}

private void button2_Click(object sender,EventArgs e)
{
Application.Exit();
}

解决方案

始终列出要插入或更改的列:如果不这样做SQL假设你的意思是从左开始的当前列顺序 - 你遇到的问题是其中一个结果(另一个更糟糕的是 - 你的数据库在对数据库进行小改动后会无法挽回地损坏这似乎工作正常,直到六个月后出现数据问题。)



基本上,你不能写入标识栏。

因此,请指定您的SQL INSRT:

  INSERT   INTO  Emp(MyColumnName1,MyColumnName2) VALUES (...)



这个问题将会走开。



但是......不要这样做!这非常危险。不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。


先生,请参阅以下链接,它可能对您有所帮助: -



http:/ /stackoverflow.com/questions/2005437/an-explicit-value-for-the-identity-column-in-table-can-only-be-specified-when-a/2005449#2005449 [ ^ ]


实际上您有一个标识列,因此您必须指定列名的其余部分 for 将值插入表。 

例如:

INSERT INTO EMP_TBL(NAME,ADDRESS)VALUES(' + TextBox1.Text +'' + TextBox2.Text +'


Hello everybody, Every time I got the error when I am trying to insert the values into the SQLServer database in visual studio 2008 and I got the Unhandled Sqlexception and the error is "An explicit value for the identity column in table 'Emp' can only be specified when a column list is used and IDENTITY_INSERT is ON.".So please help to solve my this problem.Here's below is code

private void button1_Click(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection("Data Source=PS201\\SQLEXPRESS;Initial Catalog=EmployeeInfo;Integrated Security=True;Pooling=False");
           con.Open();
           SqlCommand sc = new SqlCommand("insert into Emp values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "'," + textBox4.Text + ",'" + textBox5.Text + "');",con);
           int o = sc.ExecuteNonQuery();
           MessageBox.Show(o+"Insert value successfully..");
           con.Close();


       }

       public static void main(string[] arg)
       {
         Application.Run(new Form1());
       }

       private void button2_Click(object sender, EventArgs e)
       {
           Application.Exit();
       }

解决方案

Always list the columns you want to insert or alter: if you don't then SQL assumes you mean in it's current column order starting from the "Left" - and the problem you are meeting is one of the results of that (The other is much worse - your DB gets irretrievably corrupted after a "small change" to the DB which appears to work fine until data problems are seen six months later).

Basically, you can't write to an identity column.
So specify your SQL INSRT like this:

INSERT INTO Emp (MyColumnName1, MyColumnName2) VALUES (...)


And that problem will go away.

But...don't do it the way you are! That is spectacularly dangerous. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


sir, please see the below link it might help you :-

http://stackoverflow.com/questions/2005437/an-explicit-value-for-the-identity-column-in-table-can-only-be-specified-when-a/2005449#2005449[^]


Actually you have one Identity Column so You have to specify rest of  Column Name for Inserting Value into the table.

For Example :

INSERT INTO EMP_TBL (NAME,ADDRESS) VALUES ('"+TextBox1.Text+"','"+TextBox2.Text+"')


这篇关于如何将值插入visual studio 2008中的sqlserver数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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