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

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

问题描述

我在该查询中得到了错误提示.

我在某处使用该语句

i am getting the fallowing error in that query.

i somewhere use that statement

"INSERT INTO ROLES (userid,groupid) SELECT userid,groupid FROM users, groups WHERE username= '" + TextBox1.Text + "' AND name='" + a + "'";

对我来说很好..

在休闲的声明中.
我希望从另一个表的类别ID中获取类别ID,并根据类别名称进行选择.
其余的值将从文本框中插入.
否如何在insert命令的values字段中传递categoryid.


works fine for me..

in the fallowing statement..
i want category id to be taken from another table categories id and selection is done on the bases of category name.
and the rest of the values are inserted from the textboxes.
no how to pass categoryid in the values field of the insert command.


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 + "'";




下拉列表中的代码:




code form the dropdownlist:

SqlCommand cmd = new SqlCommand("SELECT * FROM ESK_Categories)", con"]));
cmd.Connection.Open();

SqlDataReader dr;
dr = cmd.ExecuteReader();

DropDownList1.DataSource = dr;
DropDownList1.DataValueField = "CateogoryName";

cmd.Connection.Close();



错误如下:-



the error is as under:-

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

推荐答案

5列在这里ESK_Products(CateogoryID,ProductName,ProductImage,UnitCost,Description).但是您在这里values(''" + txtproname.Text + "'',''" + FileUpload1.FileName + "''," + txtproprice.Text + ",''" + txtprodesc.Text + "'')仅使用了4列.列数应在此处匹配.如果CateogoryID是表中的自动增量,则删除CateogoryID,否则也传递CateogoryID 的值.

试试这个.
You have mentioned 5 columns here ESK_Products(CateogoryID,ProductName,ProductImage,UnitCost,Description). But you have used only 4 columns here values(''" + txtproname.Text + "'',''" + FileUpload1.FileName + "''," + txtproprice.Text + ",''" + txtprodesc.Text + "''). Number of columns should match here. If CateogoryID is auto increment in table then remove CateogoryID or else pass the value for CateogoryID also.

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


我会尝试:

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

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


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

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