1.使用ODBC连接数据源
控制面板→管理工具→数据源ODBC→系统DSN→添加→Drive do Microsoft Access(*.mdb)→选择(example.mdb)→完成
Web.config中:
<connectionStrings>
<add name="connection" connectionString="DSN=example" />
</connectionStrings>
Page_Load中:
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
2.SQL Server数据源
Web.config中:
<connectionStrings>
<add name="connectionsqlserver" connectionString="server=(local);database=Northwind;uid=sa;pwd=sa" />
</connectionStrings>
Page_Load中:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
con.ConnectionTimeout.ToString(); //是超时时间
3.OLE DB数据源(Access)
Web.config中:
<connectionStrings>
<add name="connectionoledb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;data source=D:\example.mdb" />
</connectionStrings>
Page_Load中:
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connectionoledb"].ConnectionString);
4.Oracle数据源
Oracle的托管提供程序不是.NET默认,要引入动态链接库:
网站→添加引用→.NET→System.Data.OracleClient
Web.config中:
<connectionStrings>
<add name="connectionoracle" connectionString="data source=oraclename;uid=sa;pwd=sa" />
</connectionStrings>
Page_Load中:
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["connectionoracle"].ConnectionString);
5.SqlDataSource和AccessDataSource控件数据源
SqlDataSource控件提供了3种身份验证模式:Windows模式、SQL Server模式、混合验证模式
Windows模式Web.config中:
<connectionStrings>
<add name="connection" connectionString="Data Source=.;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
SQL Server模式Web.config中:
<connectionStrings>
<add name="connection" connectionString="Data Source=.;Initial Catalog=pubs;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
</connectionStrings>
AccessDataSource类似SqlDataSource所以略
控制面板→管理工具→数据源ODBC→系统DSN→添加→Drive do Microsoft Access(*.mdb)→选择(example.mdb)→完成
Web.config中:
<connectionStrings>
<add name="connection" connectionString="DSN=example" />
</connectionStrings>
Page_Load中:
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
2.SQL Server数据源
Web.config中:
<connectionStrings>
<add name="connectionsqlserver" connectionString="server=(local);database=Northwind;uid=sa;pwd=sa" />
</connectionStrings>
Page_Load中:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
con.ConnectionTimeout.ToString(); //是超时时间
3.OLE DB数据源(Access)
Web.config中:
<connectionStrings>
<add name="connectionoledb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;data source=D:\example.mdb" />
</connectionStrings>
Page_Load中:
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connectionoledb"].ConnectionString);
4.Oracle数据源
Oracle的托管提供程序不是.NET默认,要引入动态链接库:
网站→添加引用→.NET→System.Data.OracleClient
Web.config中:
<connectionStrings>
<add name="connectionoracle" connectionString="data source=oraclename;uid=sa;pwd=sa" />
</connectionStrings>
Page_Load中:
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["connectionoracle"].ConnectionString);
5.SqlDataSource和AccessDataSource控件数据源
SqlDataSource控件提供了3种身份验证模式:Windows模式、SQL Server模式、混合验证模式
Windows模式Web.config中:
<connectionStrings>
<add name="connection" connectionString="Data Source=.;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
SQL Server模式Web.config中:
<connectionStrings>
<add name="connection" connectionString="Data Source=.;Initial Catalog=pubs;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
</connectionStrings>
AccessDataSource类似SqlDataSource所以略