Results 1 to 4 of 4

Thread: Database Connection Question [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Toronto
    Posts
    103

    Database Connection Question [Resolved]

    I'm in the process of learning VB.Net and I have been rewriting some of my VB6 projects. I'm having a problem understanding ADO.NET. Can I just call my database connection from a module like I did in VB6. And if so how do I make it work in my other modules that do my business functions.

    Examples:

    VB6 Database connection
    VB Code:
    1. Public objAccessConnection As adodb.Connection
    2. Public rsAccess As adodb.Recordset
    3. Public mbAddNewFlag As Boolean
    4. Public mbEditFlag As Boolean
    5. Public mbDataChanged As Boolean
    6.  
    7. Public Sub DBConnection()
    8. On Error GoTo CheckError_Connection
    9. Set objAccessConnection = New adodb.Connection
    10. objAccessConnection.CursorLocation = adUseClient
    11. objAccessConnection.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
    12. "Data Source=" & App.Path & "\ConcorddB.mdb;Jet OLEDB:Database Password="
    13. Exit Sub
    14. CheckError_Connection:
    15. MsgBox Err.Description, 16, "Error message. "
    16. Resume Next
    17. End Sub

    .NET Data connection
    VB Code:
    1. Public Sub DBCon()
    2. Dim myConnString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & VB6.GetPath & "\ConcorddB.mdb;Jet OLEDB:Database Password=" ' Put here the connection string
    3. Dim myConnection As New System.Data.OleDb.OleDbConnection(myConnString) 'Create a new connection
    4. myConnection.Open() ' Open Connection
    5. End Sub

    Business Function
    VB6
    VB Code:
    1. Function GetCustName(iCol As Integer, dDocket As Double) As String
    2. Dim sName As String, rsName As adodb.Recordset
    3. sName = "SELECT CustName FROM [Orders(m)] WHERE OrderID = " & dDocket & ""
    4. Set rsName = New adodb.Recordset
    5. rsName.Open sName, objAccessConnection, adOpenKeyset, adLockReadOnly
    6. If rsName.EOF Then
    7.     GetCustName = "False"
    8. Else
    9.     GetCustName = rsName!CustName
    10. End If
    11. rsName.ActiveConnection = Nothing
    12. End Function

    .NET
    VB Code:
    1. Function GetCustNameNet(ByRef iCol As Short, ByRef dDocket As Double) As String
    2. Dim sNameN As String = "SELECT CustName FROM [Orders(m)] WHERE OrderID = " & dDocket & ""
    3. Dim rsNameN As New System.Data.OleDb.OleDbCommand(sNameN, myconnection) ' Stuck here
    4. rsNameN.Open(sNameN, objAccessConnection, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly)
    5.         If rsNameN.EOF Then
    6.             GetCustNameNet = "False"
    7.         Else
    8.             GetCustNameNet = rsNameN.Fields("CustName").Value
    9.         End If
    10. End Function

    I got stuck on the connection to the database in the .NET version.

    Can any one point me in the right direction.
    Last edited by ajirwin; Feb 4th, 2003 at 02:47 PM.
    Thanks
    Andrew

    VB 6.0 Sp5 and VB.net

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I'm using this little function created by me .I'll start you .It opens , saves data against access db.
    VB Code:
    1. Public Class Class1
    2.     '########################################################
    3.     'Category :Database
    4.     'Subject : Save Data to Access Database
    5.     '########################################################
    6.  
    7.     '~~~~~~~~~~~~~~Created by Pirate at [url]WWW.VBFORUMS.COM[/url] <[url]http://WWW.VBFORUMS.COM[/url]> ~~~~~~~~~~~~
    8.    
    9.     'Libraries needed for running this Code
    10.         Imports System.Data
    11.         Imports System.Data.OleDb
    12.  
    13.     'open Connection to the Database with Password
    14.     Dim MyPath As String = Application.StartupPath & "\__mydb__.mdb"
    15.     Dim MyPassword As String = "passme"
    16.     Dim StrSQL As String = "Select * From MyTable"
    17.     Dim MyConnection As New
    18. OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    19. MyPath & ";Jet OLEDB:Database Password=" & MyPassword)
    20.         MyConnection.Open()
    21.  
    22.     'This Saves data to a DataSet and then uses update method
    23.     'against Database Source File
    24.     Dim MyCommand As New OleDbCommand(StrSQL, MyConnection)
    25.     Dim MyDataset As DataSet = New DataSet()
    26.     Dim MyAdapter As New OleDb.OleDbDataAdapter(StrSQL, MyConnection)
    27.  
    28.  
    29.         MyAdapter.Fill(MyDataset, "MyTable")
    30.     Dim MyDataRow As DataRow = MyDataset.Tables("MyTable").NewRow
    31.     'Fill the data in Four Columns in the Database (Fields)
    32.         MyDataRow("1_Column") = Textbox1.Text
    33.         MyDataRow("2_Column") = Textbox2.Text
    34.         MyDataRow("3_Column") = Textbox3.Text
    35.         MyDataRow("4_Column") = Textbox4.Text
    36.         MyDataset.Tables("MyTable").Rows.Add(MyDataRow)
    37.         MyAdapter.Update(MyDataset, "MyTable")
    38.         MessageBox.Show("Data Saved..", "Saved",
    39.  
    40.     'if you want to write the data as well to XML file then you
    41.     'include(this)before update method
    42.         MyDataset.WriteXml(Application.StartupPath & "\_MyXMLFile_.XML")
    43.     End Sub
    44.  
    45. End Class
    enjoy !

  3. #3
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    How about this?

    .NET Data connection

    VB Code:
    1. Public Function getDBConnection () as OleDbConnection
    2. Dim myConnString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & VB6.GetPath & "\ConcorddB.mdb;Jet OLEDBatabase Password=" ' Put here the connection string
    3. Dim myConnection As New System.Data.OleDb.OleDbConnection(myConnString) 'Create a new connection
    4. myConnection.Open() ' Open Connection
    5.    Return myConnection
    6. End Function


    .NET

    VB Code:
    1. Function GetCustNameNet(ByRef iCol As Short, ByRef dDocket As Double) As String
    2. Dim sNameN As String = "SELECT CustName FROM [Orders(m)] WHERE OrderID = " & dDocket & ""
    3. ' Dim rsNameN As New System.Data.OleDb.OleDbCommand(sNameN, myconnection) ' Stuck here
    4.  
    5. Dim myconn as OLEDBConnection
    6.  
    7. myconn = getDBConnection()
    8.  
    9. Dim rsNameN As New System.Data.OleDb.OleDbCommand(sNameN, myconn)
    10.  
    11. rsNameN.Open(sNameN, objAccessConnection, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly)
    12.         If rsNameN.EOF Then
    13.             GetCustNameNet = "False"
    14.         Else
    15.             GetCustNameNet = rsNameN.Fields("CustName").Value
    16.         End If
    17.  
    18. myconn.close() ' Don't forget to close connection
    19.  
    20. End Function
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Toronto
    Posts
    103
    Thank you Mr.No that was just what I was looking for. Sorry Pirate, I had already read what you had and it had got me started.
    Thanks
    Andrew

    VB 6.0 Sp5 and VB.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width