使用按钮C#将数据复制到sql中的其他表 [英] copy data to other table in sql using button C#

查看:47
本文介绍了使用按钮C#将数据复制到sql中的其他表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我在sql server 2005中有一个数据库
我有一个叫"main_table"的表,它有学生的数据数
我在同一数据库中有另一个具有相同设计的表,它被称为"student_table"

我想要,当我单击c#中的按钮时,来自"main_table"的数据将被复制到"student_table",条件是"where year = 2005(或我指定的任何一个)".

我有更多结果要根据"students_table"进行计算,因此我需要该数据,而不是所有数据

任何人都可以帮忙吗

解决方案

您可以看看使用Select选项插入到其中".

例如

  INSERT   INTO 表1(A,B)
选择 A,B
 FROM 表2
位置 > [条件] 



更多详细信息 http://www.sql-tutorial.com/sql-nested-queries-sql-tutorial/ [
但是,根据喜好,除非确实需要,否则根本不要这样做.[/edit]


您可以使用下面的SQL查询进行插入.

INSERT INTO student_table(Col1,Col2,Col3) SELECT Col1,Col2,Col3 FROM main_table where year= 2005;

并查看下面的Ado.Net数据插入链接.

http://csharp.net-informations.com/data-providers/csharp-sqlcommand- executenonquery.htm [ ^ ]


hi i have a database in sql server 2005
i have one table called "main_table", it has number of data of students
i have another table of same design, in same database, it is called "student_table"

i want, when i click a button in c#, the data from "main_table" would be copy to "student_table" with a condition "where year= 2005 (or any i specify)"

i have further results to calculate from "students_table" so i need that data, not all the data

can anyone help

解决方案

You can have a look at Insert Into with Select option.

e.g.

INSERT INTO Table1 (A, B)
SELECT A,B
FROM Table2
WHERE column > [condition]



More details here[^].


Why create another table, which holds the results? Instead, pass the results of a query to another query (this is called Nested Queries) http://www.sql-tutorial.com/sql-nested-queries-sql-tutorial/[^]

[edit]One of the reasons not to do what you propose is that it won''t work at all in a multiuser environment: Who last put the data into the table? Was it you? Or someone half the world away? You don''t know, and you can''t assume it was you.

If you must put it into a table, then look at Temporary Tables, and do it in a stored procedure.

But by preference, don''t do it at all, unless you really have to.[/edit]


You can use SQL query as below for insertion.

INSERT INTO student_table(Col1,Col2,Col3) SELECT Col1,Col2,Col3 FROM main_table where year= 2005;

And have a look at below link for Ado.Net Data Insertion.

http://csharp.net-informations.com/data-providers/csharp-sqlcommand-executenonquery.htm[^]


这篇关于使用按钮C#将数据复制到sql中的其他表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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