|
-
Jun 4th, 2003, 02:48 PM
#1
Thread Starter
Junior Member
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
-
Jun 4th, 2003, 03:18 PM
#2
Junior Member
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"
-
Jun 4th, 2003, 03:59 PM
#3
Thread Starter
Junior Member
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..
-
Jun 4th, 2003, 04:12 PM
#4
Junior Member
Opps! Forgot I was in the .NET section... Try this:
System.AppDomain.CurrentDomain.BaseDirectory
-
Jun 4th, 2003, 04:22 PM
#5
Frenzied Member
'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
-
Jun 4th, 2003, 04:25 PM
#6
Thread Starter
Junior Member
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
-
Jun 4th, 2003, 04:43 PM
#7
Junior Member
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 & "\"
-
Jun 4th, 2003, 06:52 PM
#8
Frenzied Member
Just put DataSource=mydb.mdb
-
Jun 5th, 2003, 01:36 AM
#9
Frenzied Member
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
-
Jun 5th, 2003, 03:51 AM
#10
New Member
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!
-
Jun 5th, 2003, 12:06 PM
#11
Thread Starter
Junior Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|