代码之家  ›  专栏  ›  技术社区  ›  Geoff Appleford

从sqlite导出到SQL Server

  •  23
  • Geoff Appleford  · 技术社区  · 17 年前

    有迁移工具吗 SQLite 数据库到 SQL Server (结构和数据)?

    5 回复  |  直到 9 年前
        1
  •  26
  •   Peter Mortensen Pieter Jan Bonestroo    13 年前

    SQLite 没有在命令行上运行的.dump选项。虽然我更喜欢用 SQLite Database Browser 用于管理sqlite数据库的应用程序。您可以将结构和内容导出到.sql文件中,该文件几乎可以被任何内容读取。文件>将数据库导出到SQL文件。

        2
  •  7
  •   Peter Mortensen Pieter Jan Bonestroo    13 年前

    SQLite .dump 命令将把数据库的全部内容输出为一个ASCII文本文件。此文件采用标准SQL格式,因此可以导入到任何SQL数据库中。 有关此页的详细信息: sqlite3

        3
  •  7
  •   Community Mohan Dere    9 年前

    我知道这是老线索,但我认为这个解决方案也应该在这里。

    • 为sqlite安装ODBC驱动程序
    • 为x64运行odbcad32或为x86运行c:\windows\sysww64\odbcad32.exe
    • 创建系统DSN,在其中选择SQLITE3 ODBC驱动程序
    • 然后填写数据库名称为filepath到sqlite数据库的表单

    然后在SQL Server中运行sysadmin

    USE [master]
    GO
    EXEC sp_addlinkedserver 
       @server     = 'OldSQLite', -- connection name
       @srvproduct = '',          -- Can be blank but not NULL
       @provider   = 'MSDASQL', 
       @datasrc    = 'SQLiteDNSName' -- name of the system DSN connection 
    GO
    

    然后您可以作为普通用户运行查询 例如

    SELECT * INTO SQLServerDATA FROM openquery(SQLiteDNSName, 'select * from SQLiteData')
    

    或者你可以用 this 对于较大的桌子。

        4
  •  3
  •   Community Mohan Dere    9 年前

    SQLite管理器 ,firefox插件:允许您以SQL脚本导出一个sqlite数据库。

    数据库导出数据库导出到文件

    (修正firefox 35 bugg必须修正扩展代码,如下网页所示: How to fix your optional sqlite manager module to work )

    命令行 :

    sqlite3 DB_name .dump > DB_name.sql
    

    以SQL脚本导出SQLite数据库。

    从URL: http://doc.ubuntu-fr.org/sqlite .

        5
  •  0
  •   R.Alonso    9 年前

    一个想法是这样做: -在SQL Lite中查看squama并获取create table命令。 -在SQL Server中执行、分析SQL -移动数据为每行创建一个插入语句。(也在分析SQL)

    此代码是beta,因为没有检测类型数据,也没有使用@parameter和command对象,而是运行。

    (需要插入引用并安装System.Data.sqlite;)

    C: 在头CS中插入此代码(或neccesari)

    使用系统;

    使用system.collections.generic;

    使用system.text;

    使用系统数据;

    使用system.data.sqlclient;

    使用system.data.sqlite;

    使用系统,穿线;

    使用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
        }