在数据库中将数据从一个表插入另一个表(不同的服务器)

时间:2014-03-17 12:51:24

标签: c# sql

我正在使用c#(newbee)来打开服务器中一个数据库的连接,并将其迁移到另一个服务器中的另一个数据库。这听起来很容易,但我得到这个奇怪的错误。有什么帮助吗?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection thisConnection = new SqlConnection(@"SERVER=fisymsql02.i.ftvac;DATABASE=fcpdp_new;UID=;PWD=");
            thisConnection.Open();
            SqlCommand thisCommand = thisConnection.CreateCommand();
            thisCommand.CommandText = "select [curfrom],[curto],[rateDate],[rate] from [fcpdp_new].[dbo].[latestxrates]";
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            SqlConnection thisConnection1 = new SqlConnection(@"SERVER=SBKFISDDB1;DATABASE=UniversalTool;UID=;PWD=");
            thisConnection1.Open();
            SqlCommand thisCommand1 = thisConnection1.CreateCommand();

            while (thisReader.Read())
            {
                Console.WriteLine("\t{0}\t{1}", thisReader["curfrom"], thisReader["rate"]);
                thisCommand1.CommandText = " BEGIN TRY INSERT INTO [dbo].[CurrencyConversionSource]([ToCurrency] ,[FromCurrency] ,[DateTimeStamp] ,[FISClose]) VALUES ("+thisReader["curfrom"] +"," + thisReader["curto"]+","+thisReader["rateDate"]+"," + thisReader["rate"] +") END TRY BEGIN CATCH PRINT 'Error: Same set of primary key; row skipped' END CATCH)";
                using(SqlCommand cmd = new SqlCommand(thisCommand1.CommandText, thisConnection1))
                {  
                    **thisCommand1.ExecuteNonQuery();**
                    Console.WriteLine("Row inserted !! ");
                }
                Console.ReadKey();
            }

            thisConnection.Close();
        }
    }
}

执行此操作时出现以下错误:

"名称" GBP"在这种情况下是不允许的。有效表达式是常量,常量表达式和(在某些上下文中)变量。不允许使用列名。

  

')附近的语法错误。'。"

GBP是来自第一行的有效数据,而不是其中一个列名。有任何想法吗 ? **中的代码是我收到错误的地方。

2 个答案:

答案 0 :(得分:1)

看起来你在这一行的末尾有一个不必要的);

thisCommand1.CommandText = @" BEGIN TRY INSERT INTO [dbo].[CurrencyConversionSource]([ToCurrency] ,[FromCurrency] ,[DateTimeStamp] ,[FISClose])
                             VALUES ('"+thisReader["curfrom"] +"','" + thisReader["curto"]+"','"+thisReader["rateDate"]+"','" + thisReader["rate"] +"')
                             END TRY BEGIN CATCH PRINT 'Error: Same set of primary key;
                             row skipped' END CATCH)";
                                                  ^^^^ here

删除它。

此外,您应该使用带有VALUES字符串的单引号。

顺便说一下,您应该始终使用parameterized queries。这种字符串连接对SQL Injection攻击开放。

答案 1 :(得分:1)

除了@Soner的回答,你需要用单引号括起字符串值,否则它将被识别为列名而不是字符串值,例如:

thisCommand1.CommandText = 
            "INSERT INTO [dbo].[CurrencyConversionSource]([ToCurrency]) VALUES ('"
                +thisReader["curfrom"] +"')" ;

此外,使用更好的方法,参数化查询,而不是在查询字符串中连接值。简化示例:

thisCommand1.CommandText = 
            "INSERT INTO [dbo].[CurrencyConversionSource]([ToCurrency]) VALUES (@ToCurrency)" ;
using(SqlCommand cmd = new SqlCommand(thisCommand1.CommandText, thisConnection1))
{
    cmd.Parameters.AddWithValue("@ToCurrency", thisReader["curfrom"]);
    thisCommand1.ExecuteNonQuery();
    Console.WriteLine("Row inserted !! ");
}
相关问题