将数据插入ASP.NET中的SQL Server时出错 [英] Error in insertion data to sql server in asp.net
本文介绍了将数据插入ASP.NET中的SQL Server时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试将记录插入数据库时,我在asp.net中遇到问题
我得到这个错误
Hi i have problem in asp.net when i try to insert record to my db
and i get this error
An explicit value for the identity column in table 'wiki' can only be specified when a column list is used and IDENTITY_INSERT is ON.
我正在寻找解决方案,但我得到了必须写以下内容的信息,但我不知道该在哪里写,或者它是否是正确的解决方法
SET IDENTITY_INSERT Wiki OFF如何在asp.net中编写它
这是我的代码:
and i have search for solution and i get that i must write the follwing,and i don''t know where to write it or if it''s a right solution
SET IDENTITY_INSERT wiki OFF how to write it in asp.net
here is my code:
string qry;
//String file_id=TextBox1.Text;
String file_name = TextBox2.Text;
String file_size = TextBox3.Text;
String file_url = TextBox4.Text;
String file_category = TextBox5.Text;
String file_view = TextBox6.Text;
// String emp_id = TextBox7.Text;
SqlConnection con = new SqlConnection(@"Data Source=SNIPERGHOST-PC\SQLEXPRESS;Initial Catalog=PM;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd;
SqlDataSource sds = new SqlDataSource();
qry = "insert into wiki values('" + file_name + "','" + file_size + "','" + file_url + "','" + file_category + "','" + file_view +"','"+ "')";
cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
//Image1.Visible = true;
string ToRedirectURL = "viewfiles.aspx";
Response.AppendHeader("REFRESH", "5;URL=" + ToRedirectURL);
// Response.Redirect("comment.aspx",true);
推荐答案
首先,您应该使用存储过程来执行此操作.
这是一个解决方案:
Firstly you should be using a stored procedure to do this.
Here''s a solution:
qry = "insert into wiki (column1, column2, column3, column4) values('" + file_name + "','" + file_size + "','" + file_url + "','" + file_category + "','" + file_view +"','"+ "')";
您需要指定数据将进入的列.将列重命名为您实际将数据放入的列.
You need to specify the columns into which the data is going to go. Rename the columns to those that you are actually putting the data into.
这篇关于将数据插入ASP.NET中的SQL Server时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文