chore: 初始化东安动力外部数据接口工程
This commit is contained in:
126
DatabaseClient/MysqlClient.cs
Normal file
126
DatabaseClient/MysqlClient.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
public class MysqlClient : DBClient
|
||||
{
|
||||
MySqlConnection conn;
|
||||
|
||||
public MysqlClient() { }
|
||||
|
||||
public override void Connect(string connString)
|
||||
{
|
||||
try
|
||||
{
|
||||
conn = new MySqlConnection(connString);
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public override DataTable ExecQuery(string sql)
|
||||
{
|
||||
DataTable dt = null;
|
||||
try
|
||||
{
|
||||
if (conn != null)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
MySqlCommand command = new MySqlCommand(sql, conn);
|
||||
MySqlDataAdapter da = new MySqlDataAdapter(command);
|
||||
DataSet ds = new DataSet();
|
||||
da.Fill(ds);
|
||||
|
||||
conn.Close();
|
||||
|
||||
if (ds.Tables.Count > 0)
|
||||
{
|
||||
dt = ds.Tables[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
|
||||
public override int ExecNonQuery(string sql)
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if (conn != null)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
MySqlCommand command = new MySqlCommand(sql, conn);
|
||||
res = command.ExecuteNonQuery();
|
||||
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static bool ExecQueryDataTable(string sql,string ConnectStr,out DataTable dt, out string err)
|
||||
{
|
||||
bool success = false;
|
||||
err = "";
|
||||
dt = null;
|
||||
using (MySqlConnection conn = new MySqlConnection(ConnectStr))
|
||||
{
|
||||
try
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
MySqlCommand command = new MySqlCommand(sql, conn);
|
||||
MySqlDataAdapter da = new MySqlDataAdapter(command);
|
||||
DataSet ds = new DataSet();
|
||||
da.Fill(ds);
|
||||
|
||||
if (ds.Tables.Count > 0)
|
||||
{
|
||||
dt = ds.Tables[0];
|
||||
}
|
||||
success = true;
|
||||
}
|
||||
catch (Exception er)
|
||||
{
|
||||
err = er.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (conn != null)
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user