SQLite带参数处理方法
/// <summary> /// 执行语法[新增 修改 删除] /// </summary> /// <param name="sqlText">sql语法</param> /// <returns></returns> public async Task<int> ExecuteNoneQuery(string sqlText, SQLiteParameter[] spr) { SQLiteConnection cn = new SQLiteConnection(_datasource); //连接数据库 cn.ConnectionString = GetSQLiteConnectionString(_datasource); cn.Open(); SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = cn; cmd.CommandText = sqlText; //多个参数 foreach (SQLiteParameter sp in spr) { cmd.Parameters.Add(sp); } int count = await cmd.ExecuteNonQueryAsync(); cn.Close(); return count; }
/// <summary> /// 获取连接字符串 /// </summary> /// <param name="datasource">连接字符串</param> /// <returns></returns> public string GetSQLiteConnectionString(string datasource) { //连接数据库 SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder(); connstr.DataSource = datasource; connstr.Password = "123456";//设置密码,SQLite ADO.NET实现了数据库密码保护 return connstr.ToString(); }
Webconfig
<connectionStrings> <add name="DefaultConnection" connectionString="DataBase//fang_cheng.db" /> </connectionStrings>
调用
SQLiteParameter[] spr = { new SQLiteParameter("@Code", productCode) }; affectRow = await ExecuteNoneQuery(sqlText, spr);