DockTool/accessOptions.cs
2024-12-12 14:09:48 +08:00

227 lines
6.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Data.SqlClient;
using System;
using System.Text;
using System.Data.OleDb;
namespace FlyDockTool
{
public class accessOptions
{
public OleDbConnection SqlConn = null;
/// <summary>
/// 让此类创建一个单例类
/// </summary>
public static accessOptions Instance = null;//申明一个EcgDrawing对象复制Null
private static readonly object LockHelper = new object();
public static accessOptions CreateInstance(string accessDbPath)
{
if (Instance == null)
{
lock (LockHelper)
{
if (Instance == null)
Instance = new accessOptions(accessDbPath);
}
}
return Instance;
}
public accessOptions(string accessDbPath)
{
GetSqlConnection(accessDbPath);
}
/// <summary>
/// 获得sqlite数据库连接
/// </summary>
/// <returns></returns>
public bool GetSqlConnection(string accessDbPath)
{
try
{
string ConnectionStr = "Provider=Microsoft.Jet.OleDb.4.0;";
ConnectionStr += @"Data Source="+ accessDbPath + "";
OleDbConnection conn = new OleDbConnection(ConnectionStr);
SqlConn = conn;
}
catch { return false; }
return true;
}
/// <summary>
/// 执行sqlite查询语句并返回一个 DataTable
/// </summary>
/// <param name="sqliteStr"></param>
/// <returns></returns>
public DataTable ExcuteSql(string sqliteStr)
{
try
{
if (SqlConn.State == ConnectionState.Closed)
{
SqlConn.Open();
}
var sqliteDp = new OleDbDataAdapter(sqliteStr, SqlConn) {SelectCommand = {CommandTimeout = 600000}};
var sqliteds = new DataSet();
sqliteDp.Fill(sqliteds);
return sqliteds.Tables[0];
}
catch(Exception ex)
{
//byte[] ecgDataArray = Encoding.UTF8.GetBytes(sqliteStr.ToString() + "\r\n"+ex.ToString()+"\r\n");
//ecgFileWrite(ecgDataArray, @"d:\log.txt");
return null;
}
}
private void ecgFileWrite(byte[] ecgDataArray, string FilePath)
{
FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate);
fs.Position = fs.Length;
//开始写入
fs.Write(ecgDataArray, 0, ecgDataArray.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
/// <summary>
/// Sqlite 添加数据的操作函数
/// </summary>
/// <param name="sqliteSQL"></param>
/// <returns></returns>
public bool SqlAdd(string sqliteSQL)
{
try
{
if (SqlConn.State == ConnectionState.Closed)
SqlConn.Open();
var sqliteCmd = new OleDbCommand(sqliteSQL, SqlConn);
sqliteCmd.ExecuteNonQuery();
sqliteCmd.Dispose();
SqlConn.Close();
}
catch { return false; }
return true;
}
/// <summary>
/// Sqlite 删除数据的操作函数
/// </summary>
/// <param name="sqliteSQL"></param>
/// <returns></returns>
public bool SqlDelete(string sqliteSQL)
{
try
{
if (SqlConn.State == ConnectionState.Closed)
SqlConn.Open();
var sqliteCmd = new OleDbCommand(sqliteSQL, SqlConn);
sqliteCmd.ExecuteNonQuery();
sqliteCmd.Dispose();
SqlConn.Close();
}
catch { return false; }
return true;
}
/// <summary>
/// Sqlite 更新数据的操作函数
/// </summary>
/// <param name="sqliteSQL"></param>
/// <returns></returns>
public bool SqlUpdate(string sqliteSQL)
{
try
{
if (SqlConn.State == ConnectionState.Closed)
SqlConn.Open();
var sqliteCmd = new OleDbCommand(sqliteSQL, SqlConn);
sqliteCmd.ExecuteNonQuery();
sqliteCmd.Dispose();
SqlConn.Close();
}
catch { return false; }
return true;
}
/// <summary>
/// 执行SQL语句函数
/// </summary>
/// <param name="sqlcmd">SQL语句</param>
/// <param name="paras">SQL语句中的参数组</param>
/// <returns>返回受影响记录条数</returns>
public int ExecuteSql(string sqlcmd, params SqlParameter[] paras)
{
var cmd = new OleDbCommand(sqlcmd, SqlConn);
if (SqlConn.State == ConnectionState.Closed)
{
SqlConn.Open();
}
foreach (SqlParameter p in paras)
{
cmd.Parameters.Add(p);
}
int cnt = cmd.ExecuteNonQuery();
SqlConn.Close();
return cnt;
}
/// <summary>
/// Sqlite 执行数据库的多条语句,
/// </summary>
/// <param name="sqliteSQL"></param>
/// <returns></returns>
public bool SqlExecuteNonQuery(string[] sqliteSQL)
{
if (SqlConn.State == ConnectionState.Closed)
{
SqlConn.Open();
}
OleDbTransaction sqlTran = SqlConn.BeginTransaction();
try
{
foreach (string sql in sqliteSQL)
{
var sqliteCmd = new OleDbCommand(sql, SqlConn, sqlTran);
int i = sqliteCmd.ExecuteNonQuery();
sqliteCmd.Dispose();
}
sqlTran.Commit();
}
catch
{
sqlTran.Rollback();
return false;
}
finally
{
SqlConn.Close();
}
return true;
}
public Image GetImg(byte[] imgBytes)
{
var ms = new MemoryStream(imgBytes);
Image img = Image.FromStream(ms);
return img;
}
}
}