连接不同数据--OleDb(不完整)
using System;
using System.Data;
using System.Data.OleDb;
namespace ASPNETClassLibrary
{
/// <summary>
/// Command_OLEDB 的摘要说明。
/// </summary>
public class Command_OLEDB
{
public Command_OLEDB()
{
}
#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 OleDbConnection getOleDbConnection()
{
try
{
string oledbString = _connectionString ; //"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source='" + HttpContext.Current.Server.MapPath("~/LHB_DataBase/LHB.mdb") + "';Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
OleDbConnection oc = new OleDbConnection(oledbString);
oc.Open();
return oc;
}
catch(Exception ex)
{
string temp = ex.Message;
return null;
}
}
/// <summary>
/// 获取 OleDbCommand 对象
/// </summary>
/// <returns></returns>
public static OleDbCommand getOleDbCommand()
{
OleDbConnection cn = getOleDbConnection();
if (cn == null)
{
return null;
}
else
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
return cmd;
}
}
/// <summary>
/// 新建一个DataSet对象
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Sql语句,也可是存储过程的名称</param>
/// <returns></returns>
public static DataSet getDataSet(string ExecuteString)
{
OleDbConnection cn = getOleDbConnection();
OleDbDataAdapter odda = new OleDbDataAdapter(ExecuteString,cn);
DataSet ds = new DataSet();
if (cn == null)
{
return null;
}
else
{
odda.Fill(ds);
return ds;
}
}
/// <summary>
/// 新建一个DataSet对象
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Sql语句,也可是存储过程的名称</param>
/// <param name="name">表的名字</param>
/// <returns></returns>
public static DataSet getDataSet(string ExecuteString, string name)
{
OleDbConnection cn = getOleDbConnection();
OleDbDataAdapter odda = new OleDbDataAdapter(ExecuteString,cn);
DataSet ds = new DataSet();
if (cn == null)
{
return null;
}
else
{
odda.Fill(ds,name);
return ds;
}
}
/// <summary>
/// 新建一个DataSet对象
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Sql语句,也可是存储过程的名称</param>
/// <returns></returns>
public static DataTable getDataTable(string ExecuteString)
{
OleDbConnection cn = getOleDbConnection();
OleDbDataAdapter odda = new OleDbDataAdapter(ExecuteString,cn);
DataTable dt = new DataTable();
if (cn == null)
{
return null;
}
else
{
odda.Fill(dt);
return dt;
}
}
/// <summary>
/// 执行SQL语句,返回受影响的行数,
/// -2 说明数据连接失败,
/// -3 发生异常错误。
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Sql语句,也可是存储过程的名称</param>
/// <returns></returns>
public static int ExecuteNonQuery(string ExecuteString)
{
int count;
OleDbCommand cmd = getOleDbCommand();
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>
/// 执行指定的Sql语句,返回首行首列
/// -2 说明数据连接失败,
/// -3 发生异常错误。
/// </summary>
/// <param name="ExecuteString">要执行的语句,可是Sql语句,也可是存储过程的名称</param>
/// <returns>返回首行首列的那个值</returns>
public static string ExecuteScalar(string ExecuteString)
{
try
{
string ShouHangShouLie;
OleDbCommand cmd = getOleDbCommand();
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
}
}