1.c#本地MDB数据库连接(数据库在该项目的Bin——>Debug——>DB文件夹下)
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"/DB/数据库名称;Persist Security Info=False");(Access2003版本)
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + @"/DB/数据库名称;Persist Security Info=False");(Access2010版本)
2.c#本地SQL数据库连接
string strCon = "Integrated Security=SSPI;Initial Catalog='数据库名称';Data Source='本地计算机名';User ID=
'登录用户名';Password='登录用户密码';Connect Timeout=30";
SqlConnection myConn = new SqlConnection ( strCon ) ;
c#远程SQL数据库连接
string strCon = "Initial Catalog='数据库名称';Server='远程IP地址,1433';User ID='登录用户名';Password='登录用户码';
Persist Security Info=True";
SqlConnection myConn = new SqlConnection ( strCon ) ;
3.web.config文件中数据库连接配置(数据库名称:Student;本地计算机名称:./SQLEXPRESS;用户名:sa;密码:sa)
<database>
<dataSource name="Student" connectionString="data source=./SQLEXPRESS;database=Student;user id=sa;password=sa;connection reset=false;connection lifetime=5; min pool size=1; max pool size=50"/>
</database>
<appSettings>
<add key="ConnectionString" value="workstation id=localhost;user id=sa;password=sa;data source=./SQLEXPRESS;persist security info=False;initial catalog=Student"/>
</appSettings>
<connectionStrings>
<add name="" connectionString="server=./SQLEXPRESS;uid=sa;pwd=sa;database=Student"/>
</connectionStrings>
4.以windows身份登录时:
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>
以sql server身份验证登录时:
微软的SQL Server 2005和SQL Server 2000在数据库访问上面是通用的,关键在于连接字符串的配置,其实这两种数据的连接串是可以通用的,关键是数据库服务器地址的问题。SQL Server 2005和2000数据库默认访问端口不一样。2005是2317,2000的是1433。下面四个连接串都可以访问2000。
1、Server=myServerAddress; Database=DatabaseName; Uid=UserName; Pwd=Password;
2、Server=myServerAddress , 1433; Database=DatabaseName; Uid= UserName; Pwd=Password;
3、Data Source=myServerAddress; Initial Catalog=DatabaseName; User ID=UserName; pwd=Password;
4、Data Source=myServerAddress,1433; Initial Catalog=DatabaseName; User ID=UserName; pwd=Password;
myServerAddress是数据库服务器地址,当您只写地址不写端口的情况下他们默认访问的是1433端口,也就是sql server 2000的数据库服务器。如果你想访问sql server 2005你就必须加上访问端口(,2317)或者在后面添加(\SQLExpress),如下面
1、Server=myServerAddress,2317; Database=DatabaseName; Uid= UserName; Pwd=Password;
2、Server=myServerAddress\SQLExpress; Database=DatabaseName; Uid=UserName; Pwd=Password;
3、Data Source=myServerAddress,2317; Initial Catalog=DatabaseName; User ID=UserName; pwd=Password;
4、Data Source=myServerAddress\SQLExpress; Initial Catalog= DatabaseName; User ID=UserName; pwd=Password;