271 lines
9.0 KiB
C#
271 lines
9.0 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;
|
|
using iAnywhere.Data.SQLAnywhere;
|
|
using System.Collections;
|
|
|
|
namespace HiiShe.Manager
|
|
{
|
|
class DBHelper
|
|
{
|
|
//private static string connstring = "host=192.168.10.116;server=pos2159;userid=dba;password=sql;Connect Timeout=3;";
|
|
private static string connstring = "host=127.0.0.1;server=" + System.Net.Dns.GetHostName() + ";userid=dba;password=sql;Connect Timeout=3;";
|
|
//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 (SAConnection conn = new SAConnection(connstring))
|
|
{
|
|
SACommand cmd = new SACommand(SqlString, conn);
|
|
try
|
|
{
|
|
conn.Open();
|
|
SADataAdapter adp = new SADataAdapter(cmd);
|
|
DataSet ds = new DataSet();
|
|
adp.Fill(ds);
|
|
conn.Close();
|
|
return ds;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
public static bool SyBaseTest()
|
|
{
|
|
using (SAConnection conn = new SAConnection(connstring))
|
|
{
|
|
try
|
|
{
|
|
conn.Open();
|
|
conn.Close();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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="SQLStringList"></param>
|
|
public static void ExecuteSqlTran(List<string> SQLStringList)
|
|
{
|
|
using (SAConnection conn = new SAConnection(connstring))
|
|
{
|
|
conn.Open();
|
|
SACommand cmd = new SACommand();
|
|
cmd.Connection = conn;
|
|
SATransaction 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 (Exception E)
|
|
{
|
|
tx.Rollback();
|
|
throw new Exception(E.Message);
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="SQLStringList"></param>
|
|
public static void ExecuteSqlTran(string SQLString)
|
|
{
|
|
using (SAConnection conn = new SAConnection(connstring))
|
|
{
|
|
conn.Open();
|
|
SACommand cmd = new SACommand();
|
|
cmd.Connection = conn;
|
|
SATransaction tx = conn.BeginTransaction();
|
|
cmd.Transaction = tx;
|
|
try
|
|
{
|
|
if (SQLString.Trim().Length > 1)
|
|
{
|
|
cmd.CommandText = SQLString;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
tx.Commit();
|
|
}
|
|
catch (Exception E)
|
|
{
|
|
tx.Rollback();
|
|
throw new Exception(E.Message);
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存数据(参数化)
|
|
/// </summary>
|
|
/// <param name="SQLStringList">事务sql语句组</param>
|
|
public static void ExecuteSqlTran(Hashtable SQLStringList)
|
|
{
|
|
using (SAConnection conn = new SAConnection(connstring))
|
|
{
|
|
conn.Open();
|
|
SACommand cmd = new SACommand();
|
|
cmd.Connection = conn;
|
|
SATransaction tx = conn.BeginTransaction();
|
|
cmd.Transaction = tx;
|
|
string strsql;
|
|
try
|
|
{
|
|
foreach (DictionaryEntry item in SQLStringList)
|
|
{
|
|
strsql = item.Key.ToString();
|
|
if (strsql.Trim().Length > 1)
|
|
{
|
|
cmd.CommandText = strsql;
|
|
if (item.Value != null)
|
|
{
|
|
foreach (SAParameter par in (SAParameter[])item.Value)
|
|
{
|
|
cmd.Parameters.Add(par);
|
|
}
|
|
}
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
tx.Commit();
|
|
}
|
|
catch (Exception E)
|
|
{
|
|
tx.Rollback();
|
|
throw new Exception(E.Message);
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取最大主键ID
|
|
/// </summary>
|
|
/// <param name="SqlString"></param>
|
|
/// <returns></returns>
|
|
public static int OdbcGetMaxPKID(string SqlString)
|
|
{
|
|
using (SAConnection conn = new SAConnection(connstring))
|
|
{
|
|
SACommand cmd = new SACommand(SqlString, conn);
|
|
try
|
|
{
|
|
conn.Open();
|
|
int max_id = cmd.ExecuteScalar().ToString().Equals("") ? 0 : int.Parse(cmd.ExecuteScalar().ToString());
|
|
conn.Close();
|
|
return max_id;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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 (OleDbException E)
|
|
{
|
|
tx.Rollback();
|
|
throw new Exception(E.Message);
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|