帮助我纠正SQL查询 [英] help me in correcting sql query

查看:69
本文介绍了帮助我纠正SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i改进了两位程序员在此处所说的查询..新查询为

i improved the query as said by the two programmers here.. the new query is as

string query = "insert into ESK_Products(CateogoryID,ProductName,ProductImage,UnitCost,Description) values('" + txtproname.Text + "','" + FileUpload1.FileName + "'," + txtproprice.Text + ",'" + txtprodesc.Text + "') select CategoryID from ESK_Categories where CategoryName='" + DropDownList1.Text + "'";



但是现在我又遇到了另一个错误.我认为它是因为categoryid列的原因..但是我该如何解决呢...我们必须在sql值中为category指定一些东西..



but now i am getting another error. i think its becuase of categoryid column.. but how can i solve it...some thing we have to specify in for category in sql values..

not insertedSystem.Data.SqlClient.SqlException: There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Admin_Products.btnAdd_Click(Object sender, EventArgs e)    Label

推荐答案

如果发生什么情况您删除选择部分-从ESK_Categories中选择CategoryID,其中CategoryName =''" + DropDownList1.Text +''"

它会插入数据吗?

我看到您使用的是txtproprice而不是txtproprice.Text.
txtproprice将返回"System.Web.UI.WebControls.TextBox",您可以在错误中看到它.
您可以尝试使用txtproprice.Text吗?对其他文本框(txtproprice和txtprodesc)执行相同的操作
What happens if you remove the select part - "select CategoryID from ESK_Categories where CategoryName=''" + DropDownList1.Text + "''"

Does it insert data then?

I see you have used txtproprice instead of txtproprice.Text.
txtproprice will return "System.Web.UI.WebControls.TextBox" which I can see in your error.
Can you try using txtproprice.Text? Do the same for other text boxes (txtproprice and txtprodesc)


亲爱的朋友,

字符串查询=插入ESK_Products(CategoryID,ProductName,ProductImage,UnitCost,Description)values(""+ txtproname +",""+ FileUpload1.FileName +"''," + txtproprice +' ',''"+ txtprodesc +"'')从ESK_Categories中选择CategoryID,其中CategoryName =''"+ DropDownList1.Text +"'';


用txtproprice替换为txtproprice.text,然后尝试查询


问候,

Anilkumar.
Dear Friend,

string query = "insert into ESK_Products(CategoryID,ProductName,ProductImage,UnitCost,Description) values(''" + txtproname + "'',''" + FileUpload1.FileName + "'',''" + txtproprice + "'',''" + txtprodesc + "'') select CategoryID from ESK_Categories where CategoryName=''" + DropDownList1.Text + "''";


Replace with txtproprice to txtproprice.text then try the Query


Regards,

Anilkumar.


我看到两个错误:
1.可能是拼写错误-
I see two errors:
1. probably typo -
INSERT INTO ESK_Products(CateogoryID...


2.您未在VALUES部分中为CategoryID提供值

在OP提供新信息后,我更改了查询.


2. You don''t supply value for CategoryID in VALUES part

After new information supplied by OP I''ve changed query.

string query = 
"DECLARE @categoryID INT; " +
"SET @categoryID = (SELECT CategoryID FROM ESK_Categories where CategoryName='" + DropDownList1.Text + "'); " +
"INSERT INTO ESK_Products " +
"(CategoryID, ProductName, ProductImage, UnitCost, Description) " +
"VALUES " +
"(@categoryID, '" + txtproname.Text + "', '" + FileUpload1.FileName + "', " + txtproprice.Text + ", '" + txtprodesc.Text + "')";


这篇关于帮助我纠正SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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