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("修改失败");