2013-10-28 32 views
0

背景:如何填充在C#中與列表值SQlite的列

菜鳥在這裏 - 不管怎麼說,我要建一個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測試( id,text)VALUES(1,'Test Text 1');「;

我不知道究竟在哪裏,或者如何在這裏實現foreach循環語法。

感謝

+0

您應該使用的參數和重複使用相同的命令。你應該在哪裏使用'foreach'?這不明顯嗎? –

+0

正如我所說的,在這裏點菜,所以它並不明顯。我想我會用foreach繼續向sql表輸入列表值。 – user2788405

回答

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(); 
}