sqlconnection

1.web.config file  

 <connectionStrings>
    <add name="SQLCONNECTING" connectionString="data source=192.168.8.12\SQL2008;database=Bstyle_Bus;uid=sa;pwd=sa"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

2.     /// <summary>
        /// 返回一个SqlConnection
        /// </summary>
        /// <returns></returns>
        private SqlConnection returnConn()
        {
            string strCon = ConfigurationManager.ConnectionStrings["SQLCONNECTING"].ToString();
            SqlConnection sqlCon = new SqlConnection(strCon);
            return sqlCon;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
private SqlConnection con; //数据库连接、关闭、释放资源 #region 数据库连接、关闭、释放资源 private void Open() { if (con == null) { //根据Web.Config文件中数据库连接串,建立数据连接 con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString ()); } try { //判断连接对象状态,如果是关闭,将其打开 if (con.State == System.Data.ConnectionState.Closed) con.Open(); } catch (System.Data.SqlClient.SqlException E) { //如果出现错误,关闭数据连接,并抛出错误信息 this.Close(); throw new Exception(E.Message); } } public void Close() { if (con != null) { con.Close();//关闭连接 } } public void Dispose() { if (con != null) { con.Dispose();//释放连接资源 con = null; } } #endregion #region 参数处理 public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) //参数处理 { SqlParameter param; if (Size > 0) { param = new SqlParameter(ParamName, DbType, Size); } else { param = new SqlParameter(ParamName, DbType); } param.Direction = ParameterDirection.Input; if (Value != null) { param.Value = Value; } return param; } #endregion #region 将参数添加到SqlCommand当中 private SqlCommand CreateCommand(string procName, SqlParameter[] prams) //将参数添加到SqlCommand当中 { this.Open();//确认打开连接 SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.Text; //执行类型是命令文本。上面的参数procName即为SQL语句 //cmd.CommandType = CommandType.StoredProcedure; //执行类型是存储过程。上面的参数procName即为存储过程名称 //下面依次把参数传入命令文本 if (prams != null) { foreach (SqlParameter parameter in prams) cmd.Parameters.Add(parameter); } return cmd; } #endregion #region 将参数添加到SqlDataAdapter当中 private SqlDataAdapter CreateDataAdapter(string procName, SqlParameter[] prams) //将参数添加到SqlDataAdapter当中 { this.Open(); SqlDataAdapter dap = new SqlDataAdapter(procName, con); dap.SelectCommand.CommandType = CommandType.Text;//执行类型:命令文本 //dap.SelectCommand.CommandType = CommandType.StoredProcedure;//执行类型:存储过程 if (prams != null) { foreach (SqlParameter parameter in prams) { dap.SelectCommand.Parameters.Add(parameter); } } return dap; } #endregion #region 针对查询(带参) public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName) //针对查询(带参) { SqlDataAdapter dap = CreateDataAdapter(procName, prams); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对查询(不带参) public DataSet RunProcReturn(string procName, string tbName) //针对查询(不带参) { SqlDataAdapter dap = CreateDataAdapter(procName, null); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对增删改 public int RunProc(string procName, SqlParameter[] prams) //针对增删改 { SqlCommand cmd = CreateCommand(procName, prams); int i = cmd.ExecuteNonQuery(); this.Close(); return i; } #endregion }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值