C# SQL帮助类
阅读原文时间:2023年07月12日阅读:3

C# SQL帮助类

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

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

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

public class SQLHelper
{
///

/// 执行查询语句 ///
/// 连接字符串
/// sql语句
/// 查询的字段名
/// 查询结果或错误信息
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[] = "null"; return info; }
for (int i = ; i <Param.Count(); i++)
{
info[i] = sdr[Param[i]].ToString();
}
}
catch(Exception ex)
{
info[] = "sql查询错误信息:" + ex.Message;
return info;
}
finally
{
conn.Close();
}

        return info;  
    }  
    /// <summary>  
    /// 执行增删改  
    /// </summary>  
    /// <param name="ConnStr">连接字符串</param>  
    /// <param name="Sql">sql语句</param>  
    /// <returns>受影响的行数或错误信息</returns>  
    public static string ExecuteSql(string ConnStr,string Sql)  
    {  
        SqlConnection conn = null;  
        int Row = ;  
        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 name="ConnStr">连接字符串</param>  
    /// <param name="orcl">Oracle语句</param>  
    /// <returns>受影响行数或错误信息</returns>  
    public static string ExecuteOrcl(string ConnStr, string orcl)  
    {  
        OracleConnection conn = null;  
        int Row = ;  
        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 name="ConnStr">连接字符串</param>  
    /// <param name="orcl">Oracle语句</param>  
    /// <param name="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\[\] = "null"; return info; }  
            for (int i = ; i < Param.Count(); i++)  
            {  
                info\[i\] = odr\[Param\[i\]\].ToString();  
            }  
        }  
        catch (Exception ex)  
        {  
            info\[\] = "orcl查询错误信息:" + ex.Message;  
            return info;  
        }  
        finally  
        {  
            conn.Close();  
        }  
        return info;  
    }  
}

SqlHelper