问题描述
这可能是我在程序完成之前要解决的最后一个问题,它似乎是一个常见的问题,但是参数不同,通常会在从数据库中提取日期时看到。
我的程序很简单,它有2个numupdown控件,一个按钮和三个标签。用户在第一个numupdown(BoolNum)中输入成功概率,他们将丢弃组编号放在第二个numupdown(GroupNum)中,然后他们点击按钮以告知他们获得了什么项目。
单击按钮时,程序将连接到包含项目列表及其删除组的数据库。该程序首先决定玩家是否会获得一个项目。如果它出现错误,程序将告诉玩家它是错误的,但是如果它是真的那么它在数据库中搜索指定的拖放组中的所有项目并将每个项目添加到ItemArray,然后随机选择ItemArray中的一个项目并将其显示在第三个标签(ItemReceived)中。我使用的代码如下所示。
Imports System.Data
Imports System.Data.OleDb
公共类Form1
Dim DbConnection为新OleDbConnection
Dim DbCommand为新OleDbCommand
Dim DbInsert为新OleDbCommand
Dim DbUpdate为新OleDbCommand
Dim DbDelete为新OleDbCommand
Dim strConnectionString =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =&
System.Environment.CurrentDirectory& \ Game.mdb
公共函数GetRandom(ByVal Min As Integer,ByVal Max As Integer)作为整数
',通过使Generator静态,我们保留相同的实例'
'(即,不要反复创建具有相同种子的新实例)'
'调用'
静态生成器As System.Random = New System.Random()
返回Generator.Next(Min,Max)
结束函数
Private Sub BoolBtn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理BoolBtn.Click
如果GetRandom(0,101)< BoolNum.Value然后
'尝试再次被添加到ItemArray,因为它需要一个初始值,因为
'GetRandom函数不接受负值,并且是非包容性的,所以它是
'永远不能生成0)
Dim ItemArray()As String = New String(0){Try Again}
DbConnection = New OleDbConnection(strConnectionString)
尝试
'打开连接
DbConnection.Open()
'创建抽取数据的SQL查询
Dim SqlQry As String =SELECT [项目名称] FROM [项目列表] WHERE [Drop Group] ='& GroupNum.Value& '
'创建数据适配器
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQry,DbConnection)
'create dataset
Dim ds As DataSet =新数据集
'填充数据集
da.Fill(ds,物料清单)
'获取数据表
Dim dt As DataTable = ds.Tables(Item List)
'显示数据
Dim row As DataRow
For each row in dt.Rows
Dim NewItem As新字符串(项目名称)
Array.Resize(ItemArray,ItemArray.Length + 1)
ItemArray(ItemArray.Length - 1)= newItem
下一行
Catch ex As OleDbException
MsgBox(错误:& ex.ToString& vbCrLf)
最后
'关闭连接
DbConnection.Close()
ItemReceived.Text = GetRandom(0,ItemArray.Length)
结束尝试
否则
MsgBox(False)
结束如果
结束子
结束类
This may be my last problem to solve before the program is finished and it seems to be a common problem but with different parameters, it's usually seen when pulling dates from a database.
My program is simple, it has 2 numupdown controls, a button, and three labels. The user inputs the probability of success in the first numupdown (BoolNum), they put the drop group number in the second numupdown(GroupNum), and they click the button to be told what item they are getting.
When they click the button the program connects to a database containing a list of items and their drop groups. The program first decides whether the player will get an item. If it comes up false the program will tell the player it's false, but if it's true then it searches the database for all items that are in the specified drop group and adds each item to the ItemArray, then chooses one item from the ItemArray at random and displays it in the third label (ItemReceived). The code I am using is presented below.
Imports System.Data Imports System.Data.OleDb Public Class Form1 Dim DbConnection As New OleDbConnection Dim DbCommand As New OleDbCommand Dim DbInsert As New OleDbCommand Dim DbUpdate As New OleDbCommand Dim DbDelete As New OleDbCommand Dim strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Environment.CurrentDirectory & "\Game Database.mdb" Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer ' by making Generator static, we preserve the same instance ' ' (i.e., do not create new instances with the same seed over and over) ' ' between calls ' Static Generator As System.Random = New System.Random() Return Generator.Next(Min, Max) End Function Private Sub BoolBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoolBtn.Click If GetRandom(0, 101) < BoolNum.Value Then 'Try Again was added to ItemArray because it needed an initial value and because 'GetRandom function does not accept negative values, and is non-inclusive, so it 'can never generate 0) Dim ItemArray() As String = New String(0) {"Try Again"} DbConnection = New OleDbConnection(strConnectionString) Try ' Open connection DbConnection.Open() 'Create the SQL Query that draws out the data Dim SqlQry As String = "SELECT [Item Name] FROM [Item List] WHERE [Drop Group] = '" & GroupNum.Value & "'" 'create data adapter Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQry, DbConnection) 'create dataset Dim ds As DataSet = New DataSet 'fill dataset da.Fill(ds, "Item List") 'get data table Dim dt As DataTable = ds.Tables("Item List") 'display data Dim row As DataRow For Each row In dt.Rows Dim NewItem As New String("Item Name") Array.Resize(ItemArray, ItemArray.Length + 1) ItemArray(ItemArray.Length - 1) = newItem Next row Catch ex As OleDbException MsgBox("Error: " & ex.ToString & vbCrLf) Finally ' Close connection DbConnection.Close() ItemReceived.Text = GetRandom(0, ItemArray.Length) End Try Else MsgBox("False") End If End Sub End Class
这篇关于填充数据集时数据类型不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!