2009年8月6日 星期四

C sharp or .Net 使用sqlite :2 Login DB and 新增Table

require download System.Data.SQLite 及設定, 可見 C sharp or .Net 使用sqlite 設定

SQLiteCommand 的變數定義自行參考 SQLite.Net 的 Help

簡單的 login and 新增 table function 如下




public void LoginSqliteDatabase()
{

/// Set Database Root
string DatabaseRoot = "c:\\test.db";

/// Set Database Password
string DB_Password = "password";

/// Open Database
/// Version is sqlite version
SQLiteConnection cnn = new SQLiteConnection("Data Source="+ DatabaseRoot+";"+ "Version = 3; Password ="+DB_Password);

/// Open connect
cnn.Open();


/// Define SQLite Command object
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = cnn;

CreatExampleTable(cmd);


}


注意 login 若沒有設定密碼的話,也可以換為以下指令
SQLiteConnection cnn = new SQLiteConnection("Data Source="+ DatabaseRoot+";");




public void CreatExampleTable(SQLiteCommand cmd)
{

try
{
/// Set Creat table command
cmd.CommandText = "create table tbl(one, two)";
/// IF have exist show error message
cmd.ExecuteNonQuery();

}
catch
{
Console.WriteLine("error");
}


}


注意 creat table 指令也可以換為以下格式,定義更多資訊
cmd.CommandText = "CREATE TABLE [tbl] (int, teo nvarchar(20))";

沒有留言: