Link to home
Start Free TrialLog in
Avatar of pclarke7
pclarke7

asked on

Connecting MS SQL database to C# application

I am new to c# and MS SQL databases.

I have created a database called Rulesdb2 and have attached it to my c# application. I had previously attached it as a local database but have changed this to a direct connection by specifying NO to "Copy the file to your project" prompt

Below is my connection string:

Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Rulesdb2.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True

However when I run the application I get an error indicating that the application is looking for the database in debug\bin directory as shown below.

"An attempt to attach an auto-named database for file C:\Users\User\documents\visual studio 2010\Projects\RBS07\RBS07\bin\Debug\Rulesdb2.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

Can someone explain where I am going wrong ?

regards
Pat
Avatar of XGIS
XGIS
Flag of Australia image

Hello Pat,

it seems that you have an instance of SQL Server installed,
Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Rulesdb2.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True
the below connection string will allow you to connect via the instance.

 Data Source=(local);Initial Catalog=Rulesdb2;User ID=username;Password=password" providerName="System.Data.SqlClient

Open in new window


Cheers Trentos
Avatar of pclarke7
pclarke7

ASKER

Hi Trentos,
sorry that doesn't work and I don't understand why the original connection string does not work. I am totally confused about whether it should be a local database connection or not ?

What I want is a database that I can read from and update/Write to from my application. At some point in the future I will need to locate this database on a remote server. Can you tell me the benefit of a local connection V a direct connection to my database. Is there any documentation available on this that will guide me through this ?

regards
Pat
Try this
Data Source=(local)\SQLEXPRESS;Initial Catalog=Rulesdb2;Integrated Security=False;User ID=username;Password=Password

If this does not work then make sure you are not using Windows Auth mode in which case you would set Integrated Security to True and leave out your user name and password.

Also, if you did not stick with the default name for your sql server, it might not be SQLEXPRESS.  a good way to find that if this doesn't work is to open SQL Server Management Studio and click the combobox to show servers.  You will use (local)\InstanceName


Plus: take out any reference to the database file.  That file should not be part of your project.  Unlike Access which uses an engine to help you process a file, MS SQL is a server.  It uses the file internally and that file should for the most part never be messed with.  The server sends the data directly to your program.  If you have the file in your project, it needs to come out.
Hello pclarke7, attached is a sample .net 4.5 c# website.
It has a sql express mdf database in it and a connection string and a grid to display the data

the sample is based on all default microsoft and sql install locations.
when it is opened with visual studio it should run and work

i used vs2012..see how you go.

It is important to not that there are about 8 files with a txt extension that needs to be removed
this was done so this 1920's website could upload the zipfile.

files for rename
Rulesdb2.csprojResolveAssemblyReference.cache.txt
to Rulesdb2.csprojResolveAssemblyReference.cache

the same applies for all the ones with .txt extensions, just remove the .txt BEFORE you extract the ZIP file.

cheers Trentos2
Rulesdb2.zip
ASKER CERTIFIED SOLUTION
Avatar of XGIS
XGIS
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks  XGIS and VBJim,
finally got it sorted.

 I think the problem was due to the fact that I had a number of versions of MS SQL Server installed on my PC and had at various time alternated between a local copy of the database and a direct link to the database. I backed up the database and then uninstalled all versions of MS SQL SERVER deleted all MS SQL SERVER services and removed all folders linked to MS SQL SERVER. I then installed MS SQL Server 2012 (32 bit) , restored the database and created a direct (non local) connection. Now working like a dream, and I can eventually able to move on.

Thanks for your help on this