<connectionStrings>

<add name="sqlConnection" connectionString="server=--------;Database=WeChatReport;Uid= ;Pwd= " providerName="System.Data.SqlClient"/>

</connectionStrings>

APP_CODE;

using System; using System.Data; using System.Configuration;

using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient; using System.Text; using System.Collections;

/// <summary> /// Summary description for SqlDataAccess /// </summary> public class SqlDataAccess {     //sqlConnection     private string pvtStrSqlCon = ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString;     //private SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon);

private string pvtStrTableName = string.Empty;     private SqlCommand pvtSqlCom = null;

//with table name as parameter     public SqlDataAccess(string strTableName)     {         ////set pvtStrSqlCon         //if (pvtStrSqlCon == string.Empty)         //{         //    pvtStrSqlCon = ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString;         //}

//set pvtStrTableName         if (pvtStrTableName == string.Empty)         {             pvtStrTableName = strTableName;         }

//set pvtSqlCom         pvtSqlCom = new SqlCommand();         //pvtSqlCom.Connection = pvtSqlCon;

}

//without parameter     public SqlDataAccess()     {         //set pvtStrSqlCon         //if (pvtStrSqlCon == string.Empty)         //{         //    pvtStrSqlCon = ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString;         //}

//set pvtSqlCom         pvtSqlCom = new SqlCommand();         //pvtSqlCom.Connection = pvtSqlCon;     }

//     ~SqlDataAccess()     {     }

//when table name set, get table data according to sqlParamCol     public DataSet GetTableData(SqlParameterCollection sqlParamCol)     {         using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;

StringBuilder strSql = new StringBuilder("", 200);             strSql.Append("select * from ");             strSql.Append(pvtStrTableName);             if (sqlParamCol.Count > 0)             {                 strSql.Append(" where 1=1 ");                 for (int i = 0; i < sqlParamCol.Count; i++)                 {                     strSql.Append(" and ");                     strSql.Append(sqlParamCol[i].SourceColumn);                     strSql.Append(" = @");                     strSql.Append(sqlParamCol[i].SourceColumn);

pvtSqlCom.Parameters.Add(sqlParamCol[i].ParameterName, sqlParamCol[i].SqlDbType, sqlParamCol[i].Size, sqlParamCol[i].SourceColumn);                     pvtSqlCom.Parameters[i].Value = sqlParamCol[i].Value;                 }             }             pvtSqlCom.CommandText = strSql.ToString();

SqlDataAdapter daResult = new SqlDataAdapter(pvtSqlCom);             DataSet dsResult = new DataSet();             daResult.Fill(dsResult);

pvtSqlCon.Close();             return dsResult;         }     }

public int GetDataCount(SqlParameterCollection sqlParamCol)     {         using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;             StringBuilder strSql = new StringBuilder("", 200);

strSql.Append("select COUNT(1) from ");             strSql.Append(pvtStrTableName);             if (sqlParamCol.Count > 0)             {                 strSql.Append(" where 1=1 ");                 for (int i = 0; i < sqlParamCol.Count; i++)                 {                     strSql.Append(" and ");                     strSql.Append(sqlParamCol[i].SourceColumn);                     strSql.Append(" = @");                     strSql.Append(sqlParamCol[i].SourceColumn);

pvtSqlCom.Parameters.Add(sqlParamCol[i].ParameterName, sqlParamCol[i].SqlDbType, sqlParamCol[i].Size, sqlParamCol[i].SourceColumn);                     pvtSqlCom.Parameters[i].Value = sqlParamCol[i].Value;                 }             }             pvtSqlCom.CommandText = strSql.ToString();

SqlDataAdapter daResult = new SqlDataAdapter(pvtSqlCom);             DataSet dsResult = new DataSet();             daResult.Fill(dsResult);             pvtSqlCon.Close();             return Convert.ToInt32(dsResult.Tables[0].Rows[0][0]);         }     }

//when update or insert, call this method     public void ExecProcNoneQuery(string strProcName, SqlParameterCollection sqlParamCol)     {         using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;

pvtSqlCom.CommandType = CommandType.StoredProcedure;             pvtSqlCom.CommandText = strProcName;             for (int i = 0; i < sqlParamCol.Count; i++)             {                 pvtSqlCom.Parameters.Add(sqlParamCol[i].ParameterName, sqlParamCol[i].SqlDbType, sqlParamCol[i].Size, sqlParamCol[i].SourceColumn);                 pvtSqlCom.Parameters[i].Value = sqlParamCol[i].Value;                 pvtSqlCom.Parameters[i].Direction = sqlParamCol[i].Direction;             }             pvtSqlCom.ExecuteNonQuery();             for (int i = 0; i < sqlParamCol.Count; i++)             {                 sqlParamCol[i].Value = pvtSqlCom.Parameters[i].Value;             }             pvtSqlCon.Close();         }     }

