【问题标题】:How to populate SQlite columns with list values in c#如何在 C# 中使用列表值填充 SQlite 列
【发布时间】:2013-10-28 17:55:40
【问题描述】:

背景:

这里是新手 - 无论如何,我正在构建一个 c# 表单应用程序,它解析文本文件中的数据。我已将纺织品数据存储在一个列表中。

问题

我不知道如何用这些数据填充一个 sqlite 表。如何将我的列表数据传输到我的 sqlite 表的列中?

SQlite 代码: (以下取自finisar)

     private void button4_Click(object sender, EventArgs e)
    {

        // [snip] - As C# is purely object-oriented the following lines must be put into a class:

        // We use these three SQLite objects:
        SQLiteConnection sqlite_conn;
        SQLiteCommand sqlite_cmd;
        SQLiteDataReader sqlite_datareader;

        // create a new database connection:
        sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");

        // open the connection:
        sqlite_conn.Open();

        // create a new SQL command:
        sqlite_cmd = sqlite_conn.CreateCommand();

        // Let the SQLiteCommand object know our SQL-Query:
        sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(100));";

        // Now lets execute the SQL ;D
        sqlite_cmd.ExecuteNonQuery();

        // Lets insert something into our new table:
        sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";

        // And execute this again ;D
        sqlite_cmd.ExecuteNonQuery();

        // ...and inserting another line:
        sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";

        // And execute this again ;D
        sqlite_cmd.ExecuteNonQuery();

        // But how do we read something out of our table ?
        // First lets build a SQL-Query again:
        sqlite_cmd.CommandText = "SELECT * FROM test";

        // Now the SQLiteCommand object can give us a DataReader-Object:
        sqlite_datareader = sqlite_cmd.ExecuteReader();

        // The SQLiteDataReader allows us to run through the result lines:
        while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
        {
            // Print out the content of the text field:
            string data = sqlite_datareader.GetString(1);
            MessageBox.Show(data);
        }

        // We are ready, now lets cleanup and close our connection:
        sqlite_conn.Close();
    }

所以这是我想将列表数据传输到我的 sqlite 表的地方: sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";

我不知道具体在哪里,也不知道如何在这里实现foreach循环语法。

谢谢

【问题讨论】:

  • 您应该使用参数并重用相同的命令。你应该在哪里使用foreach???不是很明显吗?
  • 正如我所说,这里是菜鸟,所以并不明显。我以为我会使用 foreach 继续将列表值输入到 sql 表中。

标签: c# sqlite list


【解决方案1】:

从上面的代码中,您可能会注意到以下部分重复:

    sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (..., ...);";
    sqlite_cmd.ExecuteNonQuery();

所以,在这里你必须使用循环。

根据你阅读文本的方式,你必须使用适当的循环。

这是一个使用TextReader的例子:

using (StreamReader reader = File.OpenText("somefile.txt")) {
    //Use a transaction: you should do bulk inserts inside transactions
    DbTransaction tr = sqlite_conn.BeginTransaction();
    //Create a command once.
    DbCommand cmd = sqlite_conn.CreateCommand();
    //Assign SQL with parameters (?)
    cmd.CommandText = "INSERT INTO test (id, text) VALUES (?, ?);"
    string line;
    int line_num;
    //Read all lines from file
    for (line_num=0; (line = reader.ReadLine()) != null; ++line_num) {
        //For each line, assign correct parameters
        cmd.Parameters(0).Value=line_num;
        cmd.Parameters(1).Value=line;
        //Send command to database
        cmd.ExecuteNonQuery();
    }
    //Commit - only at this point data is written to database
    tr.Commit();
}

【讨论】:

    猜你喜欢
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    相关资源
    最近更新 更多