使用SqlBulkCopy类将数据从SQLite导入SQL Server

时间:2009-08-26 09:58:41

标签: sqlite sqlbulkcopy

我正在尝试将数据从SQLite传输到SQL Server。目标和目标表的模式是相同的:

SQL Server:

CREATE TABLE [dbo].[Shop] (
    [ShopID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](128) NOT NULL,
    [Url] [nvarchar](128) NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [ShopID] ASC
))

和SQLite:

CREATE TABLE "Shop" (
    "ShopID" INTEGER PRIMARY KEY  NOT NULL,
    "Name" VARCHAR NOT NULL,
    "Url" VARCHAR NOT NULL);

我编写了如下代码(使用System.Data.SQLite):

using (var conn = new SQLiteConnection(@"Data Source=Data.sqlite;FailIfMissing=True"))
{ 
    conn.Open();
    var cmd = new SQLiteCommand("SELECT * FROM Shop", conn);
    var reader = cmd.ExecuteReader();

    using (var bulkCopy = new SqlBulkCopy("Data Source=.;Initial Catalog=Test;Integrated Security=True"))
    {
        bulkCopy.DestinationTableName = "Shop";
        bulkCopy.ColumnMappings.Add("ShopID", "ShopID");
        bulkCopy.ColumnMappings.Add("Name", "Name");
        bulkCopy.ColumnMappings.Add("Url", "Url");
        bulkCopy.WriteToServer(reader);
    }
}

数据已由读者加载(我已检查过)。但是在WriteToServer方法上抛出InvalidOperationException:给定的ColumnMapping与源或目标中的任何列都不匹配。

对我有任何想法或建议吗?

2 个答案:

答案 0 :(得分:1)

这可能会或可能不会解决您的问题,但您可能希望使用SqlBulkCopyOptions指定您不希望它生成新的标识值。

SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))

答案 1 :(得分:0)

这对我有用......

private void GatherDb3Info(FileInfo[] fiDb3) {
        SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder();
        foreach (FileInfo fi in fiDb3) {
            csb.Clear();
            csb.DataSource = fi.FullName;
            csb.Password = "P@$$w0rd";
            csb.SyncMode = SynchronizationModes.Full;

            using (var conn = new SQLiteConnection(csb.ToString())) {
                conn.Open();
                DataTable dtTables = conn.GetSchema(SQLiteMetaDataCollectionNames.Tables, new String[] { });
                foreach (DataRow dRow in dtTables.Rows) {
                    if (dRow["Table_Type"].ToString().ToLower() != "table") continue;
                    String
                        catName = String.Format("{0}", dRow["Table_Catalog"]),
                        schName = String.Format("{0}", dRow["Table_Schema"]),
                        tblName = String.Format("{0}", dRow["Table_Name"]);
                    DataTable dtColumns = conn.GetSchema(SQLiteMetaDataCollectionNames.Columns, new System.String[] { catName, schName, tblName });
                    StringBuilder sb = new StringBuilder();
                    foreach (DataRow dRowColumn in dtColumns.Rows) {
                        sb.AppendFormat("[{0}], ", dRowColumn["Column_Name"]);
                    }

                    String sColList = sb.ToString();
                    sColList = sColList.Remove(sColList.Length - 2);
                    var cmd = new SQLiteCommand("Select " + sColList + " From " + tblName, conn);
                    var reader = cmd.ExecuteReader();
                    using (var bulkCopy = new System.Data.SqlClient.SqlBulkCopy(@"Server=.;Integrated Security=true;Database=TargetDBName;")) {
                        bulkCopy.DestinationTableName = "TargetTableSchema." + tblName;
                        try {
                            bulkCopy.WriteToServer(reader);
                        } catch (Exception) { }
                    }
                }
                conn.Close();
            }
        }
    }