C#(ASP.NET)中连接数据库的字符串(连接Access和sqlserver数据库以及用windows和身份验证两种方式)

时间:2021-09-12 13:39:55

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 2005SQL Server 2000在数据库访问上面是通用的,关键在于连接字符串的配置,其实这两种数据的接串是可以通用的,关键是数据器地址的问题SQL Server 20052000数据认访问端口不一200523172000的是1433。下面四个接串都可以访问2000
       1Server=myServerAddress; Database=DatabaseName; Uid=UserName; Pwd=Password;
       2Server=myServerAddress , 1433; Database=DatabaseName; Uid= UserName; Pwd=Password;
       3Data Source=myServerAddress; Initial Catalog=DatabaseName; User ID=UserName; pwd=Password;
       4Data Source=myServerAddress,1433; Initial Catalog=DatabaseName; User ID=UserName; pwd=Password;

      myServerAddress是数据器地址,当您只写地址不写端口的情况下他认访问的是1433端口,也就是sql server 2000的数据器。如果你想访问sql server 2005你就必加上访问端口(,2317)或者在后面添加(\SQLExpress),如下面
       1Server=myServerAddress,2317; Database=DatabaseName; Uid= UserName; Pwd=Password;
       2Server=myServerAddress\SQLExpress; Database=DatabaseName; Uid=UserName; Pwd=Password;
       3Data Source=myServerAddress,2317; Initial Catalog=DatabaseName; User ID=UserName; pwd=Password;
       4Data Source=myServerAddress\SQLExpress; Initial Catalog= DatabaseName; User ID=UserName; pwd=Password;