I want to create a class in vb.net that make the connection to database. Also I need the way to use this class inside forms. I need a code example of both the class and the form call
Thanks.
I want to create a class in vb.net that make the connection to database. Also I need the way to use this class inside forms. I need a code example of both the class and the form call
Thanks.
Right Click on your project in solution explorer > Add > Class then right the following. Change as per your requirements.
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbPermission
Imports System.Math
Public Class ClassCon
Dim Filepath As String = Application.StartupPath & "\yourDatabase.mdb;"
Public constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Jet OLEDB:Database Password=yourPassword;data source=" & Filepath
Public con As New OleDbConnection(constring)
Now use the connection class in form as below
Imports System.Data.OleDb
Public Class form1
Dim cmd As New OleDbCommand
Dim objcon As New ClassCon
If (objcon.con.State = ConnectionState.Closed) Then objcon.con.Open()
cmd100 = New OleDbCommand("INSERT INTO Mosey VALUES('" & TextBox1.Text & "')", objcon.con)
cmd.ExecuteNonQuery()
objcon.con.Close()
I am not an expert but my suggestion is you should, rather you must know what you are trying to search in Google. I am always able to find what I am looking for. And if you are still not sure then post here with your code i.e what you have tried so far. All these code above are not tested with IDE so there may be typo.