Making our programs more approachable and more vivid is of great benefit to our programming. It can also help us get twice the result with half the effort. The unwritten standards observed during programming are beneficial to our communication with others and our re-learning. After all, sometimes the program we write is not just for ourselves, nor is it on the rise, No matter later. Good coding makes the source code readable and meaningful, consistent with other language conventions, and as intuitive as possible.
1. Handling of errors (other)
The most basic requirement for program robustness is the processing and capturing of program errors. in ASP. NET, the error processing mechanism is the same as that in other programming languages. You can use Try... Catch... Finally and so on, which is greatly improved compared with ASP. In addition, using these error handling methods can greatly improve the readability and debugging speed of the program. When combining these advantages, we should pay more attention to this.
2. string processing
In web design, string processing is almost the most common. Use ASP.. NET, the string processing speed is faster than ASP. NET, specifically added a string processing class StringBulider, using this class can complete some common string operations, and most importantly, using StringBuilder can greatly improve the string processing speed.
In ASP. NET, the most common is to use "&" to connect two strings:
|
Reference content is as follows:
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 perform some basic operations on strings, such as Append, Replace, Insert, and Remove, now let's take a look at the specific example.
(1) Use of Append in StringBuilder
Append is the same as Append in other languages, that is, adding other characters at the end of the string.
|
Reference content is as follows:
http://www.CodeHighlighter.com/ -->Dim sb as StringBuilder = New StringBuilder() sb.append( "
")
Sb. Append (dt. Rows (I). Item (k, DataRowVersion. Current). toString ())
Sb. Append (" |
")
Dim strOutput as String = sb. ToString ()
LblCompany. Text = strOutput |
In the above program, the Append method is used to output a table. Note that the StringBulider must first use ToString () the method can be converted to the String type for direct output. In the above example, all we see is a direct string of Append. In fact, this method has a very convenient function, that is, it can directly Append other types of variables, for example, you can Appemd a value of the Integer type directly. Of course, the output will be automatically converted into a string:
|
Reference content is as follows:
Http://www.CodeHighlighter.com/ --> Sub Page_Load (Source As Object, E As EventArgs) Dim sb As System. Text. StringBuilder Dim varother As Integer Varothers = 9999 Sb = new System. Text. StringBuilder () Sb. append ("other types of Append supported :") Sb. append (varother) Response. write (sb. toString ()) End Sub |
(2) Use of other methods in strings
We can also use other methods to look at the common ones:
Insert method. you can Insert other characters at the specified position. Use the Insert method: Insert (Insert position, Insert character );
The Remove method can be used to delete a specified number of characters at a specified position. Usage: Remove (actual position, number of characters );
Replace method, which can replace specified characters. Usage: Replace (Replaced string, replaced string)
3. Close the Connection and DataReader of the database.
When programming with ASP, we know that after connecting to a database, we must close the connection and set it to NoThing. In Asp. NET, we still need to use it like this, however, in ASP. NET, due to the use of ADO. NET. Therefore, there are some subtle differences in some related processing. These differences are often the most important during our design. Now, let's take an example to see what issues need to be paid attention to in common ADO. NET operations.
(1) Example 1
|
Reference content is as follows:
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 preceding example, we noticed that only DataReader is disabled and Connection is not disabled. Why? Observe the preceding ExecuteReader method carefully. Originally, the ExecuteReader parameter is set. After the ExecuteReader is executed, the Connection is automatically closed. Therefore, after this setting, there is no need to manually close the Connection.
(2) Example 2
|
Reference content is as follows:
http://www.CodeHighlighter.com/ -->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 DataReader was not closed. Why? In fact, the above Code does not directly generate the DataReader object, and of course it cannot be closed. Note that before closing the Connection, the program first checks whether the Connection is enabled. If the Connection is not enabled, it is unnecessary to close the Connection.