C#操作sqlite数据库使用SQLiteParameter传递参数
C# code
public void AddIMG_ENTRY(img_entry model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into IMG_ENTRY(");
strSql.Append("IMG_ID,USER_ID,FOLDER_ID,STATU_ID,WORK_TIME,START_TIME,END_TIME,OUTPUT_STATUS,INPUT_DATA,INPUT_STATES,FORMAT_NAME)");
strSql.Append(" values (");
strSql.Append("@IMG_ID,@USER_ID,@FOLDER_ID,@STATU_ID,@WORK_TIME,@START_TIME,@END_TIME,@OUTPUT_STATUS,@INPUT_DATA,@INPUT_STATES,@FORMAT_NAME)");
SQLiteParameter[] parameters = {
new SQLiteParameter("@IMG_ID", DbType.Int32,4),
new SQLiteParameter("@USER_ID", DbType.String),
new SQLiteParameter("@FOLDER_ID", DbType.Int32,4),
new SQLiteParameter("@STATU_ID", DbType.Int32,4),
new SQLiteParameter("@WORK_TIME", DbType.DateTime),
new SQLiteParameter("@START_TIME", DbType.DateTime),
new SQLiteParameter("@END_TIME", DbType.DateTime),
new SQLiteParameter("@OUTPUT_STATUS", DbType.Int32,4),
new SQLiteParameter("@INPUT_DATA", DbType.String),
new SQLiteParameter("@INPUT_STATES", DbType.Int32,4),
new SQLiteParameter("@FORMAT_NAME", DbType.String)};
parameters[0].Value = model.IMG_ID;
parameters[1].Value = model.USER_ID;
parameters[2].Value = model.FOLDER_ID;
parameters[3].Value = model.STATU_ID;
parameters[4].Value = model.WORK_TIME;
parameters[5].Value = model.START_TIME;
parameters[6].Value = model.END_TIME;
parameters[7].Value = model.OUTPUT_STATUS;
parameters[8].Value = model.INPUT_DATA;
parameters[9].Value = model.INPUT_STATES;
parameters[10].Value = model.FORMAT_NAME;
DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
}
public static int ExecuteSql(string SQLString, string content)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}