//when wanna to get a data result, call this method     public DataSet ExecProcReader(string strProcName, SqlParameterCollection sqlParamCol)     {

using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;

pvtSqlCom.CommandType = CommandType.StoredProcedure;             pvtSqlCom.CommandText = strProcName;             for (int i = 0; i < sqlParamCol.Count; i++)             {                 pvtSqlCom.Parameters.Add(sqlParamCol[i].ParameterName, sqlParamCol[i].SqlDbType, sqlParamCol[i].Size, sqlParamCol[i].SourceColumn);                 pvtSqlCom.Parameters[i].Value = sqlParamCol[i].Value;                 pvtSqlCom.Parameters[i].Direction = sqlParamCol[i].Direction;             }             SqlDataAdapter daResult = new SqlDataAdapter(pvtSqlCom);             DataSet dsResult = new DataSet();             daResult.Fill(dsResult);             for (int i = 0; i < sqlParamCol.Count; i++)             {                 sqlParamCol[i].Value = pvtSqlCom.Parameters[i].Value;             }             pvtSqlCon.Close();             return dsResult;         }     }

//when wanna to use sql to get a data result, call this method     public DataSet ExecSqlReader(string strSql, SqlParameterCollection sqlParamCol)     {         using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;

pvtSqlCom.CommandType = CommandType.Text;             pvtSqlCom.CommandText = strSql;             for (int i = 0; i < sqlParamCol.Count; i++)             {                 pvtSqlCom.Parameters.Add(sqlParamCol[i].ParameterName, sqlParamCol[i].SqlDbType, sqlParamCol[i].Size, sqlParamCol[i].SourceColumn);                 pvtSqlCom.Parameters[i].Value = sqlParamCol[i].Value;                 pvtSqlCom.Parameters[i].Direction = sqlParamCol[i].Direction;             }             SqlDataAdapter daResult = new SqlDataAdapter(pvtSqlCom);             DataSet dsResult = new DataSet();             daResult.Fill(dsResult);             for (int i = 0; i < sqlParamCol.Count; i++)             {                 sqlParamCol[i].Value = pvtSqlCom.Parameters[i].Value;             }             pvtSqlCom.Parameters.Clear();             pvtSqlCon.Close();             return dsResult;         }     }     //============================================================     /// <summary>     /// 查询SQL语句时用该方法,返回DataSet     /// </summary>     /// <param name="strSql"></param>     /// <returns></returns>     public DataSet ExecSqlQuery(string strSql)     {         using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;

pvtSqlCom.CommandType = CommandType.Text;             pvtSqlCom.CommandText = strSql;             SqlDataAdapter daResult = new SqlDataAdapter(pvtSqlCom);             DataSet dsResult = new DataSet();             daResult.Fill(dsResult);             pvtSqlCon.Close();             return dsResult;         }     }     //============================================================     /// <summary>     /// 处理SQL语句时,用该方法,返回处理结果,大于0:成功;小于0:失败;     /// </summary>     /// <param name="strSql"></param>     /// <returns></returns>     public int ExecSqlHandel(string strSql)     {         strSql = strSql + ";SELECT SCOPE_IDENTITY()";         using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;

pvtSqlCom.CommandType = CommandType.Text;             pvtSqlCom.CommandText = strSql;             string sResult = pvtSqlCom.ExecuteScalar().ToString();             pvtSqlCon.Close();             int nResult = 0;             if (!sResult.Equals(""))             {                 nResult = int.Parse(sResult);             }             return nResult;         }     }     public int ExecSqlHandel(string strSql, SqlParameterCollection sqlParamCol)     {         strSql = strSql + ";SELECT SCOPE_IDENTITY()";         using (SqlConnection pvtSqlCon = new SqlConnection(pvtStrSqlCon))         {             pvtSqlCon.Open();             pvtSqlCom.Connection = pvtSqlCon;             pvtSqlCom.CommandType = CommandType.Text;             pvtSqlCom.CommandText = strSql;             for (int i = 0; i < sqlParamCol.Count; i++)             {                 pvtSqlCom.Parameters.Add(sqlParamCol[i].ParameterName, sqlParamCol[i].SqlDbType, sqlParamCol[i].Size, sqlParamCol[i].SourceColumn);                 pvtSqlCom.Parameters[i].Value = sqlParamCol[i].Value;                 pvtSqlCom.Parameters[i].Direction = sqlParamCol[i].Direction;             }             string sResult = pvtSqlCom.ExecuteScalar().ToString();             pvtSqlCom.Parameters.Clear();             pvtSqlCon.Close();             int nResult = string.IsNullOrWhiteSpace(sResult) ? 0 : Convert.ToInt32(sResult);             return nResult;         }     }

