C# SQL帮助类

本人自己封装的SQLHelper类,执行sql server与Oracle数据的增删改查

vs自带的Oracle数据库引用需要安装Oracle客户端,如不想安装Oracle客户端,可以引用 Oracle.ManagedDataAccess.dll

下载链接: https://pan.baidu.com/s/1dzzTxL2fmUPrMy2Qc246jg 提取码: 16my 

C# SQL帮助类

C# SQL帮助类

public class SQLHelper { /// <summary> /// 执行查询语句 /// </summary> /// <param>连接字符串</param> /// <param>sql语句</param> /// <param>查询的字段名</param> /// <returns>查询结果或错误信息</returns> public static string[] ExecuteReaderSql(string ConnStr,string Sql,string[] Param) { string[] info = new string[Param.Count()]; SqlConnection conn = null; try { conn = new SqlConnection(ConnStr); conn.Open(); SqlCommand cmd = new SqlCommand(Sql, conn); SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); if (!sdr.HasRows) { info[0] = "null"; return info; } for (int i = 0; i <Param.Count(); i++) { info[i] = sdr[Param[i]].ToString(); } } catch(Exception ex) { info[0] = "sql查询错误信息:" + ex.Message; return info; } finally { conn.Close(); } return info; } /// <summary> /// 执行增删改 /// </summary> /// <param>连接字符串</param> /// <param>sql语句</param> /// <returns>受影响的行数或错误信息</returns> public static string ExecuteSql(string ConnStr,string Sql) { SqlConnection conn = null; int Row = 0; try { conn = new SqlConnection(ConnStr); conn.Open(); SqlCommand cmd = new SqlCommand(Sql,conn); Row = cmd.ExecuteNonQuery(); } catch(Exception ex) { return "sql执行错误信息:" + ex.Message; } finally { conn.Close(); } return Row.ToString(); } /// <summary> /// Oracle增删改 /// </summary> /// <param>连接字符串</param> /// <param>Oracle语句</param> /// <returns>受影响行数或错误信息</returns> public static string ExecuteOrcl(string ConnStr, string orcl) { OracleConnection conn = null; int Row = 0; try { conn = new OracleConnection(ConnStr); conn.Open(); OracleCommand cmd = new OracleCommand(orcl, conn); Row = cmd.ExecuteNonQuery(); } catch (Exception ex) { return "orcl执行错误信息:" + ex.Message; } finally { conn.Close(); } return Row.ToString(); } /// <summary> /// Oracle查询 /// </summary> /// <param>连接字符串</param> /// <param>Oracle语句</param> /// <param>查询字段名</param> /// <returns>查询结果或错误信息</returns> public static string[] ExecuteReaderOrcl(string ConnStr, string orcl, string[] Param) { OracleConnection conn = null; string[] info = new string[Param.Count()]; try { conn = new OracleConnection(ConnStr); conn.Open(); OracleCommand cmd = new OracleCommand(orcl, conn); OracleDataReader odr = cmd.ExecuteReader(); odr.Read(); if(!odr.HasRows) { info[0] = "null"; return info; } for (int i = 0; i < Param.Count(); i++) { info[i] = odr[Param[i]].ToString(); } } catch (Exception ex) { info[0] = "orcl查询错误信息:" + ex.Message; return info; } finally { conn.Close(); } return info; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfwwy.html