125 lines
3.7 KiB
C#
125 lines
3.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Odbc;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace CheckTool
|
|
{
|
|
class DbHelper
|
|
{
|
|
private static string connstring = "DSN=anysql1;UID=dba;PWD=sql;";
|
|
|
|
#region 测试连接
|
|
/// <summary>
|
|
/// 测试连接
|
|
/// </summary>
|
|
public void TestConn()
|
|
{
|
|
//测试连接
|
|
using (OdbcConnection _OdbcConnection = new OdbcConnection(connstring))
|
|
{
|
|
using (OdbcCommand cmd = new OdbcCommand())
|
|
{
|
|
try
|
|
{
|
|
_OdbcConnection.Open();
|
|
}
|
|
catch (System.Data.Odbc.OdbcException ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
_OdbcConnection.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
public static DataSet QueryOdbc(string SqlString)
|
|
{
|
|
using (OdbcConnection conn = new OdbcConnection(connstring))
|
|
{
|
|
OdbcCommand cmd = new OdbcCommand(SqlString, conn);
|
|
try
|
|
{
|
|
conn.Open();
|
|
OdbcDataAdapter adp = new OdbcDataAdapter(cmd);
|
|
DataSet ds = new DataSet();
|
|
adp.Fill(ds);
|
|
conn.Close();
|
|
return ds;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void ExecuteSqlTran(List<string> SQLStringList)
|
|
{
|
|
using (OdbcConnection conn = new OdbcConnection(connstring))
|
|
{
|
|
conn.Open();
|
|
OdbcCommand cmd = new OdbcCommand();
|
|
cmd.Connection = conn;
|
|
OdbcTransaction tx = conn.BeginTransaction();
|
|
cmd.Transaction = tx;
|
|
try
|
|
{
|
|
for (int n = 0; n < SQLStringList.Count; n++)
|
|
{
|
|
string strsql = SQLStringList[n].ToString();
|
|
if (strsql.Trim().Length > 1)
|
|
{
|
|
cmd.CommandText = strsql;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
tx.Commit();
|
|
}
|
|
catch (System.Data.OleDb.OleDbException E)
|
|
{
|
|
tx.Rollback();
|
|
throw new Exception(E.Message);
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string GetValue(string Name)
|
|
{
|
|
string strLine = "";
|
|
try
|
|
{
|
|
FileStream aFile = new FileStream("d://HighWayPosSoft/setup.ini", FileMode.Open);
|
|
StreamReader sr = new StreamReader(aFile);
|
|
for (strLine = sr.ReadLine(); strLine != null; strLine = sr.ReadLine())
|
|
{
|
|
if (strLine.ToLower().Contains(Name))
|
|
{
|
|
sr.Close();
|
|
string result = strLine.Replace(Name, "");
|
|
return result;
|
|
}
|
|
}
|
|
sr.Close();
|
|
}
|
|
catch
|
|
{
|
|
string result = "";
|
|
return result;
|
|
}
|
|
return "";
|
|
}
|
|
}
|
|
}
|