WEB开发网
开发学院数据库MSSQL Server 用SQL Server处理数据层错误 阅读

用SQL Server处理数据层错误

 2007-05-15 09:29:21 来源:WEB开发网   
核心提示: 列表C里是相应的VB.NET代码:<%@ Import Namespace="System.Data.SqlClient"%><%@ Import Namespace="System.Data" %><%@ Page l

列表C里是相应的VB.NET代码:

<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data" %>
<%@ Page language="VB" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html><head>
<title>TechRepublic.com - Test</title>
</head><body>
<script language="VB" runat="server">
Dim conn AS SqlConnection
Dim cmd As SqlCommand
Dim connString As String
Dim rvalue As Integer
Public Sub SubmitChanges()
connString = "datasource=LOU8-3590424PATTON;
uid=test;pwd=test;initial catalog=Northwind"
Try
conn = new SqlConnection(connString)
cmd = new SqlCommand("sp_UpdateCustomerPhone", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@id", SqlDbType.NVarChar, 5)
cmd.Parameters("@id").Value = lblID.Text
cmd.Parameters.Add("@phone", SqlDbType.NVarChar, 24)
cmd.Parameters("@phone").Value = txtPhone.Text
cmd.Parameters.Add("@retvalue",System.Data.SqlDbType.Int)
cmd.Parameters("@retvalue").Direction= ParameterDirection.Output
conn.Open()
cmd.ExecuteNonQuery()
rvalue = System.Convert.ToInt32(cmd.Parameters("@retvalue").Value)
If (rvalue = -1) Then
lblMessage.Text = "Database error duringupdate."
lblMessage.Visible = true
Else
lblMessage.Text = "Data has been updated."
lblMessage.Visible = true
End If
conn.Close()
Catch ex As SqlException
lblMessage.Text = "Error accessing database:" + ex.ToString()
Catch ex As Exception
lblMessage.Text = "Exception: " + ex.ToString()
Finally
If (conn.State = ConnectionState.Open)Then
conn.Close()
End If
conn.Dispose()
End Try
End Sub
Private Sub btnUpdatePhone_Click(sender As Object, eAs
System.Web.UI.WebControls.CommandEventArgs)
SubmitChanges()
End Sub
</script>
<form id="frmTestUpdate"method="post" runat="server">
<asp:Label ID="lblMessage"Visible="False" runat="server">
</asp:Label><br /><br />
<asp:Label ID="lblPhone"runat="server">New Number:</asp:Label>
<asp:TextBox ID="txtPhone"runat="server" /><br/><br />
<asp:Button ID="btnUpdatePhone"Text="Update"
OnCommand="btnUpdatePhone_Click" runat="server"/>
<asp:Label ID="lblID"Visible="False" runat="server">
ALFKI</asp:Label>
</form></body></html>

列表C

涵盖所有的数据库

利用try/catch代码块来处理.NET应用程序代码里的异常是一个简单明了的过程,但是你还可以监视数据库层的异常。SQLServer的T-SQL语言为你处理代码异常提供了所需要的一切。

作者:Tony Patton的职业生涯开始于应用程序开发员,并已经获得了证明其专业水平的Java、VB、Lotus和XML认证。

上一页  1 2 3 4 

Tags:SQL Server 处理

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接