Monday, May 10, 2010

VB MysqlDb Connection "Class" File.....

Imports Microsoft.VisualBasic
Imports System.Data
Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System
Imports System.Data.Odbc

Public Class mysqldb
Dim con As New MySqlConnection
Private str As String
Dim constate As Boolean = False

Private ds As New DataSet
Private da As New MySqlDataAdapter
Private dt As DataTable
Private strStatus As String
Private Cmd As New MySqlCommand
Public Sub New()

Try
con.ConnectionString = (ConfigurationManager.AppSettings("conobj"))
If con.State <> ConnectionState.Open Then
con.Open()
constate = True
End If
Catch ex As System.Exception
Throw New System.Exception("Error Occured in Opening DataBase, Contact Administrator!")
constate = False
End Try
End Sub

Public Function gettable(ByVal str As String, ByVal dt As DataTable) As DataTable
Try
If con.State <> ConnectionState.Open Then
con.Open()
constate = True
End If

da = New MySqlDataAdapter(str, con)
ds = New DataSet()
da.Fill(ds)
dt = ds.Tables(0)

Catch ex As System.Exception

End Try
con.Close()
Return dt
End Function

Public Sub filldll(ByVal str As String, ByVal ddl As DropDownList)

Try
If con.State <> ConnectionState.Open Then
con.Open()
constate = True
End If
Dim ds1 As New DataSet
Dim da1 As New MySqlDataAdapter(str, con)
da1.Fill(ds1)
ddl.DataSource = ds1
ddl.DataTextField = "txt"
ddl.DataValueField = "val"
ddl.DataBind()
Catch ex As Exception
End Try
con.Close()
ddl.Items.Insert(0, "Select")
End Sub

Public Sub filllist(ByVal str As String, ByVal list As ListBox)

Try
If con.State <> ConnectionState.Open Then
con.Open()
constate = True
End If

Dim ds1 As New DataSet
Dim da1 As New MySqlDataAdapter(str, con)
da1.Fill(ds1)
list.DataSource = ds1
list.DataTextField = "txt"
list.DataValueField = "val"
list.DataBind()
Catch ex As Exception
End Try
con.Close()
'list.Items.Insert(0, "---Select---")
End Sub

Public Sub savedata(ByVal str As String)
Try
If con.State <> ConnectionState.Open Then
con.Open()
constate = True
End If
Dim cmd As New MySqlCommand(str, con)
cmd.ExecuteNonQuery()
Catch ex As Exception
End Try
con.Close()
End Sub

Public Function returnDataset(ByVal str As String) As DataSet

Try
''con.ConnectionString = (ConfigurationManager.AppSettings("conobj"))
If con.State <> ConnectionState.Open Then
con.Open()
constate = True
End If
da = New MySqlDataAdapter(str, con)
ds = New DataSet()
da.Fill(ds)
Return ds
Catch ex As Exception
Return Nothing
Finally
ds.Dispose()
con.Close()
da.Dispose()
End Try
End Function

Public Function returnExecuteNonQuery(ByVal str As String) As String

Try
''con.ConnectionString = (ConfigurationManager.AppSettings("conobj"))
If con.State <> ConnectionState.Open Then
con.Open()
constate = True
End If
Cmd = New MySqlCommand(str, con)
strStatus = Cmd.ExecuteNonQuery()
Return strStatus
Catch ex As Exception
Return ex.Message.ToString()
Finally
con.Close()
Cmd.Dispose()
End Try
End Function
End Class