SQLite C#实现操作
debug command
C:\1.db
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SQLite; using System.IO; namespace CSharpConnectionSQLLite { class Dao { /// <summary> /// /// </summary> /// <param name="dbDirectory"></param> /// <returns></returns> public SQLiteConnection GetConnection(string dbDirectory) { if (!File.Exists(dbDirectory)) { SQLiteConnection.CreateFile(dbDirectory); } SQLiteConnectionStringBuilder connStr = new SQLiteConnectionStringBuilder(); connStr.DataSource = dbDirectory; connStr.Pooling = true; connStr.FailIfMissing = true; SQLiteConnection conn = new SQLiteConnection(connStr.ToString()); return conn; } /// <summary> /// /// </summary> /// <param name="conn"></param> public void CloseConnection(SQLiteConnection conn) { if(conn!= null) { conn.Close(); } } /// <summary> /// /// </summary> /// <param name="createTableSQL"></param> /// <param name="conn"></param> public void CreateTable(string createTableSQL,SQLiteConnection conn) { SQLiteCommand cmd = new SQLiteCommand(); cmd.CommandText = createTableSQL; cmd.Connection = conn; cmd.ExecuteNonQuery(); } /// <summary> /// /// </summary> /// <param name="insertSQL"></param> /// <param name="conn"></param> public void InsertIntoValue(string insertSQL , SQLiteConnection conn) { SQLiteCommand cmd = new SQLiteCommand(); cmd.CommandText = insertSQL; cmd.Connection = conn; cmd.ExecuteNonQuery(); } /// <summary> /// /// </summary> /// <param name="querySQL"></param> /// <param name="conn"></param> /// <returns></returns> public SQLiteDataReader GetTableValue(string querySQL, SQLiteConnection conn) { SQLiteCommand cmd = new SQLiteCommand(); cmd.CommandText = querySQL; cmd.Connection = conn; SQLiteDataReader reader = cmd.ExecuteReader(); return reader; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SQLite; namespace CSharpConnectionSQLLite { class Program { private static SQLiteConnection conn = null; private static string sql = "CREATE TABLE test(username varchar(20),password varchar(20))"; private static string insertintosql = "INSERT INTO test VALUES('myuser','mypassword')"; private static string querySql = "SELECT * FROM test"; static void Main(string[] args) { Dao dao = new Dao(); try { conn = dao.GetConnection(args[0]); conn.Open(); conn.CreateCommand(); Console.WriteLine("Success"); //dao.CreateTable(sql, conn); dao.InsertIntoValue(insertintosql,conn); SQLiteDataReader reader = dao.GetTableValue(querySql, conn); while(reader.Read()) { Console.Write(reader.GetString(0)); Console.Write("\t"); Console.WriteLine(reader.GetString(1)); } } catch(Exception) { Console.WriteLine("Failed"); } finally { dao.CloseConnection(conn); } Console.ReadKey(); } } }
相关推荐
specialbrian 2020-07-31
loveandroid0 2020-06-18
DAV数据库 2020-06-17
URML 2020-06-13
Dlanguage 2020-06-12
Plant 2020-06-07
疯狂老司机 2020-06-07
chibangyuxun 2020-06-07
sdwylry 2020-06-04
airfling 2020-05-31
Plant 2020-05-31
zbcaicai 2020-05-26
chaochao 2020-05-19
Plant 2020-05-17
小火车 2020-05-14
beibeijia 2020-04-25
Rain 2020-04-16