using System;
using System.Data;
using System.Data.Odbc;
namespace ASPNETClassLibrary
{
/// <summary>
/// Command_ODBC 的摘要说明。
/// </summary>
public class Command_ODBC
{
public Command_ODBC()
{
}
#region
/// <summary>
/// 连接数据字段
/// </summary>
private static string _connectionString;
/// <summary>
/// 设置为只读
/// </summary>
public static string connectionString
{
get
{
return _connectionString;
}
set
{
_connectionString = value;
}
}
/// <summary>
/// 打开数据的连接
/// </summary>
/// <returns></returns>
public static OdbcConnection getOdbcConnection()
{
try
{
string OdbcString = _connectionString ; //"Provider=Microsoft.Jet.Odbc.4.0;User ID=Admin;Data Source='" + HttpContext.Current.Server.MapPath("~/LHB_DataBase/LHB.mdb") + "';Jet Odbc:Engine Type=5;Jet Odbc:Database Locking Mode=1;Jet Odbc:Global Partial Bulk Ops=2;Jet Odbc:Global Bulk Transactions=1;Jet Odbc:Create System Database=False;Jet Odbc:Encrypt Database=False;Jet Odbc:Don't Copy Locale on Compact=False;Jet Odbc:Compact Without Replica Repair=False;Jet Odbc:SFP=False";
OdbcConnection oc = new OdbcConnection(OdbcString);
oc.Open();
return oc;
}
catch(Exception ex)
{
string temp = ex.Message;
return null;
}
}
/// <summary>
/// 获取 OdbcCommand 对象
/// </summary>
/// <returns></returns>
public static OdbcCommand getOdbcCommand()
{
OdbcConnection cn = getOdbcConnection();
if (cn == null)
{
return null;
}
else
{
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = cn;
return cmd;
}
}
/// <summary>
/// 新建一个DataSet对象
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Odbc语句,也可是存储过程的名称</param>
/// <returns></returns>
public static DataSet getDataSet(string ExecuteString)
{
OdbcConnection cn = getOdbcConnection();
OdbcDataAdapter odda = new OdbcDataAdapter(ExecuteString,cn);
DataSet ds = new DataSet();
if (cn == null)
{
return null;
}
else
{
odda.Fill(ds);
return ds;
}
}
/// <summary>
/// 新建一个DataSet对象
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Odbc语句,也可是存储过程的名称</param>
/// <param name="name">表的名字</param>
/// <returns></returns>
public static DataSet getDataSet(string ExecuteString, string name)
{
OdbcConnection cn = getOdbcConnection();
OdbcDataAdapter odda = new OdbcDataAdapter(ExecuteString,cn);
DataSet ds = new DataSet();
if (cn == null)
{
return null;
}
else
{
odda.Fill(ds,name);
return ds;
}
}
/// <summary>
/// 新建一个DataSet对象
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Odbc语句,也可是存储过程的名称</param>
/// <returns></returns>
public static DataTable getDataTable(string ExecuteString)
{
OdbcConnection cn = getOdbcConnection();
OdbcDataAdapter odda = new OdbcDataAdapter(ExecuteString,cn);
DataTable dt = new DataTable();
if (cn == null)
{
return null;
}
else
{
odda.Fill(dt);
return dt;
}
}
/// <summary>
/// 执行Odbc语句,返回受影响的行数,
/// -2 说明数据连接失败,
/// -3 发生异常错误。
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Odbc语句,也可是存储过程的名称</param>
/// <returns></returns>
public static int ExecuteNonQuery(string ExecuteString)
{
int count;
OdbcCommand cmd = getOdbcCommand();
if (cmd == null)
{
/// <summary>
/// 说明数据连接失败
/// <summary>
return -2;
}
else
{
/// <summary>
/// 把要执行的字符串给LHB_CMD
/// <summary>
cmd.CommandText = ExecuteString;
/// <summary>
/// 开始执行
/// <summary>
count = cmd.ExecuteNonQuery();
/// <summary>
/// 返回一个值.
/// <summary>
return count;
}
}
/// <summary>
/// 执行指定的Odbc语句,返回首行首列
/// -2 说明数据连接失败,
/// -3 发生异常错误。
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Odbc语句,也可是存储过程的名称</param>
/// <returns>返回首行首列的那个值</returns>
public static string ExecuteScalar(string ExecuteString)
{
try
{
string ShouHangShouLie;
OdbcCommand cmd = getOdbcCommand();
if (cmd == null)
{
return "-2";
}
else
{
cmd.CommandText = ExecuteString;
ShouHangShouLie = cmd.ExecuteScalar().ToString();
return ShouHangShouLie;
}
}
catch (Exception LHB_EE)
{
string temp = LHB_EE.Message;
return "-3";
}
}
#endregion
}
}