110,535
社区成员
发帖
与我相关
我的任务
分享
private void TextBox2_TextChanged(object sender, EventArgs e) //新增插入数据
{
SQLiteCommand cmdInsert = new SQLiteCommand(mConn);
string sql = "INSERT INTO KD(工号,到达时间)values(@工号,@到达时间)";
SQLiteParameter[] cmdParms = new SQLiteParameter[]{
new SQLiteParameter("@工号", (textBox2.Text)),
new SQLiteParameter("@到达时间", DateTime.Now)
};
cmd.ExecuteNonQuery(sql,cmdParms);
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SQLiteQueryBrowser
{
/// <summary>
/// 本类为SQLite数据库帮助静态类,使用时只需直接调用即可,无需实例化
/// </summary>
public static class SQLiteHelper
{
/// <summary>
/// 数据库连接字符串
/// </summary>
//public static string connectionString = "Data Source=" + Application.StartupPath + "\\" + System.Configuration.ConfigurationSettings.AppSettings["Contr"];
public static string connectionString = "Data Source=" + Application.StartupPath + "/KDDB.db";
#region 执行数据库操作(新增、更新或删除),返回影响行数
/// <summary>
/// 执行数据库操作(新增、更新或删除)
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(SQLiteCommand cmd)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
/// <summary>
/// 执行数据库操作(新增、更新或删除)
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
/// <summary>
/// 执行数据库操作(新增、更新或删除)
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText, cmdParms);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion
#region 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// <summary>
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(SQLiteCommand cmd)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
/// <summary>
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
/// <summary>
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText, cmdParms);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion
#region 执行数据库查询,返回SqlDataReader对象
/// <summary>
/// 执行数据库查询,返回SqlDataReader对象
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>SqlDataReader对象</returns>
public static DbDataReader ExecuteReader(SQLiteCommand cmd)
{
DbDataReader reader = null;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
SQLiteConnection con = new SQLiteConnection(connectionString);
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText);
try
{
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
return reader;
}
/// <summary>
/// 执行数据库查询,返回SqlDataReader对象
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>Sql