ECGGather/1200gFtp/MySQLHelper.cs
2024-12-31 10:56:48 +08:00

85 lines
2.4 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 MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ECG数据解析服务端
{
internal class MySQLHelper
{
//连接数据库
static string connectionStrings = ConfigurationManager.ConnectionStrings["MYSQLConnectionString"].ConnectionString;//连接语句,请按照自己的连接修改
static MySqlConnection conn = new MySqlConnection(connectionStrings);
private static void InitConn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
}
//增删改
public static bool ExecuteNonQuery(string sqlStr)
{
InitConn();
MySqlCommand cmd = new MySqlCommand(sqlStr, conn);
int result = cmd.ExecuteNonQuery();
conn.Close();
return result > 0;
}
//执行集合函数
public static object ExecuteScalar(string sqlStr)
{
InitConn();
MySqlCommand cmd = new MySqlCommand(sqlStr, conn);
object result = cmd.ExecuteScalar();
conn.Close();
return result;
}
//查询获取DataTable
public static DataTable GetTable(string sqlStr)
{
InitConn();
MySqlCommand cmd = new MySqlCommand(sqlStr);
cmd.Connection = conn;
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}
//二进制数据增删改
public static bool Binary(string sqlStr, Dictionary<string, byte[]> parameters)
{
InitConn();
MySqlCommand cmd = new MySqlCommand(sqlStr, conn);
int i = 0;
foreach (KeyValuePair<string, byte[]> kvp in parameters)
{
cmd.Parameters.Add("@" + kvp.Key, MySqlDbType.LongBlob);
cmd.Parameters[i].Value = kvp.Value;
i++;
}
int result = cmd.ExecuteNonQuery();
conn.Close();
return result > 0;
}
}
}