Is there a tool to migrate an SQLite database to SQL Server (both the structure and data)?
A idea is do some thing like this: - View squema in sql lite and get the CREATE TABLE command. - Execute, parsing sql, in SQL SERVER - Travel data creating a INSERT statment for each row. (parsing sql too)
This code is beta, because no detect type data, and no use @parameter and command object, but run.
(You need insert reference and install System.Data.SQLite;)
c#: Insert this code (or neccesari) in head cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;
using log4net;
using 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
}