求 C# winform用sqlite数据库 实现删改的代码

 我来答
饕餮飞花舞
2013-04-26 · 超过19用户采纳过TA的回答
知道答主
回答量:69
采纳率:0%
帮助的人:30.2万
展开全部

给你个操作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];
}
}
}

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式