</pre><pre name="code" class="csharp">
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data;
namespace HxSpecDB
{
public class sqliteHelper
{
#region 全局变量
public static string userName = "";
public static string userPwd = "";
public static string Mean_SQL = "", Mean_Table = "", Mean_Field = ""; //定义全局变量,记录“基础信息”各窗体中的表名及SQL语句
public static SQLiteConnection conn; //定义一个SqlConnection类型的公共变量conn,用于判断数据库是否连接成功
public static string sqlStr = @"Data Source = spectralDB" + ";Pooling = true;FailIfMissing=false";
#endregion
#region 建立数据库连接
public static SQLiteConnection getcon()
{
conn = new SQLiteConnection(sqlStr);
conn.Open();
return conn;
}
#endregion
#region 测试数据库是否附加
public void con_open()
{
getcon();
}
#endregion
#region 关闭数据库连接
public void con_close()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
#endregion
#region 读取制定表中的信息
public SQLiteDataReader getcom(string SQLstr)
{
getcon();
SQLiteCommand My_com = conn.CreateCommand();
My_com.CommandText = SQLstr;
SQLiteDataReader My_read = My_com.ExecuteReader();
return My_read;
}
#endregion
#region 执行SqlCommand命令
public void getsqlcom(string SQLstr)
{
getcon();
SQLiteCommand SQLcom = new SQLiteCommand(SQLstr, conn);
SQLcom.ExecuteNonQuery();
SQLcom.Dispose();
con_close();
}
#endregion
#region 创建数据集
public DataSet getDataSet(string SQLstr, string tableName)
{
getcon();
SQLiteDataAdapter SQLda = new SQLiteDataAdapter(SQLstr, conn);
DataSet My_DataSet = new DataSet();
SQLda.Fill(My_DataSet, tableName);
con_close();
return My_DataSet;
}
#endregion
}
}
我觉得代码就可以贴出来共享,这样可以帮助到很多才入门的人。