我正在尝试获取表单加载的一些值,这将是自动的,因为用户不应该输入这些值。我正在以子例程的形式进行这些操作,因此它看起来更整洁,并且我更容易重用。 贝娄,你可以看看我有的5个子,第一个完美的工作,其余的不!我不明白,因为它们基于同样的原则!
Sub CustomerNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim Rows As Integer 'Used For Customer
Dim sql As String = "SELECT * FROM Customer"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Customer")
Rows = ds.Tables("Customer").Rows.Count
Customer_IDTextBox.Text = Rows
End Sub
Sub OrderNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim ds As New DataSet
Dim sql As String = "SELECT * FROM Order"
Dim Rows As Integer 'Used for Order Table
Dim da As OleDb.OleDbDataAdapter
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds.Tables(0))
Rows = ds.Tables(0).Rows.Count
NewOrderID.Text = Rows + 1
Order_IDTextBox.Text = NewOrderID.Text
End Sub
Sub PizzaNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim Rows As Integer 'Used for Pizza Table
Dim sql As String = "SELECT * FROM Pizza"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Pizza")
Rows = ds.Tables("Pizza").Rows.Count
NewPizzaID.Text = Rows + 1
Pizza_IDTextBox.Text = NewPizzaID.Text
End Sub
Sub DrinksNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim Rows As Integer 'Used for Drinks Table
Dim sql As String = "SELECT * FROM Drinks"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Drinks")
Rows = ds.Tables("Drinks").Rows.Count
NewDrinksID.Text = Rows + 1
Drinks_IDTextBox.Text = NewDrinksID.Text
End Sub
Sub SidesNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim Rows As Integer 'Used for Sides Table
Dim sql As String = "SELECT * FROM Sides"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Sides")
Rows = ds.Tables("Sides").Rows.Count
NewSidesID.Text = Rows + 1
Sides_IDTextBox.Text = NewSidesID.Text
End Sub
以下是我在表单加载时调用的位:
Private Sub frmProducts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetValues()
CustomerNo()
OrderNo()
PizzaNo()
DrinksNo()
SidesNo()
End Sub
set values sub工作正常,这不是问题,只是在beggining设置值。它没有显示错误或什么都没有,只是代码似乎没有工作,它除了第一个CustomerNo()位之外什么都不做。
提前再次感谢。
此致
何
答案 0 :(得分:1)
这里有几个错误:1)“订单”一词几乎是任何数据库中的保留字。所以你必须分隔它,否则DB会抛出错误。 2)在数据集中,您正在尝试填充未创建的表(0)(因此不存在)。试试这段代码:
'It is always best to put settings like this in a private property.'
'Better still, learn how to look it up from your project settings (file)'
Private ReadOnly Property ConnString As String
Get
return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
End Get
End Property
Sub OrderNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = ConnString
conn.Open()
Dim ds As New DataSet
Dim sql As String = "SELECT * FROM [Order]"
Dim Rows As Integer 'Used for Order Table'
Dim da As OleDb.OleDbDataAdapter
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds)
Rows = ds.Tables(0).Rows.Count
NewOrderID.Text = Rows + 1
Order_IDTextBox.Text = NewOrderID.Text
End Sub
Sub PizzaNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = ConnString
conn.Open()
Dim Rows As Integer 'Used for Pizza Table'
Dim sql As String = "SELECT * FROM Pizza"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Pizza")
Rows = ds.Tables("Pizza").Rows.Count
NewPizzaID.Text = Rows + 1
Pizza_IDTextBox.Text = NewPizzaID.Text
End Sub
Sub DrinksNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = ConnString
conn.Open()
Dim Rows As Integer 'Used for Drinks Table'
Dim sql As String = "SELECT * FROM Drinks"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Drinks")
Rows = ds.Tables("Drinks").Rows.Count
NewDrinksID.Text = Rows + 1
Drinks_IDTextBox.Text = NewDrinksID.Text
End Sub
Sub SidesNo()
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = ConnString
conn.Open()
Dim Rows As Integer 'Used for Sides Table'
Dim sql As String = "SELECT * FROM Sides"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Sides")
Rows = ds.Tables("Sides").Rows.Count
NewSidesID.Text = Rows + 1
Sides_IDTextBox.Text = NewSidesID.Text
End Sub