Access a accdb file (MS Access 2007) from resources - vb.net

probably this is a simple question but because i have never used resources like that i can not think how should i do it.
I am writing a very simple program that connects to a accdb file (Microsoft Access 2007 file) and returns some results in a datagridview. Everything is fine. Now because we have to deploy this program in many computers i publish it so everyone we want can install it and have updates. What i wanted to do is to make the database file part of the program which i did it by adding it to the resources. My problem is that i do not know what connection string to enter in order to access it in my resources.
My previous connectionstring before i deploy it was this
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\bl.accdb;Persist Security Info=True
What should i change in the data source in order to access the same file in my resources? Or am i wrong and this is not possible?
Thanks

You can do it. All you need to do is point the datasource to the location of the resources direcory, which by default is adjacent to the application itself. You could determine this at runtime by using:
Application.StartupPath & "\Resources\DatabaseName.accdb"

Related

Visual Studio- moving local database

I am working on an app in VS which includes a SQL database stored locally on my C drive. I can publish the app and it works fine on my own computer, but because the database is stored locally, I cannot run the app from any other computer (SQL exception 52). I would like to move the app to a network drive so it can be accessed by multiple users.
I have tried to move the database by changing the default database location in the SQL server object explorer. I’m wondering if I should have SQL server express LocalDB installed?
I have no programming training but have been dumped with this project at work as I have used VBA before, so I’m sorry if this question it stupid. I would really appreciate it if someone could point me in the right direction!
When accessing your database, rather than using just the database name, you should specify the filepath aswell, like this:
Public Filename As String = $"[**FilePath**]ActiveFitness.accdb"
This tells the programme where to look for the database, and is better practice than relying on the default location.

SQL Database location

In Visual Basic 2010 Express, I use SQL statements to read, write, edit, ... a mdb database file. However, at the moment, it's pointing to a location on my local directory.
Is there a way to embed the file into the VB program and change the SQL statement to write to it?
Sort of like how in HTML, you can move the whole website folder and so long, as the root contains the folder "images" for example, then it knows to look in there...
I don't know how you are currently stating the connection string for each of your sql statements, but one approach that you could take would be to place the .mdb file in the root folder of your application and then use the generic
Application.Info.DirectoryPath
to provide the basic location of the mdb file. A better idea however (especially to avoid problems with with the UAC and permissions would be to place the mdb file in the all users application data folder and use the equally generic pointer
Application.CommonAppDataPath
With some judicious experimentation you should be able to arrive at a solution that best meets with your own requirements.
Your reference to your MDB file will likely be contained in your app.config file as an SQL connection string. In there you'll find the full path to the file itself. If you change that to a relative path (say, just the name of the MDB file itself, no folders or anything like that) then it should look in the directory of the executable.
So, if your connection string was:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyFolder1\mydatabase.mdb;User Id=admin;Password=;
You'd change it to:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydatabase.mdb;User Id=admin;Password=;
No you can't use an Access database as embedded as a resource.
You could set the connection string like this
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\myDatabaseFile.mdb;Persist Security Info=False;
and then in your startup code you could set the substitution for the DataDirectory with a call to
Dim commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
AppDomain.CurrentDomain.SetData("DataDirectory", commonAppData)
Lacking this call then your DataDirectory predefined value will be the current application startup folder.
Some info on |DataDirectory| sustitution string

Query regarding multiple DSN (domain source name)

I'm new to ODBC and DSN's in general.
We have a product A that accesses a database "T" via a DSN D1 that has already been defined.
I'm now writing a test app testA that exercises various functionalities of product A and also needs to validate various database entries made by A.
For this, I am trying to access the same DSN D1 (that was setup for database T) to read from db and do the necessary validations.
I'm however having issues with that. I've setup the dsn, but when I do a SQLConnect with this dsn, I get a return value of -1.(I dont see any corresponding values for this return type either)n
Any clues on what this means and if I'm doing something wrong.Can we have 2 different processes or apps connect to the same DSN?
You can test access using a "udl" file. These files are largly misunderstood IMHO. They are simply a script that is used to launch the (db) connection library. You can create them by creating an empty file in notepad and changing the extension to .udl from .txt. You then double click on the file and configuer access to your database. (they launch a build in configeration applet). In my experience if a .udl file connects then any application will connect or 100% there is a problem with the application. If the udl file will not connect, then 100% there is a firewall or something probibiting access to the account being used to try to connect.

VB.net connection to embedded database

I have a stand-alone Windows Form app written in VB.NET that currently connects to a local Access DB (.mdb file) and consumes data from several of the tables. It never writes to nor modifies the DB. I'm trying to figure out how to secure this DB so the user has no access to it. I'm thinking the best way is to store the DB as an embedded resource within the project. However, I can't figure out how to make this work.
I've added the .mdb file to the project resources and set its properties to "Embedded Resource" and "Do not copy." But now how do I reference the DB to create the connection?
Before I used the connection string "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=MyDatabase.mdb"
How do I write the connection string now with reference to an embedded resource?
"I'm thinking the best way is to store the DB as an embedded resource within the project."
This usually relates to old skool security, in the olden days DotNetNuke developers that used Access dBs as their backend actually renamed the .mdb to .resources as you cant download a resource file from the web.
"Would it be better to export the DB to xml or something like that and then use a stream reader as opposed to a database connection?"
No. You cant read an Access file from a stream And an XML dB has even less concurrency support than an Access Database.
If you really need to protect the data, then you've absolutely chosen the wrong data store.
#JohnBustos wrote "Store the DB remotely and have the program access it over the internet."
Please do not do this. If anything put it on a network share and restrict access. Then look at upgrading the access dB to SQL Server.
Yes, I have had the same problem and I converted the database into a XML or even a TXT file that is embedded. It works very fine !
Sorry, I never closed this out.
My solution was even simpler than those proposed. You can add the .mdb file to the project itself and not mess with the whole "Resource" business. Then set the file's Copy to Output property to "Do not copy". So the .mdb gets compiled into the app, but file itself is not available to the user.

SharePoint temporary location

I am working with SharePoint 2010. I need to get the data from excel file stored in sharepoint library. I have plans to accomplish this by using OLEDB connection. So I need to download the excel file from doc library and store it in a temporary location and then access this temp file to get data from it. Now my question is which is the best ans safe location to stored the downloaded file? So far I have used SharePoint's layouts directory, C:windows:TEMP and even Path.GetTempFileName(). But the result I get this is " ACCESS DENIED!". Any ideas in temporary location? Is there any other location which is safe?
Thanks
Check whether you are running the file saving code with Run with elevated privileges and the app pool account has write access to the provided path. It's not a good practice to store temp files inside 14 hive.
Why don't you use Excel Web services to read the excel file if your farm has excel services enabled.
Refer http://msdn.microsoft.com/en-us/library/ms500767%28v=office.14%29.aspx
Or else try to use third party libraries like EPPlus where you can open the excel file using a stream rather than from a physical file location.