티스토리 뷰

Programming/.Net

C#에서 SQLite3 연결문

Rusi(루시) 2009. 8. 14. 16:44
반응형
연결하기전에 ADO.NET 2.0 Provider for SQLite을 받아야 한다
http://sourceforge.net/projects/sqlite-dotnet2/files/SQLite%20for%20ADO.NET%202.0/

받고 설치를 하면 using문에서 쓸수 있다.

using
 System.Data.SQLite;
 
// [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:
System.Console.WriteLine( sqlite_datareader["text"] );
}

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

'Programming > .Net' 카테고리의 다른 글

Excel C#연동  (0) 2009.11.04
MS-Project C#에서 제어  (0) 2009.11.04
C# 메인창 닫기 버튼 종료 취소 방법 & 시스탬 종료  (0) 2009.08.19
C#.net MessageBox 활용  (0) 2009.05.26
C# 클립보드에 저장하기  (0) 2009.04.27
공지사항