标签:oracl record link eric system using sys 结果 行操作
/*---------------------------------------------------------------- * // author:HQ * // describe:Database connet * // date:2019-10-08 * ------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Data.OracleClient; using System.Data.SqlClient; using MySql.Data.MySqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WorkNet { public class Database_Operation { private SqlConnection sql_con; private OracleConnection oracle_con; private MySqlConnection mysql_con; #region SqlServer /// <summary> /// SQLserver open /// </summary> /// <param name="link">link statement</param> /// <returns>Success:success; Fail:reason</returns> public string Sqlserver_Open(string link) { try { sql_con = new SqlConnection(link); sql_con.Open(); return "success"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// SQLserver close /// </summary> /// <returns>Success:success Fail:reason</returns> public string Sqlserver_Close() { try { if (sql_con == null) { return "No database connection"; } if (sql_con.State == ConnectionState.Open || sql_con.State == ConnectionState.Connecting) { sql_con.Close(); sql_con.Dispose(); } else { if (sql_con.State == ConnectionState.Closed) { return "success"; } if (sql_con.State == ConnectionState.Broken) { return "ConnectionState:Broken"; } } return "success"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// SQLserver insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string Sqlserver_Insdelupd(string sql) { try { int num = 0; if (sql_con == null) { return "Please open the database connection first"; } if (sql_con.State == ConnectionState.Open) { SqlCommand sqlCommand = new SqlCommand(sql, sql_con); num = sqlCommand.ExecuteNonQuery(); } else { if (sql_con.State == ConnectionState.Closed) { return "Database connection closed"; } if (sql_con.State == ConnectionState.Broken) { return "Database connection is destroyed"; } if (sql_con.State == ConnectionState.Connecting) { return "The database is in connection"; } } return "success" + num; } catch (Exception ex) { return ex.Message.ToString(); } } /// <summary> /// SQLserver insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <param name="link">link statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string Sqlserver_Insdelupd(string sql, string link) { try { int num = 0; using (SqlConnection con = new SqlConnection(link)) { con.Open(); //操作数据库的工具SqlCommand SqlCommand cmd = new SqlCommand(sql, con); //操作语句和链接工具 num = cmd.ExecuteNonQuery(); //执行操作返回影响行数 con.Close(); return "success" + num; } } catch (Exception ex) { return ex.Message.ToString(); } } /// <summary> /// SQLserver select /// </summary> /// <param name="sql">select statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet Sqlserver_Select(string sql, out string record) { try { //储存数据的工具初始化 DataSet dataSet = new DataSet(); if (sql_con == null) { record = "Please open the database connection first"; return dataSet; } if (sql_con.State == ConnectionState.Open) { SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sql_con); sqlDataAdapter.Fill(dataSet, "sample"); sqlDataAdapter.Dispose(); record = "success"; return dataSet; } if (sql_con.State == ConnectionState.Closed) { record = "Database connection closed"; return dataSet; } if (sql_con.State == ConnectionState.Broken) { record = "Database connection is destroyed"; return dataSet; } if (sql_con.State == ConnectionState.Connecting) { record = "The database is in connection"; return dataSet; } record = "ERROR"; return dataSet; } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } } /// <summary> /// SQLserver select /// </summary> /// <param name="sql">select statement</param> /// <param name="link">link statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet Sqlserver_Select(string sql, string link, out string record) { try { //储存数据的工具初始化 DataSet ds = new DataSet(); //相当于链接数据库的一个工具类(连接字符串) using (SqlConnection con = new SqlConnection(link)) { con.Open(); //打开 //用SqlConnection工具链接数据库,在通过sql查询语句查询结果现存入sql适配器 SqlDataAdapter sda = new SqlDataAdapter(sql, con); //(查询语句和连接工具) sda.Fill(ds, "sample"); //将适配器数据存入DataSet工具中 con.Close(); //用完关闭SqlConnection工具 sda.Dispose(); //手动释放SqlDataAdapter record = "success"; return ds; } } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } } #endregion #region MySQL /// <summary> /// MySQL open /// </summary> /// <param name="link">link statement</param> /// <returns>Success:success; Fail:reason</returns> public string MySQL_Open(string link) { try { mysql_con = new MySqlConnection(link); mysql_con.Open(); return "success"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// MySQL close /// </summary> /// <returns>Success:success Fail:reason</returns> public string MySQL_Close() { try { if (mysql_con == null) { return "No database connection"; } if (mysql_con.State == ConnectionState.Open || mysql_con.State == ConnectionState.Connecting) { mysql_con.Close(); mysql_con.Dispose(); } else { if (mysql_con.State == ConnectionState.Closed) { return "success"; } if (mysql_con.State == ConnectionState.Broken) { return "ConnectionState:Broken"; } } return "success"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// MySQL insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string MySQL_Insdelupd(string sql) { try { int num = 0; if (mysql_con == null) { return "Please open the database connection first"; } if (mysql_con.State == ConnectionState.Open) { MySqlCommand sqlCommand = new MySqlCommand(sql, mysql_con); num = sqlCommand.ExecuteNonQuery(); } else { if (mysql_con.State == ConnectionState.Closed) { return "Database connection closed"; } if (mysql_con.State == ConnectionState.Broken) { return "Database connection is destroyed"; } if (mysql_con.State == ConnectionState.Connecting) { return "The database is in connection"; } } return "success" + num; } catch (Exception ex) { return ex.Message.ToString(); } } /// <summary> /// MySQL insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <param name="link">link statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string MySQL_Insdelupd(string sql, string link) { try { int num = 0; using (MySqlConnection con = new MySqlConnection(link)) { con.Open(); //操作数据库的工具SqlCommand MySqlCommand cmd = new MySqlCommand(sql, con); //(操作语句和链接工具) num = cmd.ExecuteNonQuery(); //执行操作返回影响行数 con.Close(); return "success" + num; } } catch (Exception ex) { return ex.Message.ToString(); } } /// <summary> /// MySQL select /// </summary> /// <param name="sql">select statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet MySQL_Select(string sql, out string record) { try { //储存数据的工具初始化 DataSet dataSet = new DataSet(); if (mysql_con == null) { record = "Please open the database connection first"; return dataSet; } if (mysql_con.State == ConnectionState.Open) { MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, mysql_con); sqlDataAdapter.Fill(dataSet, "sample"); sqlDataAdapter.Dispose(); record = "success"; return dataSet; } if (mysql_con.State == ConnectionState.Closed) { record = "Database connection closed"; return dataSet; } if (mysql_con.State == ConnectionState.Broken) { record = "Database connection is destroyed"; return dataSet; } if (mysql_con.State == ConnectionState.Connecting) { record = "The database is in connection"; return dataSet; } record = "ERROR"; return dataSet; } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } } /// <summary> /// MySQL select /// </summary> /// <param name="sql">select statement</param> /// <param name="link">link statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet MySQL_Select(string sql, string link, out string record) { try { //储存数据的工具初始化 DataSet ds = new DataSet(); //相当于链接数据库的一个工具类(连接字符串) using (MySqlConnection con = new MySqlConnection(link)) { con.Open(); //打开 //用SqlConnection工具链接数据库,在通过sql查询语句查询结果现存入sql适配器 MySqlDataAdapter sda = new MySqlDataAdapter(sql, con); //(查询语句和连接工具) sda.Fill(ds, "sample"); //将适配器数据存入DataSet工具中 con.Close(); //用完关闭SqlConnection工具 sda.Dispose(); //手动释放SqlDataAdapter record = "success"; return ds; } } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } } #endregion #region Oracle /// <summary> /// Oracle open /// </summary> /// <param name="link">link statement</param> /// <returns>Success:success; Fail:reason</returns> public string Oracle_Open(string link) { try { oracle_con = new OracleConnection(link); oracle_con.Open(); return "success"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// Oracle close /// </summary> /// <returns>Success:success Fail:reason</returns> public string Oracle_Close() { try { if (oracle_con == null) { return "No database connection"; } if (oracle_con.State == ConnectionState.Open) { oracle_con.Close(); oracle_con.Dispose(); } else { if (oracle_con.State == ConnectionState.Closed) { return "success"; } if (oracle_con.State == ConnectionState.Broken) { return "ConnectionState:Broken"; } } return "success"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// Oracle insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string Oracle_Insdelupd(string sql) { try { int num = 0; if (oracle_con == null) { return "Please open the database connection first"; } if (oracle_con.State == ConnectionState.Open) { OracleCommand oracleCommand = new OracleCommand(sql, oracle_con); num = oracleCommand.ExecuteNonQuery(); } else { if (oracle_con.State == ConnectionState.Closed) { return "Database connection closed"; } if (oracle_con.State == ConnectionState.Broken) { return "Database connection is destroyed"; } } return "success" + num; } catch (Exception ex) { return ex.Message.ToString(); } } /// <summary> /// Oracle insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <param name="link">link statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string Oracle_Insdelupd(string sql, string link) { try { int num = 0; using (OracleConnection oracleConnection = new OracleConnection(link)) { DataSet dataSet = new DataSet(); oracleConnection.Open(); OracleCommand oracleCommand = new OracleCommand(sql, oracleConnection); num = oracleCommand.ExecuteNonQuery(); oracleConnection.Close(); return "success" + num; } } catch (Exception ex) { return ex.Message.ToString(); } } /// <summary> /// Oracle select /// </summary> /// <param name="sql">select statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet Oracle_Select(string sql, out string record) { try { DataSet dataSet = new DataSet(); if (oracle_con != null) { if (oracle_con.State == ConnectionState.Open) { OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(sql, oracle_con); oracleDataAdapter.Fill(dataSet, "sample"); oracleDataAdapter.Dispose(); record = "OK"; return dataSet; } if (oracle_con.State == ConnectionState.Closed) { record = "Database connection closed"; } else if (oracle_con.State == ConnectionState.Broken) { record = "Database connection is destroyed"; } } else { record = "Please open the database connection first"; } record = "error"; return dataSet; } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } } /// <summary> /// Oracle select /// </summary> /// <param name="sql">select statement</param> /// <param name="link">link statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet Oracle_Select(string sql, string link, out string record) { try { using (OracleConnection oracleConnection = new OracleConnection(link)) { DataSet dataSet = new DataSet(); oracleConnection.Open(); OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(sql, oracleConnection); oracleDataAdapter.Fill(dataSet, "sample"); oracleDataAdapter.Dispose(); oracleConnection.Close(); record = "success"; return dataSet; } } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } } #endregion #region Access /// <summary> /// Access select /// </summary> /// <param name="sql">select statement</param> /// <param name="link">link statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet Access_Mdb_Select(string sql, string link, out string record) { try { DataSet dataSet = new DataSet(); using (OleDbConnection oleDbConnection = new OleDbConnection(link)) { oleDbConnection.Open(); OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(sql, oleDbConnection); oleDbDataAdapter.Fill(dataSet, "sample"); oleDbDataAdapter.Dispose(); oleDbConnection.Close(); record = "success"; return dataSet; } } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } } /// <summary> /// Access insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <param name="link">link statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string Access_Mdb_Insdelupd(string sql, string link) { try { using (OleDbConnection oleDbConnection = new OleDbConnection(link)) { DataSet dataSet = new DataSet(); oleDbConnection.Open(); OleDbCommand oleDbCommand = new OleDbCommand(sql, oleDbConnection); int num = oleDbCommand.ExecuteNonQuery(); oleDbConnection.Close(); return "success" + num; } } catch (Exception ex) { return ex.Message.ToString(); } } #endregion } }
C#学习--SQLserver、oracle、MySQL和Access的封装源码
标签:oracl record link eric system using sys 结果 行操作
原文地址:https://www.cnblogs.com/mexihq/p/12467245.html