📄 resultsetform.vb
字号:
Imports System.Data.SqlServerCe
Imports System.ComponentModel
Imports System.Text
Public Class ResultSetForm
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
DoStuffWithResultSet()
End Sub
Private Const SSCE_M_KEYDUPLICATE As Integer = 25016
Private Sub DoStuffWithResultSet()
Dim connectionString As String = "Data Source =" _
& System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) _
& "\MyDatabase.sdf; Password =""MobileP@ssw0rd"";"
' Create the ResultSets using our own constructor, to override the default behavior
Using prodCatRS As ProductCategoryResultSet = _
New ProductCategoryResultSet(False, connectionString)
Using productRS As ProductResultSet = _
New ProductResultSet(False, connectionString)
Try
' Open the two ResultSets
prodCatRS.OpenEx()
productRS.OpenEx()
' Report count of records to the screen...
ReportRecords(prodCatRS.Connection, productRS)
' SECOND: Add some product categories
' To make sure we don't duplicate names, productCategory.Name has a unique constraint
Try
prodCatRS.AddProductCategoryRecord("Rock Climbing Equipment")
prodCatRS.AddProductCategoryRecord("Scuba Diving Equipment")
textBox1.Text += "\r\nProduct Categories added.\r\n"
Catch sqlEx As System.Data.SqlServerCe.SqlCeException
If sqlEx.NativeError = SSCE_M_KEYDUPLICATE Then
' If the categories already exist, just continue
textBox1.Text += "\r\nProduct Category addition failed, items already exist.\r\n"
Else
Throw
End If
End Try
' THIRD: Add some products in the category with categoryID of 1
productRS.AddProductRecord("Contoso Single Rope", "Red/Blue", 155.95, "60m", 1)
productRS.AddProductRecord("Contoso Rock Shoes", "Black", 89.95, "8", 1)
textBox1.Text += "\r\nProducts added.\r\n"
' Report count of records to the screen...
ReportRecords(prodCatRS.Connection, productRS)
Finally
' Close the DataReaders
productRS.Close()
prodCatRS.Close()
' Close and dispose of the database connections
productRS.Connection.Close()
prodCatRS.Connection.Close()
End Try
End Using
End Using
End Sub
Private Sub ReportRecords(ByVal conn As SqlCeConnection, ByVal productRS As ProductResultSet)
Dim categoryCount As Integer
' One way of counting the records is to ask the database
Using cmd As SqlCeCommand = New SqlCeCommand("SELECT COUNT(*) FROM ProductCategory", conn)
categoryCount = CType(cmd.ExecuteScalar(), Integer)
End Using
' Alternatively, Get count by casting the ResultSet to IListSource
Dim productCount As Integer = (CType(productRS, IListSource)).GetList().Count
Dim sb As StringBuilder = New StringBuilder(textBox1.Text)
sb.Append("There are currently ")
sb.Append(categoryCount)
sb.Append(" product categories.\r\n")
sb.Append("There are currently ")
sb.Append(productCount)
sb.Append(" products.\r\n")
textBox1.Text = sb.ToString()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -