C# sqliet 读写 *.db 数据库

来源:互联网 发布:水利数据采集监测系统 编辑:程序博客网 时间:2024/06/07 18:51
public static string DbPath = string.Format(@"Data Source={0}", System.Windows.Forms.Application.StartupPath + @"\1.db"); public static DataTable GetDataTable(string sSQL)         {             DataTable dt = null;             try             {                 SQLiteConnection conn = new SQLiteConnection(DbPath);                 SQLiteCommand cmd = new SQLiteCommand();                 cmd.CommandText = sSQL;                 cmd.Connection = conn;                 SQLiteDataAdapter dao = new SQLiteDataAdapter(cmd);                 dt = new DataTable();                 dao.Fill(dt);                 return dt;             }             catch             {                  return null;             }             return null;         } //上面是取datatable的,稍修改一下就可以变成执行语句的 public static int ExecuteSQL(string sSQL)         {             DataTable dt = null;            using(SQLiteConnection conn = new SQLiteConnection(DbPath))             {                 SQLiteCommand cmd = new SQLiteCommand();                 conn.Open();                 cmd.CommandText = sSQL;                 cmd.Connection = conn;                 return cmd.ExecuteNonQuery();             }             catch             {                             }             return 0;         }  
 //调用的时候比如取所有数据 DataTable dt=GetDataTable("select * from tablename"); //修改某人数据 string str="update tablename set sex='{0}',age={1} where uname='{2}'"; str=string.Format(sql,'男',20,'张三'); if(ExecuteSQL(str)>0) MessageBox.Show("修改成功"); else MessageBox.Show("修改失败");  

0 0