using
System;
using
System.Data;
using
System.Data.SQLite;
public
class
Program
{
public
static
void
Main(
string
[] args)
{
using
(SQLiteConnection con =
new
SQLiteConnection(
"Data Source=c:\\test.db3;Pooling=true;FailIfMissing=false"
))
{
con.Open();
using
(SQLiteCommand cmd =
new
SQLiteCommand())
{
cmd.Connection = con;
Boolean testTableExists =
false
;
cmd.CommandText =
"SELECT * FROM sqlite_master WHERE type='table' and name='test'"
;
using
(SQLiteDataReader dr = cmd.ExecuteReader())
{
if
(dr.Read())
{
testTableExists =
true
;
}
}
if
(!testTableExists)
{
cmd.CommandText =
"CREATE TABLE [test] (id int, name nvarchar(20))"
;
cmd.ExecuteNonQuery();
}
cmd.CommandText =
"DELETE FROM [test]"
;
cmd.ExecuteNonQuery();
for
(
int
i = 1; i <= 5; i++)
{
cmd.CommandText =
string
.Format(
"INSERT INTO [test] VALUES ({0}, '中文测试')"
, i);
cmd.ExecuteNonQuery();
}
cmd.CommandText =
"SELECT * FROM [test]"
;
using
(SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while
(dr.Read())
{
Console.WriteLine(
"第{0} 条:{1}"
, dr.GetValue(0), dr.GetString(1));
}
}
}
}
Console.WriteLine(
"Press any key to continue..."
);
Console.ReadKey();
}
}
|