在C#中使用where子句插入语句 [英] Insert statement with where clause in C#

查看:51
本文介绍了在C#中使用where子句插入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在子句中使用insert语句..



我有两张桌子

1.公司

2.产品

在公司表中有ID(PK)和公司名称

在产品表中有ProductID(PK),Price,QTy和CompanyID (FK)



当用户从comboBox中选择公司名称并输入ProductID,Price和Qty时,它应保存在特定产品表的表格中。



我尝试过:



插入T1(SR_NO,AH,Model,价格,数量,ID)值('+ textBox1.Text +','+ textBox2.Text +','+ textBox3.Text +','+ textBox4.Text +',' + textBox5.Text +',1其中ID ='1')

How do I use insert Statement with where clause ..

I have two tables
1. Company
2. Product
In Company Table there is ID(PK) and Company Name
In product Table there is ProductID(PK), Price, QTy, and CompanyID(FK)

when user select company name from comboBox and enter ProductID , Price and Qty it should be save in the table of the particular Product Table.

What I have tried:

insert into T1 (SR_NO,AH,Model,Price,Qty,ID) Values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "', '" + textBox5.Text + "',1 where ID = '1')

推荐答案

首先,你的右括号位于错误的位置。



First, your closing parenthesis is in the wrong place.

insert into T1 (SR_NO, AH, Model, Price, Qty, ID) 
Values('" + textBox1.Text + 
       "', '" + textBox2.Text + 
       "', '" + textBox3.Text + 
       "', '" + textBox4.Text + 
       "', '" + textBox5.Text + 
       "', " 1) where ID = 1 





其次,您应该使用参数化查询,这会将您的查询更改为:





Second, you should be using parameterized queries, which would change your query to look like this:

insert into T1 (SR_NO, AH, Model, Price, Qty, ID) 
Values(@srNo, @ah, @model, @price, @qty, @id) where ID = 1 





第三,在insert语句中不需要where子句。你为什么自己分配一个ID?您可以在插入记录时让数据库为您执行此操作。这(以及参数化查询)将使您的查询如下所示:





Third, you don't need a where clause in an insert statement. Why are you assigning an ID yourself? You can make the database do that for you when a record is inserted. This (along with parameterized queries) would make your query look like this:

insert into T1 (SR_NO, AH, Model, Price, Qty) 
Values(@srNo, @ah, @model, @price, @qty)





最后,在编写SQL查询时遵循行业惯例。它通常可以帮助您更好地阅读查询。





Finally, follow industry practice when writing SQL queries. It will generally help you read the query better.

INSERT INTO [database_name].[dbo].[T1] ([SR_NO], [AH], [Model], [Price], [Qty]) 
VALUES (@srNo, @ah, @model, @price, @qty)


如果您要插入,为什么需要产品的Pk?您可以自动增加该列。



然后您的插入语句应如下所示。

If you are inserting, why you need the Pk of Product? You can have that column auto incremented.

Then your insert statement should look like below.
INSERT INTO Product (ColumnNames) VALUES (AllValues) WHERE CompanyId = ComboBoxSelectedCompanyId


它出错。

关键字'where'附近的语法不正确

我的代码是..

string query =Insert into Course(PostNum,CourseName)values('+ TextBox1.Text + ','+ DropDownList1.SelectedItem +')其中StID'+ Label1.Text +';

SqlCommand cmd = new SqlCommand(query,con);
It gives an error.
Incorrect syntax near the keyword 'where'
My code is..
string query= "Insert Into Course(PostNum,CourseName)values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem + "')where StID'"+Label1.Text+"'";
SqlCommand cmd = new SqlCommand(query, con);


这篇关于在C#中使用where子句插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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