ADO.NET与Sql Server和Access的连接
1:Access :
using System.Data.OleDb; // 用于Access
string ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"cus.mdb");
2:Sql:
using System.Data.SqlClient; //用于SqlConnection
string ConnStr = "server=servername;uid=username;pwd=password;database=dbname";
string connstring="Data Source=servername;uid=username;pwd=password;Initial Catalog=dbname";
举例: 注意可能需要使用到转义字符
string ConnStr = @"server=WWW-L16588GREB3\SHILI2008;uid=sa;pwd=tiantang;database=Student";
string ConnStr = @"Data Source=WWW-L16588GREB3\SHILI2008;uid=sa;pwd=tiantang;Initial Catalog=Student";
或者在web.config中书写 :
<connectionStrings> <add name="ConnStr" connectionString="server=WWW-L16588GREB3\SHILI2008;uid=sa;pwd=tiantang;database=student"/> </connectionStrings>然后在aspx中如下using System.Configuration; //用于web.config配置文件string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
其实有时候 有些语句是相同的含义: