OLEDB connection to mdf file - sql

I am working on a development project that needs to connect to a sql database. The thing is,is that the database will be stored only on the local machine. I also only have access SQL Server express, and I cannot use a .sdf database because the limit on it is 4Gb. I created the .mdf in visual studio with correct format and layout and placed the files on the development machine. Now, the only way to connect to it (or any database) using the software we have to use is with an OLEDB connection. I tried connecting to it with SQL Express, but it errors out probably because it is not an .sdf file. Does anyone have any ideas? I have been all over the internet and im starting to wonder if it is possible. I can connect to an mdf file in visual studio C#. Thanks a bunch.

Related

Why cant I create a database with mdf file?

I have a feature in visual studio which I have never really understood.
I am able to 'right-click' on the App_Data folder and then I am able to select 'Sql Server Database'. I dont really understand how I can create a db using just an mdf file? I thought the sql service was responsible for manipulating these files? And that you have to create the db using the 'sql management studio' interface?
Im confised as to how we can essentially just have a lone file and run a db from it for a website?
You're application is still connecting through the SQL Server service but it can instruct the service to attach to a specific mdf file at runtime through a connection string. e.g.:
"Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;"
All SQL Server databases are represented as one (or more) .mdf files and usually .ldf files as well (ldf is the log file, mdf is the data file.) An .mdf file is a file but it is highly structured and maintained by SQL Server. The way SQL Server uses this file is very different from serving up CSV data, as a simple example. SQL Server breaks the file into pages and serves the requests for reads and writes via this paging system. It is indeed like a file system within a file system. If you think about it it does all make sense. The data has to get persisted to disk and that disk persistence has to come in the form of a file or files.
When you installed Visual Studio you also installed SQL Server Express. This gives you the ability to create and use SQL Server databases.
If you were to deploy your application you would then also need to have a SQL Server (Express) install on the web-server you were using (at least because you don't want to use your development database server in production).

How to create a local server for website in Visual Studio 2010

I am trying to make a website using local server in visual studio 2010. I posted before about the errors that I got during debugging.
Now I just want to make sure that I am doing the correct thing to create a local SQL server.
I created a .mdf database in the App_code, configured the web.config and created a table for the .mdf database. Is that just what I need to do to create a local server?
thanks
If you have SQL Server installed on the machine, and you have the database in your App_Data directory then you can just connect it in the connection string:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Connecting to SQL Server 2008 Express from VB 2008 Express

I'm using VB 2008 Express for our College Project. I'm also running SQL Server 2008 Express and have installed SQL Server Management Studio and used it to create my database.
Both software have been installed and running locally. I'm trying to connect to the database from VB 2008 Express. Database connection wizard have 3 options:
connecting to Access db
connecting to SQL Server 3.5 compact db and
connecting to SQL Server db file.
There are no problems connecting to the first two but when I try to connect to my SQL server DB file (.mdf) it throws the following error ("Unable to open the physical file .... Operating system error 32:(failed to retrieve text for this error. reason:1815.......an attempt to attach an auto-named database for file ...... .mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share")
I appreciate if anyone who had the same experience and overcome the problem could point me in the right direction.
Thanks very much.
Little Critter.
You need to detach your database from the server. Since you created the file there it is already running on the SQL Express server. Operating System Error 32 is a sharing violation and it means that the file is in use.
Try this:
Open Sql Express 2008 Managment Studio and find your database in the Object Explorer tree.
Right click on your database, choose tasks, and then select "Detach"
When the "Detach Database" dialog appears, select the checkbox that says "Drop Connections"
Click ok.
Your database should detach from the database server. Now the server will no longer bring that database up automatically and you should be able to get exclusive access to it.
Note that if you want to make more edits to the MDF you will have to reattach the database or you will need to use the tools built into VB 2008 Express. Because of this most people would choose to leave the database attached and not run it as a "User Instance". Check out this link for alternative ways to get to your data: How to: Access and Initialize Server Explorer/Database Explorer.
Anyway, you may have your reasons for setting it up this way. The steps outlined above should fix you up. Good luck with your project!

Why can't I open the same database files (.mdbs) in multiple environments at the same time?

This kind'a sucks. If I connect to a SQL Server DB (.mdb file) through VS.Net's server explorer, then I can't connect to the same file via SQL Server 2008 Management Studio at the same file. The file is locked.
For example, I get the following error from SQL Server Management Studio
CREATE FILE encountered operating
system error 32(The process cannot
access the file because it is being
used by another process.) while
attempting to open or create the
physical file 'C:\SQL Server 2000
Sample Databases\NORTHWND.MDF'.
(Microsoft SQL Server, Error: 5123)
I'm playing around with LINQ to SQL. So you would need to connect to VS.Net server explorer to drag in your table objects and create the .dbml files. However, if I want to query the database using old school SQL from management studio, I can't... I get the above error. Ditto if I try to connect using LINQPad (great tool...but useless thanks to this irritating file lock).
So does anyone out there know how I can connect to the same .mdb file from multiple programs like VS.Net's server explorer, SQL Server's management studio and LINQPad all at the same time?
Thanks!
By the way, this site is simply awesome and I love the fact that they made it in ASP.Net and used LINQ to SQL for data access... okay, off topic. Sorry.
The sql engine will lock the file once connected to it. nothing prevents you from attaching a file to an instance of SQL server and then connect to it from multiple applications.
The clue is in the name "SQL Server" - its seems that you can't attach the DB to two places which is not entirely unreasonable because you can't serve it twice (given that the server has responsibilities for things like transactions, logs, locking, etc).
So...
You want to "run" the database in an SQL Server instance and then connect from Visual Studio to that instance and then to the file rather than open the file directly (the option is there because it simplifies some deployment models) at which point you should get what you need.
As an aside, its perfectly possible to to "old school SQL" from within Visual Studio - although I'm not aware of an equivalent to LinqPad

visual studio pointing to my sql server

in visual studio i have my code point to a sql server mdf file (in the APP_DATA folder). and i keep having to syncronize this local data to my server.
is there anyway in visual studio i can just give it the connection string to my real server so i can debug directly against my server.
i know this may be a bit slower but it also might be easier as it eliminates going back and forth.
Sure, just change the connection string to point to your server