Results 1 to 11 of 11

Thread: Help with my database conn string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    28

    Help with my database conn string

    I have an access database in the same directory as my vb.net program. I am trying to create the connection string in code and open the connection. My design time connection works fine but when I hardcode it by copying and pasting the conn string, it gives me an unknown error. In design time, I create a server db connection in the server explorer window and am not sure if I need to do this (or how to do this) in code.

    Also, if I am referring to the same directory level how do I specify it in the conn string ? For example, this is what I tried

    ConnString = ".....DataSource = ../mydb.mdb........"

    This is the original path generated in design time

    ConnString = ".....DataSource=C:/PROJECTS/mydb.mdb........."

    Please Help.
    Thanks
    Regards --
    Vujjeni

  2. #2
    Junior Member joan_fl's Avatar
    Join Date
    Aug 2002
    Location
    Tampa, FL
    Posts
    28
    I think this is what your looking for:

    ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\mydb.mdb;Persist Security Info=False"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    28
    I don't think App.Path still works in VB.NET....the name or notation might have changes coz my program doesn't recognize it..
    Regards --
    Vujjeni

  4. #4
    Junior Member joan_fl's Avatar
    Join Date
    Aug 2002
    Location
    Tampa, FL
    Posts
    28
    Opps! Forgot I was in the .NET section... Try this:

    System.AppDomain.CurrentDomain.BaseDirectory

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Application.StartUpPath
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    28
    Great I get the path till my bin directory..

    This is my code..

    Dim app As AppDomain
    Dim myState
    Dim myPath = app.CurrentDomain.BaseDirectory

    myConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=" & myPath & " GraphicsProject.mdb;Mode=Share Deny None;"

    mydb = New OleDbConnection(myConnStr)
    mydb.Open() ---------------------> Error
    myState = mydb.State()
    MessageBox.Show(myState)

    I get an unknown error when I open the database. Any idea what is going wrong ? My rest of the conn string is copiued and pasted from a design time conn object
    Regards --
    Vujjeni

  7. #7
    Junior Member joan_fl's Avatar
    Join Date
    Aug 2002
    Location
    Tampa, FL
    Posts
    28
    You need to add a "\" at the end of the path to your database...

    Try this:

    myConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=" & myPath & " \GraphicsProject.mdb;Mode=Share Deny None;"

    or this:

    Dim myPath = app.CurrentDomain.BaseDirectory & "\"

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Just put DataSource=mydb.mdb

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Just put DataSource=mydb.mdb
    Noooooooooo!
    I am strongly against this. I have my reason for that which i think is logical. I described it in this thread
    So its better to provide full path.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  10. #10
    New Member
    Join Date
    Jun 2003
    Location
    varna
    Posts
    2
    in module:
    Module ConnectionDB
    Public PathToDb As String
    Public conn As ADODB.Connection
    Public Sub ConnToDB(ByRef DBPath As String)
    conn = New ADODB.Connection
    If conn.State = ADODB.ObjectStateEnum.adStateClosed Then
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBPath
    conn.Open()
    End If

    End Sub
    Public Sub CloseDB()
    If conn.State = ADODB.ObjectStateEnum.adStateOpen Then
    conn.Close()
    End If
    End Sub
    next in form:
    Private Sub BUSDB()
    PathToDb = VB6.GetPath & "\.mdb"
    ConnToDB((PathToDb))

    End Sub

    Private Sub Form_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

    BUSDB()
    End Sub
    regards!

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    28
    I have got the path working fine and if I just used : datasource = mydb.mdb then it actually looks in the bin directory..however i am going to stick with Application.StarupPath and point to the base directory. Too bad they don't have Server.MapPath in .NET.

    I have one question about the connection though-->
    When I use mydb.Open() I keep getting an unknown error.
    Does having a Connection in the Server Explorer makes any difference when you are invoking a database connection in code ?
    Do I need it there at all ?


    mydb = New OleDbConnection(myConnStr)
    myState = mydb.State() =====> returns 0
    mydb.Open() =====> Unknown Error

    Thanks.
    Regards --
    Vujjeni

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