将一个表的顶行插入另一个表 [英] Inserting top row of one table to another
问题描述
Hello Everyone,
我有一个表库存,其中包含以下列[发票编号,日期,产品,[序列号],状态,金额。我有另一个表表1.我想插入表格1的最上面一行表格,其中Product = TextBox.Text。如何帮助。
我的代码如下: -
Hello Everyone,
I have a Table Inventory with following columns[Invoice Number,Date,Product,[Serial Number],Status, Amount .I have another table Table 1. I want to insert the top(1) row of table inventory into table 1 where the Product=TextBox.Text.How to do it kindly Help.
My code is as follows:-
con.Open();
string quer = "insert into Table1([Invoice Number],Date,Product,[Serail Number],Status, Amount) Select Top(1) Inventory where Product='" + TextBox10.Text + "'";
SqlCommand cmdd = new SqlCommand(quer, con);
cmdd.ExecuteNonQuery();
con.Close();<pre lang="c#">
推荐答案
尝试使用
try with
string quer = "insert into Table1([Invoice Number],Date,Product,[Serail Number],Status, Amount) Select Top(1) [Invoice Number],Date,Product,[Serail Number],Status, Amount from Inventory where Product='" + TextBox10.Text + "'";
$您需要b $ b可能需要在sql select语句的第二部分中指定列名和from关键字。 baically语法将是
you need may need to specify the column names and "from" keyword in the second part of the sql select statement. baically syntax would be
-- Option 1:
INSERT INTO table1 (column names....)
SELECT TOP (1) column names....
FROM table2
-- Option 2:
INSERT TOP (1) INTO table1 (column names....)
SELECT column names....
FROM table2
你最好使用参数terized sql语句而不是连接字符串以避免sql注入攻击。
you better use parameterized sql statement rather than concatenating strings to avoid sql injection attacks.
INSERT TOP(1) INTO Table1
SELECT * FROM Inventory where Product = '" + TextBox10.Text + "'"
这篇关于将一个表的顶行插入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!