数据库
首页 > 数据库> > sqlHelper+app.config

sqlHelper+app.config

作者:互联网

public class SqlHelper
    {
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public static string ConString = ConfigurationManager.ConnectionStrings["HotelManagerConnectionString"].ToString();
        public static string Dbowner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();

        /// <summary>
        /// 设置执行工具
        /// </summary>
        /// <param name="con">连接对象</param>
        /// <param name="cmd">工具</param>
        /// <param name="paras">参数数组</param>
        /// <param name="sqlStr">SQL语句</param>
        /// <param name="cmdType">命令类型</param>
        public static void PrepareCommand(SqlConnection con, SqlCommand cmd, SqlParameter[] paras, string sqlStr,CommandType cmdType)
        {
            if(con.State != ConnectionState.Open)
            {
                con.Open();
            }
            cmd.Connection = con;
            cmd.CommandType = cmdType;
            cmd.CommandText = sqlStr;
            if(paras==null)
            {
                return;
            }
            else
            {
                foreach (SqlParameter p in paras)
                {
                    cmd.Parameters.Add(p);
                }
            }
        }

        /// <summary>
        /// 执行查询,返回数据集
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandPrarmeters"></param>
        /// <returns></returns>
        public static DataTable Execute(CommandType cmdType, string cmdText, params SqlParameter[] commandPrarmeters)
        {
            SqlConnection connection = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand();
            PrepareCommand(connection, cmd, commandPrarmeters, cmdText, cmdType);
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            return ds.Tables[0];
        }

        /// <summary>
        /// 查询首行首列的值
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandPrarmeters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(CommandType cmdType,string cmdText, params SqlParameter[] commandPrarmeters)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection connection = new SqlConnection (ConString))
            {
                PrepareCommand(connection, cmd, commandPrarmeters, cmdText, cmdType );
                object val = cmd.ExecuteScalar();//返回查询的第一行第一列
                return val;
            }
        }

        /// <summary>
        /// 获取reader对象
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static SqlDataReader GetDataReader(string cmdText, CommandType cmdType, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(ConString);
            try
            {
                PrepareCommand(conn, cmd, commandParameters, cmdText, cmdType);
                //sqlDataReader必须数据库打开才能运行,所以使用commandBehavior重载,同时关闭connection
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch(Exception)
            {
                conn.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行增删改,返回受影响行数
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection (ConString))
            {
                PrepareCommand(conn, cmd, commandParameters , cmdText, cmdType);
                int val = cmd.ExecuteNonQuery();
                return val;
            }
        }
    }
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings >
    <add name="DataBaseOwner"  connectionString="dbo"/>
    <add name ="HotelManagerConnectionString" connectionString="Data Source=.;Database=HotelManageEx;User ID=sa;Password=123" providerName ="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="DBtype" value="Sql"/>
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

 

标签:cmdText,sqlHelper,app,cmd,SqlConnection,cmdType,new,config,SqlCommand
来源: https://www.cnblogs.com/bedfly/p/12465816.html