我有一个Web服务,可从数据库插入数据,从数据库中删除数据或将数据更新到数据库。因此,以这种特殊形式,它具有外键。因此,基本上,它应该只接受原始表中存在的值。 当用户输入其他任何值时,都会发生异常。
如何确保在捕获到此类SQL异常后,从我的Web服务传递了用户友好的自定义错误消息(例如,输入的ID不存在。数据插入失败!)。
非常感谢。
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>
Public Class WebService2
Inherits System.Web.Services.WebService
Public Class Details
Public Property CardID As Integer
Public Property BookID As Integer
Public Property IssuedBy As String
End Class
<WebMethod>
Public Sub AddBook(ByVal emp As Details)
Dim cs1 As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con1 As SqlConnection = New SqlConnection(cs1)
Dim thequery As String = "select * from BookIssue where CardID=@CardID"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, con1)
cmd1.Parameters.Add(New SqlParameter() With {
.ParameterName = "@CardID",
.Value = emp.CardID
})
con1.Open()
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MsgBox("ID Number is Already in use")
con1.Close()
Else
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spInsertIntoBookIssue", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@CardID",
.Value = emp.CardID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@BookID",
.Value = emp.BookID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@IssuedBy",
.Value = emp.IssuedBy
})
con.Open()
cmd.ExecuteNonQuery()
MsgBox("Data Inserted")
End Using
End If
End Using
End Sub
<WebMethod>
Public Sub GetAllDetails()
Dim listEmployees As List(Of Details) = New List(Of Details)()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("Select * from BookIssue", con)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Dim details As Details = New Details()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
listEmployees.Add(details)
End While
End Using
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Context.Response.Write(js.Serialize(listEmployees))
End Sub
<WebMethod>
Public Sub GetAllDetails1()
Dim listEmployees As List(Of Details) = New List(Of Details)()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("Select * from BookIssue", con)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Dim details As Details = New Details()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
listEmployees.Add(details)
End While
End Using
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Context.Response.Write(js.Serialize(listEmployees))
End Sub
<WebMethod>
Public Function GetdetailsById(ByVal CardID As Integer) As Details
Dim details As Details = New Details()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spGetBookIssueDetailsByCardID", con)
cmd.CommandType = CommandType.StoredProcedure
Dim parameter As SqlParameter = New SqlParameter()
parameter.ParameterName = "@CardID"
parameter.Value = CardID
cmd.Parameters.Add(parameter)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
End While
End Using
Return details
End Function
<WebMethod>
Public Function GetdetailsById1(ByVal CardID As Integer) As Details
Dim details As Details = New Details()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spGetBookIssueDetailsByCardID1", con)
cmd.CommandType = CommandType.StoredProcedure
Dim parameter As SqlParameter = New SqlParameter()
parameter.ParameterName = "@CardID"
parameter.Value = CardID
cmd.Parameters.Add(parameter)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
End While
End Using
Return details
End Function
<WebMethod>
Public Sub DeleteRecord1(ByVal emp As Details)
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spDeleteByID", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@CardID",
.Value = emp.CardID
})
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
<WebMethod>
Public Sub Update(ByVal emp As Details)
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spUpdateBookIssue", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@CardID",
.Value = emp.CardID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@BookID",
.Value = emp.BookID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@IssuedBy",
.Value = emp.IssuedBy
})
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
End Class
答案 0 :(得分:0)
我建议根据此示例对代码进行建模,我修改了您提供的delete子项,以针对未处理的情况包含具有自定义ErrorRoutine的catch,否则,您可以从executeNonQuery()函数获取已删除的行数。我也进行了更改,因此我们返回已删除的行数,这样,如果它的行数小于1,则可以在调用删除操作后对消息执行任何操作。
Public Sub DeleteRecord1(ByVal emp As Details) As Integer
Dim cs As String = ConfigurationManager.ConnectionStrings("library management
systemConnectionString").ConnectionString
Dim m_RowsDeleted As Integer
Dim con As SqlConnection = New SqlConnection(cs)
Try
Using con
Dim cmd As SqlCommand = New SqlCommand("spDeleteByID", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "@CardID",
.Value = emp.CardID
})
con.Open()
m_RowsDeleted = cmd.ExecuteNonQuery()
End Using
Catch ex As Exception
ErrorRoutine(ex, "Delete")
Finally
If Not IsNothing(con) Then
If con.State = ConnectionState.Open Then
con.Close()
End If
End If
End Try
Return m_RowsDeleted
End Sub