ASP.NET连接SQL、Access、Excel数据库(二)——连接实例

 源代码:13033480群共享

ASP.NET使用ADO.NET连接数据库,ADO.NET的程序集名称是System.Data.dll,内有4个命名空间:System.Data.SqlClient、System.Data.OleDb、System.Data.Odbc和System.Data.OracleClient。每一个命名空间都包含ConnectionCommandDataReaderDataAdapter等类,我们就使用这些类建立与SQLAccessExcel数据库的连接。

我们添加五个窗体文件,分别使用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不有换成DatabaseDBQ也不能;

2、使用OleDb连接SQL数据库,Trusted_Connection=yes只能是yes,不能是trueSSPI;

3providerName="System.Data.SqlClient"providerName属性只起说明作用,可以不要。

4、从五个程序的代码可以看出ADO.NET的一个主要优点,那就是:

提供了对诸如 SQL Server XML 这样的数据源以及通过 OLE DB ODBC公开的数据源的一致访问

版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客http://blog.csdn.com/yousuosi

posted on 2012-04-23 18:07  WestGarden  阅读(245)  评论(0编辑  收藏  举报

导航