C#将SQL Server中的数据加载到DataTable中并将其保存到SQlite

时间:2017-03-22 09:40:42

标签: c# sql-server sqlite

我想从我的SQL Server加载一个表,并使用以下代码将其保存到SQLite数据库中:

string connectionString = "Persist Security Info=false" +
                          ";Integrated Security=SSPI" +
                          ";Initial Catalog=tablename" +
                          ";Server=tcp:ipaddress" +
                          ";User ID=username" +
                          ";Password=password";

DataTable dt = new DataTable("ARTIKEL");

// load Data from SQL Server into DataTable
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
    sqlConn.Open();

    try
    {
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select ARTIKEL from ARTIKEL", sqlConn);
        sqlDataAdapter.Fill(dt);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

// Save DatatTable to SQlite
using (SQLiteConnection con = new SQLiteConnection(@"Data Source=c:\tmp\test.db"))
{
    con.Open();

    String sqlCmd = "CREATE TABLE ARTIKEL (ARTIKEL)";

    using (SQLiteCommand command = new SQLiteCommand(sqlCmd, con))
    {
        try
        {
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    } // using

    var sqliteAdapter = new SQLiteDataAdapter("SELECT ARTIKEL FROM ARTIKEL", con);
    var cmdBuilder = new SQLiteCommandBuilder(sqliteAdapter);
    sqliteAdapter.Update(dt);
}

MessageBox.Show("fertig");

将从SQL Server(近250000行)加载数据,并将创建sqlite db和table(带列)。但是没有行会被保存到sqlite数据库中。

我做错了什么?提前致谢

0 个答案:

没有答案