次方法为读取程序配置文件的内容(winexx.config)<appSettings>
<!-- for information on setting the values for the connection strings see the configTool project -->
<add key="p_ConnStringSetName" value="User ID=sa;pwd=;Initial Catalog=ECoupon;Data Source=localhost;" />
</appSettings>
<!-- for information on setting the values for the connection strings see the configTool project -->
<add key="p_ConnStringSetName" value="User ID=sa;pwd=;Initial Catalog=ECoupon;Data Source=localhost;" />
</appSettings>
解决方案 »
- xp sp3 iis5.1 .net framework4.0 无法浏览aspx网页
- 如何设置打印范围中指页为真
- 一个关于List.Sort()方法的疑问——用List.Sort()方法排序结果不正确!!
- 如果在类库项目中配置log4net,要达到这样的要求。
- 帮我用C#写一个程序?
- 吐血求救!vb.net转c#,给推荐一本c#入门书!
- 如何获得某一时间的小时数,分钟数和秒数?
- 一个xsd有两张表时,怎么区分两张表内取名一样的字段?
- !!急急急急!!急急急!!socket文件传输莫名其妙问题,高分求救(100分)高手一句话能搞定!!!我认为可能是个小问题!!!!
- 再次提问~
- 简单问题,在线等待!C#中string 类型转为int 类型如何转?
- 怎样截获水晶报表的print事件?
using System;
namespace com.joybase.DB
{
/// <summary>
/// 数据库连接提供类
/// </summary>
public class Provider
{
//一个静态的连接接口;
private static System.Data.IDbConnection conn;
/// <summary>
/// 构造方法,设为私有类型,是防止外部无效的引用;
/// </summary>
protected Provider()
{
//
// TODO: Add constructor logic here
//
System.Data.IDbCommand x=new System.Data.OleDb.OleDbCommand(); }
// /// <summary>
// /// 数据库连接类型,即判断是System.Data.SqlClient类型或者是System.Data.OleDB类型的。
// /// </summary>
// private static DBType DataBaseType
// {
// get
// {
// //从配置文件当中读取数据库类型字符串
// string m_DBType=System.Configuration.ConfigurationSettings.AppSettings["DataBase.Type"];
// //如果未设置默认为System.Data.SqlClient类型的连接
// if(m_DBType==null)
// {
// return DBType.SqlClient;
// }
// else
// {
// //如果为空或者为MSSQLServer2000,则使用System.Data.SqlClient类型的连接
// if(m_DBType.Trim()==""||m_DBType.Trim().ToUpper()=="MSSQLSERVER2000"||m_DBType.Trim().ToUpper()=="MSSQLSERVER7")
// {
// return DBType.SqlClient;
// }
// //其它则返回System.Data.OleDB类型的连接
// else
// {
// if(m_DBType.Trim().ToUpper()=="OLEDB")
// {
// return DBType.OleDB;
// }
// }
//
// }
// return DBType.OleDB;
//
// }
//
//
// }
/// <summary>
/// 重载getConn(string)方法,此时连接字符串的标签名将为“DataBase.ConnectionString”
/// <seealso cref="getConn(string) "/>
/// </summary>
/// <returns>返回一个连接</returns>
public static System.Data.IDbConnection getConn()
{
return Provider.getConn("");
}
/// <summary>
/// 获得数据库连接接口
/// </summary>
/// <param name="p_ConnStringSetName">一个在Config文件中设置连接字符串的标签名</param>
/// <returns></returns>
public static System.Data.IDbConnection getConn(string p_ConnStringSetName)
{// if(conn==null)
// {
string ConnStr="";
if(p_ConnStringSetName.Trim()=="")
{
ConnStr=System.Configuration.ConfigurationSettings.AppSettings["DataBase.ConnectionString"];
}
else
{
ConnStr=System.Configuration.ConfigurationSettings.AppSettings[p_ConnStringSetName];
}
if(ConnStr==null||ConnStr=="")
{
throw new Exception("Not find connection string!");
}
DBType m_DBType;//=Provider.DataBaseType;
/*
* 注释:我们对前面的编码进行了部分的修改,鉴于System.Data.SqlClient的连接
* 字符串当中不可能出现"Provider"字样,所以我们根据是否有Provider字样来判断
* 该连接是基于System.Data.SqlClient的或者System.Data.OleDB的。
* 参考资料:
* 可以将 ConnectionString 属性设置为单个单元。(不能为 SqlConnection 对象指定 Provider 属性。)
* –或–
*
* 可以设置单个属性(DataSource、Database、UserName 等等)。如果设置单个属性,则将为您生成连接字符串。
* 注意 在连接字符串中存储用户名和密码有安全性设置的意味。有关详细信息,请参阅Introduction to ADO.NET Connection Design Tools。
*
*/
if(ConnStr.ToLower().IndexOf("provider")==-1) m_DBType=DBType.SqlClient;
else m_DBType=DBType.OleDB;
try
{
if(m_DBType==DBType.SqlClient)
{
conn=new System.Data.SqlClient.SqlConnection(ConnStr);
}
else
{
conn=new System.Data.OleDb.OleDbConnection(ConnStr);
}
}
catch
{
throw new Exception("Error to connect DB!");
}
// }
//if(conn.State!=System.Data.ConnectionState.Closed) conn.Close();
return conn;}}
/// <summary>
/// 枚举类型,即一个数据库连接类型的枚举
/// </summary>
enum DBType
{
SqlClient=0,
OleDB=1
}
}
<appSettings>
<!-- 此处显示用户应用程序和配置的属性设置。-->
<!-- 示例:<add key="settingName" value="settingValue"/> -->
<add key="ConnXxwgl" value=" Provider = msdaora;Data Source =xxwgl.HFJ.AH.SP.COM.CN;User Id = xxwgl_sa;Password = xxwgl"/>
</appSettings>
在源程序的在加:using system.configuration;
that's allright!
<appSettings>
<add key="调用时的名字" value="连接字符串">
</appSettings>
</configuration>
上述代码在WebConfig中设置.
在代码中
using System.Configuration;
那么在连接数据库时就可以利用这个来传递连接字符串.
constr=ConfigurationSettings.AppSettings["在KEY中设置的名字"].Tostring();
这就是连接字符串
web.config中数据连接配置节如下:
<appSettings>
<!--数据库连接参数-->
<add key = "p_ConnStringSetName"
value = "Password=密码;User ID=sa;Initial Catalog=数据库名称;Data Source=IP地址" />
<add key = "DBtimeout"
value = "10" />
</appSettings>