ASP.NET編程習(xí)慣淺析
ASP.NET編程一、 錯誤(以外)的處理
程序健壯性最基本要求就是程序錯誤的處理與捕捉,在ASP.NET中,錯誤的處理有和其他編程語言一樣的機制,可以使用Try…Catch…Finally等方式,這一點和ASP相比具有較大的進步。而且,使用這些錯誤處理方法,可以大大提高程序的可讀性和程序調(diào)試速度,在這幾個優(yōu)勢結(jié)合的情況下,我們更加應(yīng)該注意這一點。
ASP.NET編程二、 字符串的處理
網(wǎng)頁設(shè)計中,字符串的處理幾乎是最常見的了。使用ASP.NET以后,字符串的處理比ASP的速度快,而且,在 ASP.NET中,專門增加一個字符串處理類StringBulider,使用這個類可以完成一些常見的字符串操作,而最主要的,使用 StringBuilder可以大大提高字符串處理速度。
在ASP.NET中,最常見的就是使用“&”來連接兩個字符串:
- Dim myOutputString As String = "My name is"
- Dim myInputString As String = " Alex"
- myOutputString = myOutputString & myInputString
- Response.Write(myoutputString)
現(xiàn)在,我們來看看StringBuilder的使用,在使用StringBuilder的時候,我們對字符串可以做一些基本的操作,比如Append、Replace、Insert、Remove等,現(xiàn)在我們來看具體舉例。
(1)StringBuilder中Append的使用
Append和其他語言的Append一樣,就是在字符串最后增加其他字符。
- Dim sb as StringBuilder = New StringBuilder()
- sb.append( "﹤table border='1' width='80%'﹥" )
- For i = 0 To﹤ RowCount - 1
- sb.Append("tr﹥")
- For k = 0 To ColCount - 1
- sb.Append("﹤td﹥")
- sb.Append( dt.Rows(i).Item(k, DataRowVersion.Current).toString())
- sb.Append( "﹤/td﹥" )
- Next
- sb.Append("﹤tr﹥")
- Next
- sb.Append( "﹤/table﹥")
- Dim strOutput as String = sb.ToString()
- lblCompany.Text = strOutput
在以上的程序中,用Append方法實現(xiàn)了一個表格的輸出,需要注意的一點是,StringBulider必須首先使用ToString()方法將其轉(zhuǎn)化為 String類型才可以直接輸出。在以上的舉例中,我們看到的全部是Append一個直接的字符串,其實,這個方法有一個很方便的功能,那就是可以直接 Append其他類型的變量,比如可以直接Appemd一個Integer類型的數(shù)值,當(dāng)然,我們輸出以后自動轉(zhuǎn)化為一個字符串:
- Sub Page_Load(Source As object, E As EventArgs)
- Dim sb As System.Text.StringBuilder
- Dim varother As Integer
- varother=9999
- sb =new System.Text.StringBuilder()
- sb.append("﹤font color='blue'﹥可以Append其他類型:﹤/font﹥")
- sb.append(varother)
- Response.write(sb.toString())
- End Sub
(2)字符串中其他方法的使用
我們還可以使用其他方法,我們來看看常見的:
Insert方法,可以在指定位置插入其他字符,使用方法:Insert(插入位置,插入字符);
Remove方法,可以在指定位置刪除指定字?jǐn)?shù)字符,使用方法:Remove(其實位置,字符數(shù));
Replace方法,可以替換指定字符,使用方法:replace(被替換字符串,替換字符串)
ASP.NET編程三、 數(shù)據(jù)庫鏈接Connection和DataReader的關(guān)閉
在使用ASP編程的時候,我們就已經(jīng)知道,在使用數(shù)據(jù)庫連接以后,一定要將連接關(guān)閉,然后設(shè)置為NoThing。在Asp.NET中,我們?nèi)匀恍枰@樣使用,不過,在ASP.NET中,由于使用了ADO.NET,所以,在一些相關(guān)的處理方面,實際還是有一些細(xì)微的區(qū)別,而這些區(qū)別,往往也就是我們設(shè)計的時候最需要注意的?,F(xiàn)在,我們通過舉例,來看看在常見的ADO.NET操作中,需要注意哪些問題。
(1)舉例一
- Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
- Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)
- Dim myDataReader As SqlDataReader
- Try
- myConnection.Open()
- myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
- dropDownList1.DataSource = myDataReader
- dropDownList1.DataBind()
- Catch myException As Exception
- Response.Write("An error has occurred: " & myException.ToString())
- Finally
- If Not myDataReader Is Nothing Then
- '關(guān)閉DataReader
- myDataReader.Close()
- End If
- End Try
在以上的舉例中,我們注意到,這里只關(guān)閉了DataReader,并沒有關(guān)閉Connection。為什么呢?仔細(xì)觀察以上的ExecuteReader方法,原來,設(shè)置了ExecuteReader參數(shù),當(dāng)執(zhí)行完ExecuteReader以后,會自動關(guān)閉Connection。所以,這樣設(shè)置以后,就沒有必要再手動關(guān)閉Connection了。
(2)舉例二
- Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
- Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)
- Try
- myConnection.Open()
- dropDownList1.DataSource = myCommand.ExecuteReader()
- dropDownList1.DataBind()
- Catch myException As Exception
- Response.Write("An error has occurred: " & myException.ToString())
- Finally
- If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then
- myConnection.Close()
- End If
- End Try
在以上的舉例中,我們發(fā)現(xiàn),居然沒有關(guān)閉DataReader。為什么呢?其實上面的代碼中,沒有直接生成DataReader對象,當(dāng)然也就無從關(guān)閉了。需要注意一點的是,在關(guān)閉Connection之前,程序首先判斷Connection是否已經(jīng)打開,如果沒有打開,也就沒必要關(guān)閉了。
ASP.NET編程四、使用Web.Config/Maching.Config保存常用數(shù)據(jù)
一些數(shù)據(jù)我們需要時常使用,比如使用ADO.NET的時候,最常見的就是數(shù)據(jù)庫連接語句,在ASP中,我們常常將這些信息保存在Application中。當(dāng)然,在ASP.NET中,也可以這樣,不過, ASP.NET已經(jīng)提供一個配置文件WEB.Config,所以,我們最好將這些信息保存在WEB.Config中,當(dāng)然,我們也可以保存在 Machine.Config中,不過,這樣的話,整個網(wǎng)站都必須使用,所以,一般我們都使用Web.Config?,F(xiàn)在,我們來看具體這個文件的使用。
(1)Web.Config文件的設(shè)置
首先,我們來看Web.Config的設(shè)置,我們在這個文件中增加設(shè)置以下兩個項目,設(shè)置如下:
- ﹤configuration﹥
- ﹤appsettings﹥
- ﹤add key="dsn" value="myserver"/﹥
- ﹤add key="someotherkey" value="somevalue"/﹥
- ﹤/appsettings﹥
- ﹤/configuration﹥
(2)變量的使用
以上XML文件設(shè)置了dsn和someotherkey兩個變量,現(xiàn)在我們看看程序中怎樣使用:
- ﹤html﹥
- ﹤script language="VB" runat=server﹥
- Sub Page_Load(Sender as object, E as EventArgs)
- Dim AppSettings as Hashtable = Context.GetConfig("appsettings")
- DSN.Text = AppSettings("dsn")
- SomeOther.Text = AppSettings("someotherkey")
- End Sub
- ﹤/script﹥
- ﹤body﹥
- DSN Setting: ﹤asp:label id="DSN" runat=server/﹥ ﹤br﹥
- Some Other Setting: ﹤asp:label id="SomeOther" runat=server/﹥
- ﹤/body﹥
- ﹤/html﹥
上面的程序我們看到,使用這樣定義的變量很簡單也很方便。
ASP.NET編程五、使用.NET的方式調(diào)試程序
ASP程序的調(diào)試一直是編寫ASP最難的地方,這一點,ASP程序員大概都深有體會,因為大家都是使用 Response.write來調(diào)試。而這樣調(diào)試最大的缺點是,當(dāng)我們調(diào)試完畢,必須一個個來刪除或者注釋掉這些信息,想一想,如果程序代碼達到幾百行或者頁面很多的程序,這樣的工作是多么枯燥,最怕一點,忘記將這些調(diào)試用的write刪除,可能在用戶使用的時候就會出現(xiàn)一些不雅的調(diào)試信息。
使用ASP.NET以后,我們可以直接定義Trace來實現(xiàn)程序的調(diào)試。以上提到的麻煩可以輕松解決,熟悉,Trace可以通過具體頁面和在 Web.Config配置文件中來定義實現(xiàn),這樣,當(dāng)程序調(diào)試完畢以后,直接將Trace設(shè)置為Off就可以了,這樣,程序就不會有調(diào)試功能了。
(1)頁面調(diào)試的實現(xiàn)
在一個具體的頁面需要實現(xiàn)調(diào)試功能的時候,我們可以這樣設(shè)置:
- ﹤%@ Page Language="VB" Trace="True" %﹥
(2)定義WEB.Config實現(xiàn)
在WEB.CONFIG中,我們也可以實現(xiàn)程序調(diào)試的打開:
- ﹤configuration﹥
- ﹤system.web﹥
- ﹤trace enabled="true" requestLimit="10" localOnly="false"/﹥
- ﹤/system.web﹥
- ﹤/configuration﹥
使用以上的設(shè)置打開Trace以后,我們在具體的程序中就可以使用Trace來調(diào)試程序了,比如:
- Trace.Write("This is some custom debugging information")
或者調(diào)試程序變量:
- Trace.Write("This is is my variable and it's value is:" & myVariable.ToString())
以上設(shè)置我們可以看出,在ASP.NET中,程序調(diào)試功能已經(jīng)很方便簡單了
ASP.NET編程的一些習(xí)慣和注意的地方就介紹到這里,希望對你有所幫助。
【編輯推薦】