Я пытаюсь создать программу для вставки новых записей в базу данных Access,
Я также хочу просматривать записи в прямом эфире и обновлять после каждого нажатия на одну из кнопок ДОБАВИТЬ. Ты должен знать :
- Я использую одну форму1.
- Я использую Access DB (.mdb).
- Я использую Visual Studio 2017.
- Я использую Microsoft Access 2016.
Я также новичок в VB .net.
Наконец-то это мой код:
Imports System.Data.OleDb
Module modConnection
Public cn As New OleDb.OleDbConnection
Public cm As New OleDb.OleDbCommand
Public dr As OleDbDataReader
Public Sub connection()
cn = New OleDb.OleDbConnection
With cn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Application.StartupPath & "\sample.mdb"
.Open()
End With
End Sub
End Module
Public Class Form1
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFind.Click
Dim found As Boolean
Try
cm = New OleDb.OleDbCommand
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "SELECT * FROM tblStudent WHERE (Snum = '" &
txtSearch.Text & "')"
dr = .ExecuteReader
End With
While dr.Read()
txtName.Text = dr("Sname").ToString
txtCourse.Text = dr("Scourse").ToString
txtSection.Text = dr("Ssection").ToString
found = True
End While
cn.Close()
Exit Sub
If found = False Then MsgBox("Student ID not found.",
MsgBoxStyle.Critical)
dr.Close()
Catch ex As Exception
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SampleDataSet.tblStudent'
table. You can move, or remove it, as needed.
Me.TblStudentTableAdapter.Fill(Me.SampleDataSet.tblStudent)
Call connection()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Try
cm = New OleDb.OleDbCommand
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "INSERT INTO tblStudent
(Snum,Sname,Scourse,Ssection) VALUES (@Snum,@Sname,@Scourse,@Ssection)"
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Snum",
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtSearch.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Sname",
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtName.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Scourse",
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtCourse.Text))
.Parameters.Add(New
System.Data.OleDb.OleDbParameter("@Ssection",
System.Data.OleDb.OleDbType.VarChar, 255, Me.txtSection.Text))
' RUN THE COMMAND
cm.Parameters("@Snum").Value = Me.txtSearch.Text
cm.Parameters("@Sname").Value = Me.txtName.Text
cm.Parameters("@Scourse").Value = Me.txtCourse.Text
cm.Parameters("@Ssection").Value = Me.txtSection.Text
cm.ExecuteNonQuery()
MsgBox("Record saved.", MsgBoxStyle.Information)
Me.txtCourse.Text = ""
Me.txtName.Text = ""
Me.txtSearch.Text = ""
Me.txtSection.Text = ""
Exit Sub
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
Me.TblStudentTableAdapter.Fill(Me.SampleDataSet.tblStudent)
Call connection()
ShowData.Items.Refresh();
End Sub
End Class
Мне действительно нужна помощь,
Спасибо ребята в продвинутом.