170 lines
5.8 KiB
C#
170 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Odbc;
|
|
using System.Data.OleDb;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
namespace HiiShe.Manager
|
|
{
|
|
class DBHelper
|
|
{
|
|
private static string connstring = "DSN=anysql1;UID=dba;PWD=sql;";
|
|
private static string filePath = System.Threading.Thread.GetDomain().BaseDirectory + "hydb.mdb";
|
|
private static string filePath1 = System.Threading.Thread.GetDomain().BaseDirectory + "commodity.mdb";
|
|
private static string oleconnstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ filePath + ";Persist Security Info=False";
|
|
private static string oleconnstring1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath1 + ";Persist Security Info=False";
|
|
/// <summary>
|
|
/// 查询本地数据库
|
|
/// </summary>
|
|
/// <param name="SqlString"></param>
|
|
/// <returns></returns>
|
|
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 new Exception (ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询基础数据库
|
|
/// </summary>
|
|
/// <param name="SqlString"></param>
|
|
/// <returns></returns>
|
|
public static DataSet QueryOle(string SqlString)
|
|
{
|
|
using (OleDbConnection conn = new OleDbConnection(oleconnstring))
|
|
{
|
|
OleDbCommand cmd = new OleDbCommand(SqlString, conn);
|
|
try
|
|
{
|
|
conn.Open();
|
|
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
|
|
DataSet ds = new DataSet();
|
|
adp.Fill(ds);
|
|
conn.Close();
|
|
return ds;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 查询基础数据库
|
|
/// </summary>
|
|
/// <param name="SqlString"></param>
|
|
/// <returns></returns>
|
|
public static DataSet QueryOledb(string SqlString)
|
|
{
|
|
using (OleDbConnection conn = new OleDbConnection(oleconnstring1))
|
|
{
|
|
OleDbCommand cmd = new OleDbCommand(SqlString, conn);
|
|
try
|
|
{
|
|
conn.Open();
|
|
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
|
|
DataSet ds = new DataSet();
|
|
adp.Fill(ds);
|
|
conn.Close();
|
|
return ds;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="SQLStringList"></param>
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 备份数据
|
|
/// </summary>
|
|
/// <param name="SQLStringList"></param>
|
|
public static void OleExecuteSqlTran(List<string> SQLStringList)
|
|
{
|
|
using (OleDbConnection conn = new OleDbConnection(oleconnstring))
|
|
{
|
|
conn.Open();
|
|
OleDbCommand cmd = new OleDbCommand();
|
|
cmd.Connection = conn;
|
|
OleDbTransaction 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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|