/// <summary>
/// Ole操作类
/// </summary>
public class OleDataBaseHandle
{
private static OleDbConnection con = null;
private static OleDbCommand cmd = null;
/// <summary>
/// 通过文件路径 获取文件的所有表名
/// </summary>
/// <param name="Filename">文件路径</param>
/// <returns></returns>
public static DataTable GetFileTableName(string Filename)
{
string DbConnect = string.Empty;
try
{
DbConnect = GetConnect(Filename);//通过文件获取连接字符串
con = new OleDbConnection(DbConnect);
con.Open();
DataTable dt = new DataTable();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" });
return dt;
}
catch (Exception)
{
return null;
}
finally
{
if (cmd != null)
{
cmd.Dispose();
}
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
/// <summary>
/// 通过文件路径和表名获取所有数据(现支持EXCEL2003和ACCESS2003)
/// </summary>
/// <param name="Filename">文件路径</param>
/// <param name="TableName">表名</param>
/// <returns></returns>
public static DataTable GetFileTableData(string Filename, string TableName)
{
string DbConnect = string.Empty;
try
{
DbConnect = GetConnect(Filename);//通过文件获取连接字符串
con = new OleDbConnection(DbConnect);
con.Open();
DataTable dt = new DataTable();
cmd = new OleDbCommand("select * from [" + TableName + "]", con);
OleDbDataAdapter dapter = new OleDbDataAdapter(cmd);
dapter.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
if (cmd != null)
{
cmd.Dispose();
}
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
/// <summary>
/// 通过数据文件和表名获取该表的所有列(现支持EXCEL2003和ACCESS2003)
/// </summary>
/// <param name="Filename">文件路径</param>
/// <param name="TableName">文件表名</param>
/// <returns></returns>
public static DataTable GetFileTableName(string Filename, string TableName)
{
List<string> ErrorList = new List<string>();
OleDbCommand cmd = null;
OleDbConnection con = null;
string DbConnect = string.Empty;
try
{
DbConnect = GetConnect(Filename);//通过文件获取连接字符串
con = new OleDbConnection(DbConnect);
con.Open();
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, TableName, null });
if (dt == null || dt.Rows.Count < 1)
{
return null;
}
else
{
return dt;
}
}
catch (Exception ex)
{
ErrorList.Add(ex.Message);
return null;
}
finally
{
if (cmd != null)
{
cmd.Dispose();
}
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
/// <summary>
/// 通过语句执行返回结果true false
/// </summary>
/// <param name="StrQuery">执行的SQL语句</param>
/// <returns></returns>
public static bool OleImplement(string StrQuery, string ConnectStr)
{
try
{
con = new OleDbConnection(ConnectStr);
con.Open();
cmd = new OleDbCommand(StrQuery, con);
int a = cmd.ExecuteNonQuery();
if (a != 0)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
finally
{
if (cmd != null)
{
cmd.Dispose();
}
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
/// <summary>
/// 通过数据文件和表名获取该表的所有列
/// </summary>
/// <param name="StrQuery">所执行的SQL语句</param>
/// <returns></returns>
public static bool OleImplement(string StrQuery)
{
try
{
cmd = new OleDbCommand(StrQuery, con);
int a = cmd.ExecuteNonQuery();
if (a != 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception exxx)
{
return false;
}
}
/// <summary>
/// 通过连接字符串建立长链接
/// </summary>
/// <param name="StrConnect">连接数据库字符串</param>
public static void OleConnectOpen(string StrConnect)
{
con = new OleDbConnection(StrConnect);
con.Open();
}
/// <summary>
/// 关闭长链接并释放资源
/// </summary>
public static void OleConnectColse()
{
if (cmd != null)
{
cmd.Dispose();
}
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
}
/// <summary>
/// 执行参数化SQL语句
/// </summary>
/// <param name="StrQuery">所执行的SQL语句</param>
/// <param name="para">参数化集合</param>
/// <returns></returns>
public static bool OleImplement(string StrQuery, List<OleDbParameter> para)
{
try
{
cmd = new OleDbCommand(StrQuery, con);
foreach (var item in para)
{
cmd.Parameters.Add(item);
}
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 通过文件路径返回ADO连接字符串
/// </summary>
/// <param name="Filename">文件路径</param>
/// <returns></returns>
public static string GetConnect(string Filename)
{
string DbConnect = string.Empty;
if (Filename.Split('.')[1].ToLower() == "xls")
{
DbConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Filename + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=2'";
}
else if (Filename.Split('.')[1].ToLower() == "xlsx")
{
DbConnect = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + Filename + ";Extended Properties='Excel 12.0 Macro; HDR = NO'";
}
else if (Filename.Split('.')[1].ToLower() == "mdb")
{
DbConnect = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Filename;
}
else if (Filename.Split('.')[1].ToLower() == "accdb")
{
DbConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Filename;
}
else
{
DbConnect = null;
}
return DbConnect;
}
}
浙公网安备 33010602011771号