Why cant I create a database with mdf file? - sql

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).

Related

Upload .db file to Azure SQL

I have a .db file, with a bunch of tables and data (and a few one to many relationships), instead of rewriting everything, is there a way for me to just upload this database directly to Azure SQL?
My suggestion is to install SQL Server Developer Edition on your laptop or local computer and import those database files to that SQL instance as explained here, if they are Dbase files.
Once you have imported the .dbf files to the local SQL Server instance, use SQL Server Management Studio to deploy the database to Azure as shown here and here, or you can use Data Migration Assistant to migrate to Azure as explained here.

SQL Server encrypt mdf file

I have a database SQL server ,so I have 2 files (mdf and log).
how to protect mdf file from attach
SQL Server Transparent Data Encryption can prevent people from using the files elsewhere unless they have the appropriate certificates.
But if they've gained access to the server to the point where they can access those files directly, you have to consider the possibility that it's completely compromised.

VB.NET Creating a local database in VS2013 or in SQL Server Management Studio

I am about to begin a personal project to build my skills in the .net environment. I am familiar with SQL Server Management Studio and how to create a database in it but I discovered how to make a local database in Visual Studio as well. My program is only going to require local database access as it will be used for individual inventory systems rather than connected ones. Am I ok to use the onboard tools in visual studio and create a local databasse or should I be using the SQL Server Management Studio?
When you use the Local Database item template in VS, it creates a SQL Server CE data file (SDF) and adds it to your project. When you use the Service-based Database item templete, it creates a SQL Server (Express) data file (MDF) and adds it to your project.
The advantage of using the VS tools is that the data file becomes part of your project and can therefore be easily deployed with the compiled application. As such, the database is basically part of the application.
If you choose SQL Server CE then you don't need a server installed on the user's local machine. They can install SQL Server CE or you can install it with your app if you want, but you also have the option of simply deploying a DLL with your app and it will work.
If you choose SQL Server Express then the user actually needs a SQL Server instance installed on their machine. To be honest, I'm not 100% sure whether that instance must be SQL Server Express or it can be a full SQL Server instance too. It would usually be SQL Server Express though, which you can install and even download automatically when you install your app, depending on the deployment method you choose.
If you use the VS tools to create an MDF data file then your connection string will contain the Data Source and AttachDbFilename attributes. The Data Source will generally be ".\SQLExpress", i.e. an instance named "SQLExpress" on the local machine. That instance name is not required, although it is the default for SQL Server Express, but it must be on the local machine. The MDF file gets attached at run time and detached again when you're app is done with it. It will also usually be attached to a user instance, which means that other users can't see it, even when it's attached. Note that, in later versions, the LocalDB feature of SQL Server may also be utilised.
If you create your database in Management Studio then it's not actually part of your app. It will be permanently attached to the SQL Server instance so, everyone will be able to see it and open it, assuming permissions allow. Creating the database during deployment will be an extra step in that case. You might create a backup and restore that during deployment or generate SQL scripts that get run. In this case, your connection string will contain the Initial Catalog attribute to specify the name of database to connect to, as well as the Data Source attribute. This option is required if you want multiple clients to be able to connect to the database.
In short, if you are only going to be accessing a database from the local instance of the one application then creating a database in VS is OK and probably a good idea. Whether you choose SQL Server CE or SQL Server Express may well depend on what level of functionality you need.

OLEDB connection to mdf file

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.

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