我正在使用VB来创建更改数据库中记录的内容。我有一个名为tblCustomers的表。当用户单击homeForm上的按钮时,会出现一个名为FormNewCustomer的新表单,其中包含用于输入信息以放入数据库的文本框。提交后,数据会插入,但不会显示在datagridview中。
这是我的代码:
Imports System.Data.OleDb
Public Class FormNewCustomer
Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ProgramDatabase.accdb"
Public conn As New OleDbConnection(connstring)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub btnCopy_Click(sender As Object, e As EventArgs) Handles btnCopy.Click
If txtCustomerName.Text = "" Or txtAC.Text = "" Then
MsgBox("Enter a Customer Name and a Customer Reference.")
Else
conn.Open()
Dim SqlQuery As String = "INSERT INTO tblCustomers (CustomerName,AC,Address,Phone,Email) VALUES (@CustomerName,@AC,@Address,@Phone,@Email)"
Dim SqlCommand As New OleDbCommand
With SqlCommand
.CommandText = SqlQuery
.Parameters.AddWithValue("@CustomerName", txtCustomerName.Text)
.Parameters.AddWithValue("@AC", txtAC.Text)
.Parameters.AddWithValue("@Address", txtAddress.Text)
.Parameters.AddWithValue("@Phone", txtPhone.Text)
.Parameters.AddWithValue("@Email", txtEmail.Text)
.Connection = conn
.ExecuteNonQuery()
End With
conn.Close()
MsgBox("Successfully added new Customer.")
FormHome.DataGridView1.Refresh()
Me.Close()
End If
End Sub
End Class
答案 0 :(得分:0)
试用此代码:
Dim objDataGridView As YourFormClassName = New YourFormClassName
objDataGridView.ShowDialog()