繁体   English   中英

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

[英]Inserting data from one table to another in a database(different servers)

我正在使用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是第一行中的有效数据,不是列名之一。 有任何想法吗 ? **中的代码是我得到错误的地方。

看起来您在此行的末尾有一个不必要的)

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字符串中使用单引号。

您应该始终使用参数化查询 这类字符串连接对SQL注入攻击开放。

除了@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 !! ");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM