求 C# winform用sqlite数据库 实现删改的代码
展开全部
给你个操作SQLite数据库的类。
调用方式:
增删改
SQLiteHelper.ExecuteNonQuery("insert/delete/update的sql语句", null);
查
SQLiteHelper.Query("select的sql语句")
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.Data.Common;
namespace DAL
{
public class SQLiteHelper
{
/// <summary>
/// ConnectionString样例:Data Source=Test.db3;Pooling=true;FailIfMissing=false
/// </summary>
public static string ConnectionString { get; set; }
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Parameters.Clear();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 30;
if (p != null)
{
foreach (object parm in p)
cmd.Parameters.AddWithValue(string.Empty, parm);
}
}
public static DataSet ExecuteQuery(string cmdText, params object[] p)
{
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
using (SQLiteCommand command = new SQLiteCommand())
{
DataSet ds = new DataSet();
PrepareCommand(command, conn, cmdText, p);
SQLiteDataAdapter da = new SQLiteDataAdapter(command);
da.Fill(ds);
return ds;
}
}
}
public static int ExecuteNonQuery(string cmdText, params object[] p)
{
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
using (SQLiteCommand command = new SQLiteCommand())
{
PrepareCommand(command, conn, cmdText, p);
return command.ExecuteNonQuery();
}
}
}
public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p)
{
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
using (SQLiteCommand command = new SQLiteCommand())
{
PrepareCommand(command, conn, cmdText, p);
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
public static object ExecuteScalar(string cmdText, params object[] p)
{
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
using (SQLiteCommand command = new SQLiteCommand())
{
PrepareCommand(command, conn, cmdText, p);
return command.ExecuteScalar();
}
}
}
public static DataTable Query(string sqlStr)
{
return ExecuteQuery(sqlStr, null).Tables[0];
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询