internal DataSet ExecSqlQuery(StringBuilder sbSql)     {         throw new NotImplementedException();     } }

webconfig连接数据库配置的更多相关文章

  1. [置顶] c# asp.net 修改webconfig文件 配置

    c# asp.net 修改webconfig文件 配置 #region 修改config文件 /// <summary> /// 修改config文件(AppSetting节点) /// ...

  2. log4net保存到数据库系列一:WebConfig中配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志 一.WebConfig中配置log4net 二.独立配置文件中配置log4net ...

  3. LinqHelper连接数据库配置

    LinqHelper连接数据库配置/// <summary> /// Linq通用数据访问类 /// 指定TDataBase来代替后面要使用的数据上下文(指代) /// where:说明指 ...

  4. TP【连接数据库配置及Model数据模型层】

    [连接数据库配置及Model数据模型层] convertion.php config.php 在config.php做数据库连接配置 制作model模型 a) model本身就是一个类文件 b) 数据 ...

  5. 2016/05/10 thinkphp 3.2.2 ①系统常量信息 ②跨控制器调用 ③连接数据库配置及Model数据模型层 ④数据查询

    [系统常量信息] 获取系统常量信息: 如果加参数true,会分组显示: 显示如下: [跨控制器调用] 一个控制器在执行的时候,可以实例化另外一个控制,并通过对象访问其指定方法. 跨控制器调用可以节省我 ...

  6. 虚拟目录webconfig的配置

    昨天需要新建一个虚拟目录放在以前的一个站点下,新建了应用池,配好了环境置顶路径,虚拟目录页建立成功 ,但是程序一直是报错.这个程序我在测试服务器上是测过的,新建了一个站点是可以正常访问的,排除了程序问 ...

  7. .net学习笔记----WebConfig常用配置节点介绍

    一.配置文件入门 .Net提供了一种保存项目配置信息的办法,就是利用配置文件,配置文件的后缀一般是.config.在WinForm程序中配置文件一般是App.config.在Asp.net中一般默认是 ...

  8. webconfig的配置解析

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  9. 数据源连接数据库配置相关xml文件

    学完数据源连接数据后,做个笔记,当我们的程序对数据库访问频繁时,为了提高程序运行效率,我们可以通过 数据源连接数据库,从数据库连接池中直接取得出于空闲状态的数据库连接对象,以下是相关xml文件的配置: ...

随机推荐

  1. 2.HelloWorld程序

    1.流程图 2./itcast0711/src/main/java/cn/itcast/a_helloworld/HelloWorld.java package cn.itcast.a_hellowo ...

  2. neutron VPC

    The goal of this document is to provide an umbrella blueprint defining how to add support for VPC in ...

  3. C++指向函数的指针

    直接上代码: #include<iostream> #include<string> #include<vector> using namespace std; t ...

  4. 分享知识-快乐自己:JS 检查元素是否含有某种css样式

    第一种 原生 JS: ************************************************************* 结构部分: <div> <p> ...

  5. Java企业微信开发_07_JSSDK多图上传

    一.本节要点 1.1可信域名 所有的JS接口只能在企业微信应用的可信域名下调用(包括子域名),可在企业微信的管理后台“我的应用”里设置应用可信域名.这个域名必须要通过ICP备案,不然jssdk会配置失 ...

  6. NYOJ--42--dfs水过||并查集+欧拉通路--一笔画问题

    dfs水过: /* Name: NYOJ--42--一笔画问题 Author: shen_渊 Date: 18/04/17 15:22 Description: 这个题用并查集做,更好.在练搜索,试试 ...

  7. nginx uwsgi wsgi django 这些东西究竟是什么关系

    有太多的文章告诉我们nginx uwsgi django 这些东西怎么用了,太多的人知道这些东西的怎么使用,怎么配置,怎么优化,但是还是有一部分人比如我这种水货不知道这些东西到底是啥,为啥一个项目的发 ...

  8. JSONP -- 跨域数据交互协议

    一.概念 ①传统Ajax:交互的数据格式——自定义字符串或XML描述: 跨域——通过服务器端代理解决. ②如今最优方案:使用JSON格式来传输数据,使用JSONP来跨域. ③JSON:一种数据交换格式 ...

  9. 制作spark镜像

    构建镜像 添加jdk引用(可以使用yum进行安装): 安装SSH 碰到一个问题,执行systemctl的时候发生了异常: Failed to get D-Bus connection 解决这个问题的方 ...

  10. 51nod 1301 集合异或和——异或dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1301 好题!看了TJ才会. 因为是不可重集合,所以当然有前 i 个 ...