using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
namespace BaseLibDataProcess
{
public class OraDBBase
{
public OleDbConnection oledbConn = null;
///
/// 服务名
///
public string strServerName { get; set; }
///
/// 用户名
///
public string strID { get; set; }
///
/// 密码
///
public string strPWD { get; set; }
///
/// 连接字符串
///
public string strConnectString { get; set; }
///
/// 连接状态
///
public bool bConnectState { get; set; }
///
/// oracle数据库构造函数
///
public OraDBBase()
{
}
///
/// oracle数据库构造函数
///
/// 服务名
/// 用户名
/// 密码
public OraDBBase(string ServerName, string ID, string PWD)
{
strServerName = ServerName;
strID = ID;
strPWD = PWD;
}
///
/// 获得连接字符串
///
/// 连接字符串
public string getConnectString()
{
if (string.IsNullOrEmpty(strServerName))
{
throw new OraDBBaseException("请设置服务名");
}
if (string.IsNullOrEmpty(strID))
{
throw new OraDBBaseException("请设置ID");
}
if (string.IsNullOrEmpty(strPWD))
{
throw new OraDBBaseException("请设置PWD");
}
strConnectString = "Provider=MSDAORA.1" +
";Data Source=" + strServerName +
";User Id=" + strID +
";Password=" + strPWD +
";Persist Security Info=False";
return strConnectString;
}
///
/// 获取连接状态
///
/// 布尔值
public bool getConnectState()
{
return bConnectState;
}
///
/// 获取指定字段的指定值的插入语句
///
/// 表
/// 字段和值
/// 字符串值
public static string getInsertString(string Table, string[,] arrFieldAndValue)
{
string strSQL = string.Empty;
string str1 = "", str2 = "";
strSQL = "insert into " + Table + "(";
for (int i = 0; i < arrFieldAndValue.GetLength(0); i++)
{
str1 += string.Format("{0},", arrFieldAndValue[i, 0]);
str2 += string.Format("'{0}',", arrFieldAndValue[i, 1]);
}
strSQL += str1.Substring(0, str1.Length - 1) + ") values (";
strSQL += str2.Substring(0, str2.Length - 1) + ")";
return strSQL;
}
///
/// 获取指定表的指定字段的指定值的插入语句
///
/// 表
/// 字段
/// 值
/// 字符串值
public static string getInsertString(string Table, string[] arrField, string[] arrValue)
{
if (arrField.Length != arrValue.Length)
{
throw new OraDBBaseException("字段的个数和值的个数不一致");
}
string str1 = "", str2 = "";
string strSQL = "insert into " + Table + "(";
for (int i = 0; i < arrField.Length; i++)
{
str1 += string.Format("{0},", arrField[i]);
str2 += string.Format("'{0}',", arrValue[i]);
}
strSQL += str1.Substring(0, str1.Length - 1) + ") values (";
strSQL += str2.Substring(0, str2.Length - 1) + ")";
return strSQL;
}
///
/// 获取所有字段的所有值的插入语句
///
/// 表
/// 值
/// 字符串值
public string getInsertString(string Table, string[] arrValue)
{
string strSQL = string.Empty;
string[] arrField = getTableColumns(Table).ToArray();
if (arrField.Length != arrValue.Length)
{
throw new OraDBBaseException("字段的个数和值的个数不一致");
}
string str1 = "", str2 = "";
strSQL = "insert into " + Table + "(";
for (int i = 0; i < arrField.Length; i++)
{
str1 += string.Format("{0},", arrField[i]);
str2 += string.Format("'{0}',", arrValue[i]);
}
strSQL += str1.Substring(0, str1.Length - 1) + ") values (";
strSQL += str2.Substring(0, str2.Length - 1) + ")";
return strSQL;
}
///
/// 获取所有字段的所有值的插入语句
///
/// 表
/// 值
/// 字符串值
public static string getInsertString2(string Table, string[] arrValue)
{
string str2 = "";
string strSQL = "insert into " + Table + " values (";
for (int i = 0; i < arrValue.Length; i++)
{
str2 += string.Format("'{0}',", arrValue[i]);
}
strSQL += str2.Substring(0, str2.Length - 1) + ")";
return strSQL;
}
///
/// 获取有条件的指定字段的查询语句
///
/// 表
/// 字段和值
/// 字符串值
public static string getSelectString(string Table, string[,] arrFieldAndValue)
{
string strSQL = string.Empty;
strSQL = string.Format("select * from {0} where ", Table);
string str1 = "";
for (int i = 0; i < arrFieldAndValue.GetLength(0); i++)
{
str1 += string.Format("{0}='{1}' and ", arrFieldAndValue[i, 0], arrFieldAndValue[i, 1]);
}
strSQL += str1.Substring(0, str1.Length - 5);
return strSQL;
}
///
/// 获取有条件的指定字段的查询语句
///
/// 表
/// 字段
/// 值
/// 字符串值
public static string getSelectString(string Table, string[] arrField, string[] arrValue)
{
if (arrField.Length != arrValue.Length)
{
throw new OraDBBaseException("字段的个数和值的个数不一致");
}
string strSQL = string.Format("select * from {0} where ", Table);
string str1 = "";
for (int i = 0; i < arrField.Length; i++)
{
str1 += string.Format("{0}='{1}' and ", arrField[i], arrValue[i]);
}
strSQL += str1.Substring(0, str1.Length - 5);
return strSQL;
}
///
/// 获取无条件的指定字段的查询语句
///
/// 表
/// 字段
/// 字符串值
public static string getSelectString(string Table, string[] arrField)
{
string str1 = "";
string strSQL = "select ";
for (int i = 0; i < arrField.Length; i++)
{
str1 += string.Format("{0},", arrField[i]);
}
strSQL += str1.Substring(0, str1.Length - 1) + " from " + Table;
return strSQL;
}
///
/// 获取所有字段的查询语句
///
/// 表
/// 字符串值
public string getSelectString2(string Table)
{
return string.Format("select * from {0}", Table);
}
///
/// 获取所有字段的查询语句
///
/// 表
/// 字符串值
public string getSelectString(string Table)
{
string[] arrField = getTableColumns(Table).ToArray();
string str1 = "";
string strSQL = "select ";
for (int i = 0; i < arrField.Length; i++)
{
str1 += string.Format("{0},", arrField[i]);
}
strSQL += str1.Substring(0, str1.Length - 1) + " from " + Table;
return strSQL;
}
///
/// 获取创建表语句
///
/// 表
/// 字段和类型
/// 字符串值
public static string getCreateString(string Table, string[,] FieldAndType)
{
string str1 = "";
string strSQL = "create table " + Table + "(";
for (int i = 0; i < FieldAndType.GetLength(0); i++)
{
str1 += string.Format("{0} {1},", FieldAndType[i, 0], FieldAndType[i, 1]);
}
strSQL += str1.Substring(0, str1.Length - 1) + ")";
return strSQL;
}
///
/// 获取更新语句
///
/// 表
/// 新的字段和值
/// 旧的字段和值
/// 字符串值
public static string getUpdateString(string Table, string[,] arrFieldAndNewValue, string[,] arrFieldAndOldValue)
{
string strSQL = string.Empty;
string str1 = "update " + Table + " set ";
for (int i = 0; i < arrFieldAndNewValue.GetLength(0); i++)
{
str1 += string.Format("{0}= '{1}',", arrFieldAndNewValue[i, 0], arrFieldAndNewValue[i, 1]);
}
string str2 = " where ";
for (int i = 0; i < arrFieldAndOldValue.GetLength(0); i++)
{
str2 += string.Format("{0}= '{1}' and ", arrFieldAndOldValue[i, 0], arrFieldAndOldValue[i, 1]);
}
strSQL += str1.Substring(0, str1.Length - 1);
strSQL += str2.Substring(0, str2.Length - 5);
return strSQL;
}
///
/// 获取删除语句
///
/// 表
/// 条件字段
/// 条件值
/// 字符串值
public static string getDeleteString(string Table, string Field, string Value)
{
return string.Format("delete from {0} where {1}='{2}'", Table, Field, Value);
}
///
/// 获取指定表的指定字段和值的删除语句
///
/// 表名
/// 条件字段
/// 条件值
///
public static string getDeleteString(string Table, string[] arrField, string[] arrValue)
{
if (arrField.Length != arrValue.Length)
{
throw new OraDBBaseException("字段的个数和值的个数不一致");
}
string strSQL = string.Empty;
string str1 = "";
str1 = "delete from " + Table + " where ";
for (int i = 0; i < arrField.Length; i++)
{
str1 += string.Format("{0}= '{1}' and ", arrField[i], arrValue[i]);
}
strSQL += str1.Substring(0, str1.Length - 5);
return strSQL;
}
///
/// 获取指定表的指定字段和值的删除语句
///
/// 表名
/// 条件字段和值
///
public static string getDeleteString(string Table, string[,] arrFieldAndValue)
{
string strSQL = string.Empty;
string str1 = "delete from " + Table + " where ";
for (int i = 0; i < arrFieldAndValue.GetLength(0); i++)
{
str1 += string.Format("{0}= '{1}' and ", arrFieldAndValue[i, 0], arrFieldAndValue[i, 1]);
}
strSQL += str1.Substring(0, str1.Length - 5);
return strSQL;
}
///
/// 获取删除语句
///
/// 表
/// 字符串值
public string getDeleteString(string Table)
{
return string.Format("delete from {0}", Table);
}
///
/// 获取删除表语句
///
///
/// 字符串值
public string getDropString(string Table)
{
return string.Format("drop table {0}", Table);
}
public string[] List2Arr(List list)
{
return list.ToArray();
}
public List List2Arr(string[] arr)
{
return arr.ToList();
}
public static string[,] List2Arr(List list)
{
if (list.Count > 0)
{
string[,] arr = new string[list.Count, list[0].Length];
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < list[0].Length; j++)
{
arr[i, j] = list[i][j];
}
}
return arr;
}
return null;
}
public static List List2Arr(string[,] arr)
{
List v = new List();
for (int i = 0; i < arr.GetLength(0); i++)
{
string[] arr1 = new string[arr.GetLength(1)];
for (int j = 0; j < arr.GetLength(1); j++)
{
arr1[j] = arr[i, j];
}
v.Add(arr1);
}
return v;
}
///
/// 连接数据库
///
/// 布尔值
public bool connect()
{
try
{
string str1 = getConnectString();
if (str1 != null)
{
if (oledbConn == null)
{
bConnectState = false;
oledbConn = new OleDbConnection(str1);
oledbConn.Open();
if (oledbConn.State == ConnectionState.Open)
{
bConnectState = true;
}
}
else
{
if (oledbConn.State != ConnectionState.Open)
{
oledbConn.Open();
if (oledbConn.State == ConnectionState.Open)
{
bConnectState = true;
}
}
}
}
else
{
throw new OraDBBaseException("无效的连接字符串");
}
}
catch (System.Exception ex)
{
throw new OraDBBaseException(ex.Message);
}
return bConnectState;
}
///
/// 断开数据库连接
///
public void disconnect()
{
if (oledbConn != null)
{
oledbConn.Close();
oledbConn.Dispose();
oledbConn = null;
bConnectState = false;
}
}
///
/// 执行SQL语句
///
/// sql语句
/// 布尔值
public bool executeSQL(string SQL)
{
bool res1 = false;
if (SQL == null)
{
throw new OraDBBaseException("无效的SQL语句");
}
if (oledbConn != null)
{
if (bConnectState)
{
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = oledbConn;
cmd1.CommandText = SQL;
cmd1.CommandType = CommandType.Text;
int num = cmd1.ExecuteNonQuery();
if (num > 0)
{
res1 = true;
}
cmd1.Dispose();
}
else
{
throw new OraDBBaseException("数据库连接没有成功");
}
}
return res1;
}
///
/// 获得用户的所有的表名
///
/// 链表值
public List getTables()
{
List arr1 = new List();
if (oledbConn != null)
{
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = oledbConn;
cmd1.CommandText = string.Format("select TABLE_NAME from USER_TABLES order by TABLE_NAME");
cmd1.CommandType = CommandType.Text;
OleDbDataReader rd1 = cmd1.ExecuteReader();
while (rd1.Read())
{
for (int i = 0; i < rd1.FieldCount; i++)
{
arr1.Add(rd1[i].ToString());
}
}
rd1.Close();
rd1.Dispose();
cmd1.Dispose();
}
return arr1;
}
///
/// 获得指定的表的所有字段名
///
/// 表
/// 链表值
public List getTableColumns(string Table)
{
List arr1 = new List();
try
{
if (oledbConn != null)
{
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = oledbConn;
cmd1.CommandText = $"select t.COLUMN_NAME from USER_TAB_COLUMNS t where t.TABLE_NAME='{Table.ToUpper()}'";
cmd1.CommandType = CommandType.Text;
OleDbDataReader rd1 = cmd1.ExecuteReader();
while (rd1.Read())
{
for (int i = 0; i < rd1.FieldCount; i++)
{
Console.WriteLine(rd1[i].ToString());
arr1.Add(rd1[i].ToString());
}
}
rd1.Close();
rd1.Dispose();
cmd1.Dispose();
}
}
catch (System.Exception ex)
{
string error = ex.ToString();
}
finally
{
}
return arr1;
}
///
/// 获得指定的表的所有数据
///
/// 表
/// 数组链表值
public List getTableValues(string Table)
{
return selectTableValues(getSelectString2(Table));
}
///
/// 执行查询SQL语句
///
/// sql语句
/// 数组链表值
public List selectSQL(string SQL)
{
List m1 = new List();
if (oledbConn != null)
{
if (bConnectState)
{
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = oledbConn;
cmd1.CommandText = SQL;
cmd1.CommandType = CommandType.Text;
OleDbDataReader rd1 = cmd1.ExecuteReader();
while (rd1.Read())
{
string[] arr1 = new string[rd1.FieldCount];
for (int i = 0; i < rd1.FieldCount; i++)
{
arr1[i] = rd1[i].ToString();
}
m1.Add(arr1);
}
rd1.Close();
rd1.Dispose();
cmd1.Dispose();
}
else
{
throw new OraDBBaseException("数据库连接没有成功");
}
}
return m1;
}
///
/// 执行查询SQL语句
///
/// sql语句
/// 记录集
public DataSet selectSQL3(string SQL)
{
DataSet ds = new DataSet();
if (oledbConn != null)
{
if (bConnectState)
{
OleDbDataAdapter da = new OleDbDataAdapter(SQL, oledbConn);
int m = da.Fill(ds);
}
else
{
throw new OraDBBaseException("数据库连接没有成功");
}
}
return ds;
}
///
/// 查询记录的个数
///
/// sql语句
///
public int selectSQL2(string SQL)
{
int num = 0;
if (oledbConn != null)
{
if (bConnectState)
{
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = oledbConn;
cmd1.CommandText = SQL;
cmd1.CommandType = CommandType.Text;
OleDbDataReader rd1 = cmd1.ExecuteReader();
while (rd1.Read())
{
num++;
}
rd1.Close();
rd1.Dispose();
cmd1.Dispose();
}
else
{
throw new OraDBBaseException("数据库连接没有成功");
}
}
return num;
}
///
/// 执行指定的查询语句
///
/// sql语句
/// 数组链表值
public List selectTableValues(string SQL)
{
return selectSQL(SQL);
}
///
/// 查询指定的表的所有数据
///
/// 表
/// 数组链表值
public List selectTable(string Table)
{
return selectSQL(getSelectString2(Table));
}
///
/// 执行指定表的有条件的查询语句
///
/// 表
/// 条件字段和值
/// 数组链表值
public List selectTable(string Table, string[,] arrFieldAndValue)
{
return selectSQL(getSelectString(Table, arrFieldAndValue));
}
///
/// 执行指定表的有条件的查询语句
///
/// 表
/// 字段
/// 值
/// 数组链表
public List selectTable(string Table, string[] arrField, string[] arrValue)
{
return selectSQL(getSelectString(Table, arrField, arrValue));
}
///
/// 执行指定表的有条件的查询语句
///
/// 表
/// 字段
/// 值
/// 记录集
public DataSet selectTable2(string Table, string[] arrField, string[] arrValue)
{
return selectSQL3(getSelectString(Table, arrField, arrValue));
}
///
/// 执行无条件的指定字段的查询语句
///
/// 表
/// 字段
/// 数组链表
public List selectTable(string Table, string[] arrField)
{
return selectSQL(getSelectString(Table, arrField));
}
///
/// 执行插入语句
///
/// sql语句
/// 布尔值
public bool insertSQL(string SQL)
{
return executeSQL(SQL);
}
///
/// 执行指定表的指定字段的指定值的插入语句
///
/// 表
/// 字段
/// 值
/// 布尔值
public bool insertSQL(string Table, string[] arrField, string[] arrValue)
{
return insertSQL(getInsertString(Table, arrField, arrValue));
}
///
/// 检查记录然后执行指定表的指定字段的指定值的插入语句
///
/// 表名
/// 字段
/// 值
///
public bool insertSQLWithCheck(string Table, string[] arrField, string[] arrValue)
{
if (checkSQL(Table, arrField, arrValue))
{
return false;
}
return insertSQL(getInsertString(Table, arrField, arrValue));
}
///
/// 执行指定表的指定字段的指定值的插入语句
///
/// 表
/// 字段和值
/// 布尔值
public bool insertSQL(string Table, string[,] FieldValue)
{
return insertSQL(getInsertString(Table, FieldValue));
}
///
/// 检查记录然后执行指定表的指定字段的指定值的插入语句
///
/// 表名
/// 字段和值
///
public bool insertSQLWithCheck(string Table, string[,] arrFieldValue)
{
if (checkSQL(Table, arrFieldValue))
{
return false;
}
return insertSQL(getInsertString(Table, arrFieldValue));
}
///
/// 先检查表中是否有记录,有记录则删除记录,然后插入记录
///
/// 表名
/// 字段
/// 值
///
public bool insertSQLWithDelete(string Table, string[] arrField, string[] arrValue)
{
if (checkSQL(Table, arrField, arrValue))
{
deleteSQL(Table, arrField, arrValue);
}
return insertSQL(getInsertString(Table, arrField, arrValue));
}
///
/// 先检查表中是否有记录,有记录则删除记录,然后插入记录
///
/// 表名
/// 字段和值
///
public bool insertSQLWithDelete(string Table, string[,] arrFieldValue)
{
if (checkSQL(Table, arrFieldValue))
{
deleteSQL(Table, arrFieldValue);
}
return insertSQL(getInsertString(Table, arrFieldValue));
}
///
/// 执行指定的表全字段的所有值的插入语句
///
///
///
/// 布尔值
public bool insertSQL(string Table, string[] arrValue)
{
return insertSQL(getInsertString(Table, arrValue));
}
///
/// 执行删除语句
///
/// sql语句
/// 布尔值
public bool deleteSQL2(string SQL)
{
return executeSQL(SQL);
}
///
/// 执行指定表的指定字段的指定值的删除语句
///
/// 表
/// 条件字段
/// 条件值
/// 布尔值
public bool deleteSQL(string Table, string Field, string Value)
{
return deleteSQL2(getDeleteString(Table, Field, Value));
}
///
/// 执行指定表的指定字段的指定值的删除语句
///
/// 表名
/// 条件字段
/// 条件值
///
public bool deleteSQL(string Table, string[] arrField, string[] arrValue)
{
return deleteSQL2(getDeleteString(Table, arrField, arrValue));
}
///
/// 执行指定表的指定字段的指定值的删除语句
///
/// 表名
/// 条件字段和值
///
public bool deleteSQL(string Table, string[,] arrFieldAndValue)
{
return deleteSQL2(getDeleteString(Table, arrFieldAndValue));
}
///
/// 执行指定表的所有数据的删除语句
///
/// 表
/// 布尔值
public bool deleteSQL(string Table)
{
return deleteSQL2(getDeleteString(Table));
}
///
/// 执行表的删除表语句
///
/// sql语句
/// 布尔值
public bool dropSQL2(string SQL)
{
return executeSQL(SQL);
}
///
/// 执行指定表的删除表语句
///
/// 表
/// 布尔值
public bool dropSQL(string Table)
{
return dropSQL2(getDeleteString(Table));
}
///
/// 执行创建表语句
///
/// sql语句
/// 布尔值
public bool createSQL(string SQL)
{
return executeSQL(SQL);
}
///
/// 执行指定字段和类型的创建表语句
///
/// 表
/// 字段和类型
/// 布尔值
public bool createSQL(string Table, string[,] FieldAndType)
{
return createSQL(getCreateString(Table, FieldAndType));
}
///
/// 执行更新表语句
///
///
/// 布尔值
public bool updateSQL(string SQL)
{
return executeSQL(SQL);
}
///
/// 执行指定表的指定字段的指定条件的更新语句
///
/// 表
/// 新的字段和值
/// 条件字段和值
/// 布尔值
public bool updateSQL(string Table, string[,] FieldAndNewValue, string[,] FieldAndOldValue)
{
return updateSQL(getUpdateString(Table, FieldAndNewValue, FieldAndOldValue));
}
///
/// 指定字段指定值的记录在指定的表中是否有记录
///
/// 表名
/// 条件字段
/// 条件值
///
public bool checkSQL(string Table, string[] arrField, string[] arrValue)
{
int n = selectSQL2(getSelectString(Table, arrField, arrValue));
return n > 0 ? true : false;
}
///
/// 指定字段指定值的记录在指定的表中是否有记录
///
/// 表名
/// 条件字段和值
///
public bool checkSQL(string Table, string[,] arrFieldAndValue)
{
int n = selectSQL2(getSelectString(Table, arrFieldAndValue));
return n > 0 ? true : false;
}
}
public class OraDBBaseException : ApplicationException
{
public OraDBBaseException(string msg)
: base(msg)
{
}
public override string Message
{
get
{
return base.Message;
}
}
}
}