PrepareCommand(command, conn, cmdText, parameters);
055 SQLiteDataAdapter da = new SQLiteDataAdapter(command);
056 da.Fill(ds);
057 return ds;
058 }
059 }
060 }
061 }
062
063 public static int ExecuteNonQuery(string cmdText, List parameters)
064 {
065 lock (lockObject)
066 {
067 using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
068 {
069 using (SQLiteCommand command = new SQLiteCommand())
070 {
071
072 PrepareCommand(command, conn, cmdText, parameters);
073 return command.ExecuteNonQuery();
074 }
075 }
076 }
077 }
078
079 public static SQLiteDataReader ExecuteReader(string cmdText, List parameters)
080 {
081 lock (lockObject)
082 {
083 SQLiteConnection conn = new SQLiteConnection(ConnectionString);
084
085 SQLiteCommand command = new SQLiteCommand();
086
087 PrepareCommand(command, conn, cmdText, parameters);
088 SQLiteDataReader sqLiteDataReader = command.ExecuteReader();
089 return sqLiteDataReader;
090 }
091 }
092
093 public static object ExecuteScalar(string cmdText, List parameters)
094 {
095 lock (lockObject)
096 {
097 using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
098 {
099 using (SQLiteCommand command = new SQLiteCommand())
100 {
101 PrepareCommand(command, conn, cmdText, parameters);
102 return command.ExecuteScalar();
103 }
104 }
105 }
106 }
107 public static void CreateDataBase()
108 {
109 if (!File.Exists(DataBasePath))
110 SQLiteConnection.CreateFile(DataBasePath);
111 CreateTable();
112 }
113
114
115 public static void CreateTable()
116 {
117 ExecuteNonQuery(CodeDetailTabale, null);
118 }
119
120
121 private static string CodeDetailTabale
122 {
123 get
124 {
125 return @"CREATE TABLE [CodeDetail] (
126 [CdType] [nvarchar] (10) NOT NULL ,
127 [CdCode] [nvarchar] (20) NOT NULL ,
128 [CdString1] [ntext] NOT NULL ,
129 [CdString2] [ntext] NOT NULL ,
130 [CdString3] [ntext] NOT NULL,
131 PRIMARY KEY (CdType,CdCode)
132 ) ;";
133 }
134 }
135 }
136 }
示例讲解
A、使用到自己定义的锁private static object lockObject = new object();
B、使用完连接后都进行关闭操作。使用了using
C、创建数据库命令:SQLiteConnection.CreateFile(DataBasePath);
最后再讲解个Insert or Replace into的经典用法
1 Insert or Replace INTO User(ID, Name,Age) Select old.ID,new.Na