我正在使用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
字符串中使用单引号。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句