1. <appSettings>
<add key= "dsn " value= "Data Source=localhost;UID=sa;PWD=;DATABASE=SMS "/>
</appSettings>
2. <connectionStrings>
<add name = "ConnectionStr1 " connectionString= "Server=(local); Database=aspnetdb; User ID=sa; Password= " />
</connectionStrings>
3.(从一个连接Access数据库例子中拷出来的) <sessionState
mode= "InProc "
stateConnectionString= "tcpip=127.0.0.1:42424 "
sqlConnectionString= "data source=127.0.0.1;Trusted_Connection=yes "
cookieless= "false "
timeout= "20 "
/>
一、连接数据库的几种写法
1.server=数据库服务器地址;database=数据库名称;uid=登录数据库名称;pwd=登录数据库密码;
2.DataSource=数据库服务器地址;database=数据库名称;uid=登录数据库名称;pwd=登录数据库密码;
3.Data Source=数据库服务器地址;Initial Catalog=数据库名称;Persist Security Info=True;User ID=登录数据库名称;password=登录数密码;
二、调用或使用连接串
1.直接将连接保存在某一字段中,进行获取
public string GetConn{
Get{ return “连接串";}
}
2.将连接配置在web.Config里,再进行获取
2.1配置web.config,在ConnectionStrings标签中添加add标签,再给add标签添加如下属性
<connectionStrings>
<add name="ConnectionStr" connectionString="server=127.0.0.1;database=JH_Goods;uid=sa;pwd=sa;"/>
</connectionStrings>
2.2:在后台获取web.Config里的连接串
public string GetConn = ConfigurationManager.ConnectionStrings["webconfig配置名称"].toString();
例:在webconfig 中的设定格式
<appSettings>
<!--连接字符串-->
<add key="Connection" value="Server=kc;database=kehucrm;uid=sa;pwd=ab-0987"/>
<!-- <add key= "Connection" value= "Data Source=localhost;UID=sa;PWD=****;DATABASE=kehucrm "/> -->
<!--连接字符串
//connectionString="Data Source=LIFAN;DataBase=NorthWind;User ID=**;Password=**;Provider=SQLOLEDB" name="connectionString"
///<add connectionString="Data Source=LIFAN;DataBase=NorthWind;User ID=**;Password=**;Provider=SQLOLEDB" name="connectionString"/>
-->
</appSettings>
<connectionStrings>
<add connectionString="Data Source=kc;DataBase=kehucrm;User ID=sa;Password=****;" name="connectionString" />
</connectionStrings>
// AppSettings key调用方法
string sql = "select * from laiyuan ";
SqlConnection Sqlcon = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
Sqlcon.Open();
SqlCommand SqlCom = new SqlCommand(sql, Sqlcon);
SqlDataReader dr = SqlCom.ExecuteReader();
if (dr.Read() )
{
tbUsername.Text = dr["fuli1"].ToString().Trim();
// tbUsername.Text = dr.GetValue(1).ToString();
}
else
{
tbUsername.Text = "kongzhi";
}
// connectionStrings调用方法
string sql = "select * from laiyuan ";
string GetConn = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
SqlConnection sqlconn = new SqlConnection(GetConn);
SqlDataAdapter sqldr=new SqlDataAdapter (sql,sqlconn);
DataSet ds=new DataSet("laiyuan");
sqldr.Fill(ds);
gridv1.DataSource = ds; gridv1.DataBind();