我试图使用Sybase安装提供的.net DLL (Sybase.AdoNet4.AseClient.dll版本16.0.02)将数据从Server迁移到Sybase 16.0数据库。
为了保持简单,我尝试从带有一个INT列的表中复制值。
--source table (MSSQL)
CREATE TABLE [dbo].[TO_INTS](
[TO_INT] [int] NULL,
[TO_INT2] [int] NULL,
[NAME] [varchar](50) NULL,
[DT] [datetime] NULL
) ON [PRIMARY]
至
--target table (Sybase)
CREATE TABLE dbo.TO_INTS
(
FROM_INT INT NOT NULL
)
ON 'default'
我使用的代码是:
public void BulkCopyFromSqlServer(string sourceConnectionString, string targetConnectionString)
{
SqlConnection sourceConnection = null;
AseConnection targetConnection = new AseConnection(targetConnectionString);
IDataReader dataSource=null;
try
{
targetConnection.Open();
MssqlCommand.GetDataReader(sourceConnectionString, out sourceConnection, out dataSource); //see below
AseBulkCopy blk = new AseBulkCopy(targetConnection);
blk.BulkCopyTimeout = 1200;
blk.DestinationTableName = "TO_INTS";
blk.ColumnMappings.Clear();
blk.ColumnMappings.Add(new AseBulkCopyColumnMapping(0,0));
blk.WriteToServer(dataSource); // System.ArgumentException thrown here.
blk.Close();
}
catch (AseException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sourceConnection.Dispose();
targetConnection.Dispose();
}
}
//MssqlCommand.GetDataReader(sourceConnectionString, out sourceConnection, out dataSource):
public static void GetDataReader(string sqlServerConnectionString, out SqlConnection conn, out IDataReader reader)
{
conn = new SqlConnection(sqlServerConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from TO_INTS", conn);
cmd.CommandTimeout = 60;
reader = cmd.ExecuteReader();
}
当使用消息“System.ArgumentException”调用WriteToServer()
时,会抛出一个Value does not fall within the expected range
。堆栈跟踪很有趣,因为看起来Sybase DLL无法使用映射中提供的索引解析DB列名称,这似乎很奇怪:
at Sybase.Data.AseClient.AseBulkCopy.GetDBColName(String clientColName, Int32 clientColInx)
at Sybase.Data.AseClient.AseBulkCopy.GenerateInsertCmdByReaderMetaInfo(DataTable rowFmt)
at Sybase.Data.AseClient.AseBulkCopy.WriteToServer(IDataReader reader)
对于Sybase > Sql Server,我遵循了相同的处理过程(几乎是逐行进行的,但使用了相关的DLL切换),这是可行的。
我漏掉了什么明显的东西吗?
发布于 2015-01-14 09:13:56
看来我现在已经修好了。
我的代码中有两个错误。
出现初始错误是因为我不知道如何将EnableBulkLoad
参数放在连接字符串中。我的工作连接字符串如下所示:
string SybaseConnectionString = "Data Source=server1;Initial Catalog=mydb;persist security info=False;User Id=sa;Password=password1;Port=5000;EnableBulkLoad=2"
一旦添加了这一点,就会引发第二个错误:
Bad row data received from the client while bulk copying into object 2080007410 partition 2080007410 in database 6. Received a row of length 11 whilst maximum or expected row length is 6.
之所以出现这种情况,是因为表名是使用以下方法设置的:
blk.DestinationTableName = "TO_INTS";
本来应该是:
blk.DestinationTableName = "dbo.TO_INTS";
一旦我添加了所有者,那么BulkCopy就起作用了。
对于ref,现在我已经开始工作了,我能够在具有不同名称的表之间完成一个WriteToServer调用。此外,每个表(即源Server表)中的列名是唯一的:
CREATE TABLE [dbo].[SOURCE_INTS](
[TO_INT] [int] NULL,
[TO_INT2] [int] NULL,
[NAME] [varchar](50) NULL,
[DT] [datetime] NULL
) ON [PRIMARY]
目标Sybase表:
CREATE TABLE dbo.TO_INTS
(
THE_STRING VARCHAR(50) NOT NULL,
THE_INT INT NOT NULL,
THE_DT DATETIME NOT NULL
)
LOCK ALLPAGES
ON 'default'
GO
您还会注意到顺序不同,但WriteToServer
通过映射处理这一问题:
blk.ColumnMappings.Add(new AseBulkCopyColumnMapping(2, 0)); //string col
blk.ColumnMappings.Add(new AseBulkCopyColumnMapping(1, 1)); //int col
blk.ColumnMappings.Add(new AseBulkCopyColumnMapping(3, 2)); //datetime col
如果您需要有关C#的Sybase数据提供程序的更多信息,请尝试Sybase联机丛书。
希望这能帮上忙。
发布于 2015-01-13 08:46:12
除了这个之外,我没有任何直接的观察: MS中的源表与ASE中的目标表有两种不同之处:
如果不了解这里所发生的事情的全部范围和上下文,就不难想象源表和目标表之间会发生不匹配。
https://stackoverflow.com/questions/27898291
复制相似问题