2011-01-09 69 views
0

我有多年的ASP編程經驗,但我是ASP.NET編程的新手。連接到ASP.NET與經典ASP的數據庫

我在看一個網站的代碼,我注意到編程這個網站的程序員與數據庫建立了兩個連接。一個在Web.config中,另一個在ASPX頁面中。

Web.config中有這樣的:

<connectionStrings> 
    <add name="pearl" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Hosting\7195242\html\db\xxx.mdb" providerName="System.Data.OleDb" /> 
</connectionStrings> 

ASPX頁面有這樣的:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="D:\Hosting\7195242\html\db\xxx.mdb" 
    SelectCommand="SELECT * FROM [Pearl_PageContents]"> 
</asp:AccessDataSource> 

有了ASP,我通常會創建一個名爲connection.asp一個ASP文件,添加我的數據庫連接碼此文件並將其附加到與數據庫交互的所有其他ASP頁面。爲什麼它與ASP.NET不同?

回答

1

第一個不是連接。這只是連接字符串的聲明。其他一些代碼將使用連接字符串連接到數據庫,可能使用的代碼有點類似於您用於ADODB的代碼。

第二個是使用數據源控件。這是一個可以將數據提供給另一個控件的控件。 ASP中沒有任何東西存在。它允許純粹聲明的Web表單,您可以在一個控件中聲明數據,並使FormView或GridView控件使用該數據,根本不需要額外的代碼即可執行CRUD操作。

我建議你從http://www.asp.net的教程開始。

+0

第二個(在aspx頁面中)很可能是拖放設計的結果。 – 2011-01-09 07:09:55

0

Web.config只是讓您可以存儲全局變量。在技​​術上,創建另一個類並在該cs文件中保存相同的數據沒有任何問題。但是,使用web.config可以輕鬆編輯該連接,爲該文件添加更多連接字符串,然後使用ConnectionManager獲取任何這些連接字符串。你也可以加密你的web.config文件。

0
using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 

namespace Volta_Reporting_Application.DBL 
{ 
    public class DBHandler 
    { 
     public SqlConnection _SqlConnection { get; set; } 
     public String _SqlConnectionString { get; set; } 
     public DataSet _DataSet { get; set; } 
     public List<SqlCommand> _CommandList { set; get; } 
     public DBHandler() 
     { 
      //_SqlConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; ; 
      _SqlConnectionString = Helpers.Helper.GetConnectionString(); 
     } 
     public bool OpenConnection() 
     { 

       _SqlConnection = new SqlConnection(_SqlConnectionString); 
      if (SqlConnection != null && SqlConnection.State == ConnectionState.Closed) 
      { 

       _SqlConnection.Open(); 
      } 
    .Open); 
     } 

     public bool CloseConnection() 
     { 
      if (SqlConnection != null && SqlConnection.State == ConnectionState.Open) 
       _SqlConnection.Close(); 
      if (_SqlConnection != null) 
       _SqlConnection.Dispose(); 
      return _SqlConnection == null; 
     } 
     public object ExecuteMyCommand(SqlCommand cmd) 
     { 
      bool res = false; 
      try 
      { 
       OpenConnection(); 
       cmd.Connection = _SqlConnection; 
       if (cmd.ExecuteNonQuery() > 0) 
       { 
        res = true; 
       } 
      } 
      catch (Exception) 
      { 

       res = false; 
      } 
      finally 
      { 
       CloseConnection(); 
      } 
      return res; 
     } 

     public object CRUD(string Query, char operation = 'c') 
     { 
      operation = char.Parse(operation.ToString().ToLower()); 
      object res = null; 
      try 
      { 
       OpenConnection(); 
       SqlDataAdapter da = new SqlDataAdapter(); 
       switch (operation) 
       { 
        case 'c': 
        case 'i': 
         da.InsertCommand = _SqlConnection.CreateCommand(); 
         da.InsertCommand.CommandText = Query; 
         da.InsertCommand.ExecuteNonQuery(); 
         res = true; 
         break; 
        case 'z': 
         da.SelectCommand = _SqlConnection.CreateCommand(); 
         da.SelectCommand.CommandText = Query; 
         return da.SelectCommand.ExecuteScalar(); 
        case 's': 
        case 'r': 
         DataSet ds = new DataSet(); 
         da.SelectCommand = _SqlConnection.CreateCommand(); 
         da.SelectCommand.CommandText = Query; 
         //da.SelectCommand.ExecuteReader(); 
         da.Fill(ds); 
         res = ds; 
         //ds.Dispose(); 
         break; 
        case 'u': 
         da.UpdateCommand = _SqlConnection.CreateCommand(); 
         da.UpdateCommand.CommandText = Query; 
         res=da.UpdateCommand.ExecuteNonQuery(); 
         break; 
        case 'd': 
         da.DeleteCommand = _SqlConnection.CreateCommand(); 
         da.DeleteCommand.CommandText = Query; 
         da.DeleteCommand.ExecuteNonQuery(); 
         res = true; 
         break; 
        default: break; 
       } 
       if (res == null) 
        res = false; 
      } 
      catch (Exception) 
      { 
       res = null; 
      } 
      finally 
      { 
       CloseConnection(); 
      } 
      return res; 
     } 
    } 
}