SQL Database location - sql

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

Related

Create a Sql Server Db from WIX in a specific location

I'm trying to have WIX create a database in a location the user specifies. I've got it working for the most part using some CustomActions and passing parameters to a sql script. But am having problems with permissions on the directory I'm attempting to create the DB in.
Searching for a resolution to this problem lead me to a post on here that I had not seen before today (Wix: create a sql server database at a specified location) where it seems like someone accomplished the same thing I'm wanting to do.
It is not clear from that post how the user was invoking the script? I am hoping they were doing something like the code code snippet below and I just don't know how to pass values to the script. If so, that would greatly simplify my task.
<sql:SqlDatabase>
<sql:SqlScript>
</sql:SqlDatabase
Can someone tell me how to pass values to a SqlScript when not using a customAction to invoke the script via sqlcmd.exe?
Thanks.
For the future reference of anyone else wishing to do this there are a couple of ways to create a database at a specified location. The simplest of which is using the wix tags that I did not know existed:
<sql:SqlFileSpec Name="DataBaseFile" Id ="mdfFile" Filename = "[DBDIR]TestDataBase.mdf"/>
<sql:SqlLogFileSpec Name="DataBaseLogFile" Id ="ldfFile" Filename = "[DBDIR]TestDataBase_Log.ldf"/>
This works perfectly as long as the directory that the database is being created in is not under "Program Files" or "Program Files (x86)". Both of these locations seem to have special permissions to them that don't allow SQL Server to create a database there. This theory can be tested by trying to create a database using Sql Server Mgmt Studio in a folder somewhere under Program Files and looking at the error that is produced. Which is something like this for me:
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file
I'm still trying to figure out how an installer can accomplish putting a database under Program Files as I'm sure it's been done before.

How do I assign a URL to a file stored in a SQL database?

I have a self-made document management system that stores files of all sorts in an SQL database. The database stores 4 basic rows of information, namely:
FileID
FileName
FileSize
FileType
FileContent
I want to use viewer.JS to preview files, but it requires that I have a URL to access my file, and I have no idea how to assign a URL that would access a file of my choice in a row in a database. I'm building on a home-grown PAAS, thats built on .net.
I've heard that the URL rewrite module for IIS (I'm on 7) may solve my problem, but cant seem to crack it.
Any help on my problem would be much appreciated.
IMHO IIS rewrite would not help you. Assuming you're using .net, you'd need to build a simple website and use routing in a view.
The url could be something like /Document/{fileid}. when this path is encountered, the router would call your view and you can return the file's content.
Store file in SQL Server database using .Net MVC3 with Entity Framework has some info on a model/controller.
Note: There are some security issues with this approach.

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.

Access a accdb file (MS Access 2007) from resources

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"