我正在使用 vb.net 为 sql 数据库编写一个非常基本的数据库输入表单
在执行代码时
dsnewrow = ds.Tables("Car Details").NewRow()
我在此之前的同一个 sub 中声明 dsnewrow:
Dim dsnewrow As DataRow
并且 ds 在这里被声明:
Dim ds As DataSet
在表单的公共类中。
我知道我的 ds 变量有问题,但我不明白它是什么,有人可以帮忙吗?
完整程序:
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Form1
Dim inc As Integer
Dim ds As New DataSet
Dim da As SqlDataAdapter
Dim maxrows As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
If MessageBox.Show("Are you sure that you want to delete this record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Cancelled")
Else
ds.Tables("Car Details").Rows(inc).Delete()
maxrows = maxrows - 1
inc = 0
NavigateRecords()
da.Update(ds, "authors2")
End If
End Sub
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Car_DetailsTableAdapter.Fill(Me.CourseworkDataSet.Car_Details)
Dim sqlstring As String
Dim conn As SqlConnection
Dim connstring As String
connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Daniel\Documents\Coursework.mdf;Integrated Security=True;Connect Timeout=30"
conn = New SqlConnection(connstring)
conn.Open()
MsgBox("successful")
sqlstring = "SELECT * FROM Car Details"
da = New SqlDataAdapter(sqlstring, conn)
ds = New DataSet
da.Fill(ds, "Car Details")
conn.Close()
conn = Nothing
maxrows = ds.Tables("Car Details").Rows.Count
inc = 0
navigaterecords()
End Sub
Private Sub Car_DetailsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Car_DetailsBindingNavigatorSaveItem.Click
Me.Validate()
Me.Car_DetailsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CourseworkDataSet)
End Sub
Private Sub NavigateRecords()
MakeTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(0)
ModelTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(1)
Engine_SizeTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(2)
ColourTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(3)
DoorsTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(4)
Buy_PriceTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(5)
Sell_PriceTextBox = ds.Tables("Car Details").Rows(inc).Item(6)
End Sub
Private Sub btnFwd_Click(sender As Object, e As EventArgs) Handles btnFwd.Click
If inc <> maxrows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Records")
End If
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
Else
MsgBox("First Record")
End If
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
If inc <> maxrows - 1 Then
inc = maxrows - 1
NavigateRecords()
End If
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
ds.Tables("Car Details").Rows(inc).Item(0) = MakeTextBox.Text
ds.Tables("Car Details").Rows(inc).Item(1) = ModelTextBox.Text
ds.Tables("Car Details").Rows(inc).Item(2) = Engine_SizeTextBox.Text
ds.Tables("Car Details").Rows(inc).Item(3) = ColourTextBox.Text
ds.Tables("Car Details").Rows(inc).Item(4) = DoorsTextBox.Text
ds.Tables("Car Details").Rows(inc).Item(5) = Buy_PriceTextBox.Text
ds.Tables("Car Details").Rows(inc).Item(6) = Sell_PriceTextBox.Text
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
btnUpdate.Enabled = False
btnAdd.Enabled = False
btnConfirm.Enabled = True
btnDelete.Enabled = False
ModelTextBox.Clear()
MakeTextBox.Clear()
Engine_SizeTextBox.Clear()
ColourTextBox.Clear()
DoorsTextBox.Clear()
Buy_PriceTextBox.Clear()
Sell_PriceTextBox.Clear()
End Sub
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
If inc <> -1 Then
Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim dsnewrow As DataRow
dsnewrow = ds.Tables("Car Details").NewRow()
dsnewrow.Item("Make") = MakeTextBox.Text
dsnewrow.Item("Model") = ModelTextBox.Text
dsnewrow.Item("Engine Size") = Engine_SizeTextBox.Text
dsnewrow.Item("Colour") = ColourTextBox.Text
dsnewrow.Item("Doors") = DoorsTextBox.Text
dsnewrow.Item("Buy Price") = Buy_PriceTextBox.Text
dsnewrow.Item("Sell Price") = Sell_PriceTextBox.Text
btnConfirm.Enabled = False
btnAdd.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
End If
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
btnUpdate.Enabled = True
btnAdd.Enabled = True
btnConfirm.Enabled = True
btnDelete.Enabled = True
inc = 0
NavigateRecords()
End Sub
End Class