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):
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)
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
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:
appsettings>
configure>
(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:
SUB Page_Load (Sender As Object, E as Eventargs)
DIM Appsettings as Hashtable = Context.getconfig ("Appsettings")
DSN.Text = AppSettings ("DSN")
Someother.text = Appsettings ("Some Portkey")
End Sub
script>
DSN Setting:
Some Other Setting:
body>
html>
The above program we see that the variables defined using this are simple and convenient.
V. Use the .NET to debug the program
The debugging of the ASP program has always been the most difficult place to write ASP. At this point, the ASP programmer is probably appreciated, because everyone is using Response.Write to debug. And the biggest disadvantage of this is that when we debug, you must delete or comment out of this information, think about it, if the program code reaches a few hundred rows or a multi-page, how boring, most afraid One, forget to delete these debugled Write, some indecent debug information may appear when the user is used.
After using ASP.NET, we can directly define Trace to implement programs debugging. The trouble mentioned above can be easily resolved. It is familiar with the implementation of the specific page and the specific page and in the web.config configuration file, so that after the program debugging is completed, it is possible to set the trace to OFF, so that the program is There will be no debugging. (1) Implementation of page debugging
When a specific page needs to be debugged, we can set it:
<% @ Page language = "vb" trace = "true"%>
(2) Define Web.config implementation
In Web.config, we can also implement program debugging:
configure>
After using the above settings Open TRACE, we can use Trace to debug programs in a specific program, such as:
Trace.Write ("this is some customer design")
Or debug program variables:
Trace.write ("this is is my variable and it's value is:" & myvariable.toString ())
The above settings We can see that in ASP.NET, the program debugging function is very convenient, we have ignored these features in programming, continue to use ASP thinking to design the program, then our procedures are not only efficient, but Also added difficulty in cooperation between other developers.
Six, summary
Some of the above programs have used habits, we can slowly develop, when the program is designed, don't be too simple to make the simplest, for general developers, program standardization and readability may be more flexible than pursuit of procedures. important. In the case where the Internet resources are increasing, we can refer to some of the very standardized program source code. Of course, the best is the Microsoft's own things, we can refer to the following Website: http://www.asp. NET, about more program writing questions, we can refer to the following website: http://www.wrclub.net/