-1

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.

Mohammed Tawfik
  • 571
  • 1
  • 8
  • 21
  • I think if I found an answer in google I wouldn't come here. I did not found a clear answer so I am trying to ask experts. – Mohammed Tawfik Mar 19 '12 at 16:11
  • You should pick up a book or look at some tutorial on the web, then if you have a specific question you can ask it on SO. – Meta-Knight Mar 19 '12 at 16:14

1 Answers1

0

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.

MOB
  • 195
  • 3
  • 13