/// <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(); }
Webconfigsql
<connectionStrings> <add name="DefaultConnection" connectionString="DataBase//fang_cheng.db" /> </connectionStrings>
調用數據庫
SQLiteParameter[] spr = { new SQLiteParameter("@Code", productCode) }; affectRow = await ExecuteNoneQuery(sqlText, spr);