将部分数据从一个表复制到另一个表#

时间:2013-02-18 16:32:14

标签: sql-server-2008 c#-3.0

我有下表。我运行此查询以获得分数超过90的学生:

Select Name, Class, Score from Student where Score > 90
Student
Name    Class   Rank    Score
A   1   20  100 
B   1   12  95
C   2   11  89
D   1   14  60
...

现在我想将收集的数据移到另一个名为ExcellentStudent的表中,如下所示:

ExcellentStudent
Name    Class   Score
A   1   100
B   1   95

在c#中有一种简单的方法吗?

2 个答案:

答案 0 :(得分:0)

由于您可以运行SQL语句来获取分数,为什么现在调用C#代码中的另一个SQL来将数据填充到ExcellentStudent表中?

INSERT INTO ExcellentStudent(Name, Class, Score)
Select Name, Class, Score from Student where Score > 90

答案 1 :(得分:0)

用于对db

运行插入查询的C#代码
using System.Data.SqlClient;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using (var objConnection = new SqlConnection("Your Conneciton String"))
            {
                objConnection.Open();
                using (var objCommand = new SqlCommand("INSERT INTO ExcellentStudent (Name, Class, Score) SELECT Name, Class, Score FROM Student WHERE Score > 90", objConnection))
                {
                    objCommand.ExecuteNonQuery();
                }
                objConnection.Close();
            }
        }
    }
}