Best ASP.NET program habits

xiaoxiao2021-03-06  58

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.

First, the process of errors (outside)

The most basic requirements for the program robustness are the processing and capture of program errors. In ASP.NET, the wrong process is the same as other programming languages, which can use Try ... catch ... finaLLy, which is more than the ASP. Big progress. 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 of strings

In web design, 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 to use "&" to connect 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) Use of Append in StringBuilder

Append and other languages ​​Append is the final addition of other characters in the string.

DIM SB AS STRINGBUILDER = New StringBuilder ()

sb.append ("

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) Use of other methods in strings

We can also use other methods, let's take a look at common:

Insert method, other characters can be inserted in the specified location, how to use: INSERT (inserted, inserted characters);

Remove method, you can delete the specified word numbered characters in the specified location, how to use: remove (in fact, the number of positions, characters);

Replace method, you can replace the specified character, how to use: Replace (replaced string, replace string)

The specific introduction and usage of strings can refer to the following article (English):

http://aspfree.com/aspnet/stringbuilder.aspx

Http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemtextStringBuilderClasstopic.asp

Third, the database link Connection and DataReader close

When using 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

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.executeRead (commandbehavior.closeconnection)

DROPDOWNLIST1.DataSource = MyDataReader

DropDownList1.Database

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 here, 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 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.Database

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 settings

First, let's see the Web.config settings, we add two items to this file, set the following:

(2) Use of variables

The above XML file sets two variables of DSN and SomeotherKey, and now we look at how to use in the program: