源代码:13033480群共享
ASP.NET使用ADO.NET连接数据库,ADO.NET的程序集名称是System.Data.dll,内有4个命名空间:System.Data.SqlClient、System.Data.OleDb、System.Data.Odbc和System.Data.OracleClient。每一个命名空间都包含Connection、Command、DataReader和DataAdapter等类,我们就使用这些类建立与SQL、Access、Excel数据库的连接。
我们添加五个窗体文件,分别使用SQLServer.NET连接SQL数据库、使用OleDb.NET连接SQL数据库、使用OleDb.NET连接Excel数据库、使用OleDb.NET连接Access数据库、使用Odbc.NET连接Excel数据库。
因为要使用ConfigurationManager读取Web.config文件中的连接字符串,所以,网站Web中要添加引用System.Configuration,并在aspx.cx文件中添加using System.Configuration。
一、SQLServerToSQLDatabase.aspx.cs
连接字符串:
<addname="SQLServerToSQLString"connectionString="Server=.\SQLEXPRESS;Database=NetShop;Trusted_Connection=yes;"providerName="System.Data.SqlClient"/>
代码:
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public partialclassSQLServerToSQLDatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connectionString =ConfigurationManager.ConnectionStrings["SQLServerToSQLString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Category";
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Response.Write(rdr["Name"] +"<br/>");
}
rdr.Close();
conn.Close();
}
}
二、OleDbToSQLDatabase.aspx.cs
连接字符串:
<addname="OleDbToSQLString"connectionString="Provider=SQLOLEDB;Server=.\SQLEXPRESS;Database=NetShop;Trusted_Connection=yes;"providerName="System.Data.OleDb"/>
代码:
using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
public partialclassOleDbToSQLDatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connectionString =ConfigurationManager.ConnectionStrings["OleDbToSQLString"].ConnectionString;
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Category";
conn.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Response.Write(rdr["Name"] +"<br/>");
}
rdr.Close();
conn.Close();
}
}
三、OleDbToExcelDatabase.aspx.cs
连接字符串:
<addname="OleDbToExcelString"connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\NetShop.xls;extended properties=excel 8.0;"providerName="System.Data.OleDb"/>
代码:
using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
public partialclassOleDbToExcelDatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connectionString =ConfigurationManager.ConnectionStrings["OleDbToExcelString"].ConnectionString;
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Category";
conn.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Response.Write(rdr["Name"] +"<br/>");
}
rdr.Close();
conn.Close();
}
}
四、OleDbToAccessDatabase.aspx.cs
连接字符串:
<addname="OleDbToAccessString"connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\NetShop.mdb"providerName="System.Data.OleDb"/>
代码:
using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
public partialclassOleDbToAccessDatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connectionString =ConfigurationManager.ConnectionStrings["OleDbToAccessString"].ConnectionString;
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Category";
conn.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Response.Write(rdr["Name"] +"<br/>");
}
rdr.Close();
conn.Close();
}
}
五、OdbcToExcelDatabase.aspx.cs
连接字符串:
<addname="OdbcToExcelString"connectionString="Driver={Microsoft Excel Driver (*.xls)};DBQ=|DataDirectory|\NetShop.xls"providerName="System.Data.Odbc"/>
代码:
using System;
using System.Data;
using System.Configuration;
using System.Data.Odbc;
public partialclassOdbcToExcelDatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connectionString =ConfigurationManager.ConnectionStrings["OdbcToExcelString"].ConnectionString;
OdbcConnection conn = new OdbcConnection(connectionString);
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Category";
conn.Open();
OdbcDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Response.Write(rdr["Name"] +"<br/>");
}
rdr.Close();
conn.Close();
}
}
六、分别运行5个窗体文件,查看结果。
注意:
1、两个Data Source不有换成Database,DBQ也不能;
2、使用OleDb连接SQL数据库,Trusted_Connection=yes只能是yes,不能是true或SSPI;
3、providerName="System.Data.SqlClient"等providerName属性只起说明作用,可以不要。
4、从五个程序的代码可以看出ADO.NET的一个主要优点,那就是:
提供了对诸如 SQL Server 和 XML 这样的数据源以及通过 OLE DB和 ODBC公开的数据源的一致访问