connection string does not work for local database running windows 7
Hi all,
I am following a tutorial at http://www.homeandlearn.co.uk/NET/nets12p4.html
When I run there programme in debug mode it works great, the problem is when I try to run there programme on another computer I have running Windows 7.
I am using Visual studio ultimate 2012 to learn with and my Operating system is Windows 7.
All I am trying to achieve is make this small programme run on another computer
I have been to lot's of different forums and searched the net high and low for my answer but I can get it to work. OK here goes in debug mode it works. So I created a folder on my desktop and called it "databasetest" I then used the publish option which comes with VS2012 and published this app to my new folder I created on the desktop. I then click on the .exe file to install the software and it appears to install fine but then I get this error when I try to run it.
Code:
Exception Text **************
System.Data.OleDb.OleDbException (0x80004005): Could not find file 'C:\Users\Nige\AppData\Local\Apps\2.0\2GKHLO4K.J93\4M6EYCZL.BJG
\data..tion_406852d1df2764c8_0001.0000_9ff6234dc360b631\AddressBook.mdb'.
**My code is **
Option Strict On
Option Explicit On
Public Class Form1
Dim inc As Integer
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim MaxRows As Integer
Dim fldr As String
Dim Builder As New OleDb.OleDbConnectionStringBuilder
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'dbSource = "Data Source = C:\Program Files\databasetest\databasetest2\AddressBook.mdb"
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = " + AppDomain.CurrentDomain.BaseDirectory + "\AddressBook.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "AddressBook")
con.Close()
MaxRows = ds.Tables("AddressBook").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtFirstName.Text = CStr(ds.Tables("AddressBook").Rows(inc).Item(1))
txtSurname.Text = CStr(ds.Tables("AddressBook").Rows(inc).Item(2))
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Rows")
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
Else
MsgBox("First Record")
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
End Class
You will notice I have changed the connection string slightly from the tutorial which I did under someone Else's guidance.
in the tutorial is shows the connection string as being
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:/AddressBook.mdb"
What I really want is someone to show me how I can get this small application to install on another computer running Windows 7.
It would also be good if someone out there had a small application that I could maybe download from them along with the source code so I can see it working. I am just wanting to learn how to attach and use a local database with software I create Any guidance is very much appreciated.
Thanks.
Re: connection string does not work for local database running windows 7
Here's what looks like the cause of the problem:
Quote:
Originally Posted by
nigelvisio
'dbSource = "Data Source = C:\Program Files\databasetest\databasetest2\AddressBook.mdb"
dbSource = "Data Source = " + AppDomain.CurrentDomain.BaseDirectory + "\AddressBook.mdb"
There are several folders within Windows that you should not store writeable files in, as they are not designed to allow it, and are protected in various ways.
.mdb files are written to, even if they are only used as read-only.
One of those protected folders is Program Files, and it has been for a long time (for a link to the Windows 2000 documentation about it, see the article Where should I store the files that my program uses/creates? , but ignore the code, as that is not for VB.Net). Before Windows Vista it was possible to 'get away with it' if your users didn't care about security at all (always ran everything in as admin, didn't have virus scanner, etc), but it was not wise... since Vista the protection has got better to make things harder for malware.
There are a variety of potential places that you could store the file, but which would be best depends on how you are going to use it, eg: ...will it only be used by your program? ...will it only be used by a single Windows user? ...if that user is on a local network, should they be able to use the same database on another computer on the network?
Re: connection string does not work for local database running windows 7
That was a great reply with a good link,
Thanks a bunch
Nige.