2007-09-25

用SQLServer处理数据层错误

来源: 开发者在线 作者:Tony Patton 评论 0 条
 
<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里是相应的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"/>
共4页: 上一页 [1] [2] 3 [4] 下一页

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:应用SQL Server 2005内置工具建立审查系统  
下一篇:您知道SQLServer2005的10个高级特性吗
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号