Best ASP.NET program habits

xiaoxiao2021-03-31  198

Friends programming friends often like to collect some "wonderful" programming skills. However, the accumulation of skills often does not improve the quality of the program, but guide some programming people to pursue and new, forget the cultivation of basic programming, not conducive to the team Cooperation, possibly, this is also a reason why China does not lack smart, but lacks a clever development team. In the development of ASP.NET, there are a lot of skills, but some basic programming habits we must develop, which can not only fundamentally improve the quality and development efficiency, but also facilitate the reading and team development of procedures. . If you write, you can understand or only a few people can understand, even if the program skills are skillful, the upgrade and maintenance of the program is a fatal issue.

I. The most basic requirements of the problem of the handler (outside) is the most basic requirements of the program error. In ASP.NET, the wrong handling has the same mechanism as other programming languages, you can use Try ... catch ... finaly or other means, This is more advanced compared to the ASP. Moreover, using these error handling methods, it can greatly improve the program's readability and program debugging speed. In the case of these advantages, we should pay more attention to this. About the wrong process, we can refer to this article (English): http://www.123aspx.com/redir.aspx?res=28336

Second, the processing web design of the string, the processing of strings is almost the most common. After using ASP.NET, the processing of strings is faster than ASP, and in ASP.NET, it is specifically adding a string handling class StringBulider. Use this class to complete some common string operations, and the main, Use StringBuilder to greatly improve string processing speed. In ASP.NET, the most common is the use, "" connected to two strings: Dim myOutputString As String = "My name is" Dim myInputString As String = "Alex" myOutputString = myOutputString & myInputString Response.Write (myoutputString) Now let's take a look at the use of StringBuilder. When using StringBuilder, we can do some basic operations for strings, such as Append, Replace, Insert, Remove, etc., now let's see specific examples. (1) Append's Append and other languages ​​in StringBuilder, is the last addition of other characters in the string. DIM SB AS STRINGBUILDER = new stringbuilder () sb.append ("

) for i = 0 to rowcount - 1 sb.append (" ") fork = 0 to colcount - 1 sb.append ("") Next sb.append ("") DIM STROUTPUT AS STRING = sb.toString () LBLCompany.Text = Stroutput In the above program, use the Append method to implement it. The output of a table is concerned that StringBulider must first use the toString () method to convert it to the String type directly.

In the above example, all we see is a direct string in Append. In fact, this method has a very convenient function, that is, you can directly append other type variables, such as the value of an Integer type, Of course, we have automatically converted into a string after output: 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 ( ") Sb.Append (Varother) response.write (sb.tostring ()) End Sub (2) String Other methods of other methods We can also use other methods, let's take a look at the common: Insert method, you can insert other characters in the specified location, use methods: INSERT, insert characters); REMOVE method, you can delete the specified word number in the specified location, use Method: Remove (actually position, character); Replace method, you can replace the specified character, how to use: Replace (replaced string, replace string) string specific introduction and usage can refer to the following article (English): http : //ASPFree.com/aspnet/StringBuilder.aspx http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystexTextStringBuilderClasstopic.asp

Third, when the database link Connection and DataReader are time to use ASP programming, we already know that after using the database connection, be sure to turn the connection, and then set to Nothing. In ASP.NET, we still need to use this, but in ASP.NET, due to the use of ADO.NET, there is still some subtle differences in some related processing, and these differences often It is most important to pay attention to our design. Now, we need to pay attention to what problems need to be paid in the common ADO.NET operation. (1) Example I 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 'Close DataReader myDataReader.close () end if End TRY In the above example, we noticed that DataReader is only closed, and does not turn off the Connection. why? Carefully observe the above ExecuteReader method, originally set the executereader parameter. After executing ExecuteReader, the Connection is automatically turned off. So, after this setting, there is no need to turn off the connection again.

(2) Example II 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 In the above example, we found that there is no DataReader without closing. why? In fact, in the above code, there is no DataReader object directly, and of course it is not closed. It should be noted that before turning off the connection, the program first determines whether the connection has been opened, and if it is not opened, there is no need to turn off.

Fourth, using Web.config / Maching.config Save Common Data Some Data We need to use often, for example, when using ADO.NET, the most common is the database connection statement. In ASP, we often save this information in Application. Of course, in ASP.NET, it can also be the case, however, ASP.NET has provided a profile web.config, so we'd better save this information in web.config, of course, we can also save in Machine. In Config, however, this must be used throughout the website, so we usually use Web.config. Now let's see the use of the specific file. (1) Web.config file setting first, let's see the Web.config settings, we add settings two items in this file, set the following: (2) Variable User XML file sets two variables, now we look at the program how to use in:

") sb.append (Dt.Rows (i) .Item (k, datarowversion.current) .tostring ()) sb.append ("" Next sb.append ("