一个想法是做这样的事情:
- 在sql lite中查看squema并获取CREATE TABLE命令。
- 在SQL SERVER中执行,解析sql
- 旅行数据为每行创建INSERT语句。 (也解析sql)
这段代码是测试版,因为没有检测类型数据,也没有使用@parameter和命令对象,但是运行。
(您需要插入引用并安装System.Data.SQLite;)
C#:
在head cs中插入此代码(或neccesari)
使用System;
使用System.Collections.Generic;
使用System.Text;
使用System.Data;
使用System.Data.SqlClient;
使用System.Data.SQLite;
使用System.Threading;
使用System.Text.RegularExpressions;
使用System.IO;
使用log4net;
使用System.Net;
public static Boolean SqLite2SqlServer(string sqlitePath, string connStringSqlServer)
{
String SqlInsert;
int i;
try
{
string sql = "select * from sqlite_master where type = 'table' and name like 'YouTable in SQL'";
string password = null;
string sql2run;
string tabla;
string sqliteConnString = CreateSQLiteConnectionString(sqlitePath, password);
//sqliteConnString = "data source=C:\\pro\\testconverter\\Origen\\FACTUNETWEB.DB;page size=4096;useutf16encoding=True";
using (SQLiteConnection sqconn = new SQLiteConnection(sqliteConnString))
{
sqconn.Open();
SQLiteCommand command = new SQLiteCommand(sql, sqconn);
SQLiteDataReader reader = command.ExecuteReader();
SqlConnection conn = new SqlConnection(connStringSqlServer);
conn.Open();
while (reader.Read())
{
//Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
sql2run = "" + reader["sql"];
tabla = "" + reader["name"];
/*
sql2run = "Drop table " + tabla;
SqlCommand cmd = new SqlCommand(sql2run, conn);
cmd.ExecuteNonQuery();
*/
sql2run = sql2run.Replace("COLLATE NOCASE", "");
sql2run = sql2run.Replace(" NUM", " TEXT");
SqlCommand cmd2 = new SqlCommand(sql2run, conn);
cmd2.ExecuteNonQuery();
// insertar los datos.
string sqlCmd = "Select * From " + tabla;
SQLiteCommand cmd = new SQLiteCommand(sqlCmd, sqconn);
SQLiteDataReader rs = cmd.ExecuteReader();
String valor = "";
String Valores = "";
String Campos = "";
String Campo = "";
while (rs.Read())
{
SqlInsert = "INSERT INTO " + tabla;
Campos = "";
Valores = "";
for ( i = 0; i < rs.FieldCount ; i++)
{
//valor = "" + rs.GetString(i);
//valor = "" + rs.GetName(i);
Campo = "" + rs.GetName(i);
valor = "" + rs.GetValue(i);
if (Valores != "")
{
Valores = Valores + ',';
Campos = Campos + ',';
}
Valores = Valores + "'" + valor + "'";
Campos = Campos + Campo;
}
SqlInsert = SqlInsert + "(" + Campos + ") Values (" + Valores + ")";
SqlCommand cmdInsert = new SqlCommand(SqlInsert, conn);
cmdInsert.ExecuteNonQuery();
}
}
}
return true;
} //END TRY
catch (Exception ex)
{
_log.Error("unexpected exception", ex);
throw;
} // catch
}