473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Trouble connecting to SQL Server 2005 Express

I am trying to connect to SQL Server 2005 Express with Visual C# 2005
Express using this code:
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\temp.MDF;User
ID=MyUsername;password=MyPass;DATABASE=Test;Integr ated
Security=False;User Instance=True");

conn.Open();

conn.Close();
}

I get an error while calling the method Open Here is the error
"Failed to generate a user instance of SQL Server. Only an integrated
connection can generate a user instance. The connection will be
closed."

so I changed the connection string to

SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\temp.MDF;User
ID=MyUsername;password=MyPass;DATABASE=Test;Integr ated
Security=False;User Instance=False");

and I get error when while calling Open:

"Database 'c:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\DATA\\test.mdf' already exists.\r\nCould not
attach file 'C:\\Projects\\test1\\test\\bin\\Debug\\temp.MDF' as
database 'Test'."

The user account MyUsername has is part of Server Role sysadmin.

I can also connect to this database using Microsoft SQL Server
Management Studio Express without a problem with the same user.

So does anyone know what i'm doing wrong?

Thanks,
BravesCharm

Nov 17 '05 #1
4 13288
You are attempting to connect to an existing database (Test) and to attach
and to connect to another database (Temp.MDF) at the same time. Chose
between Test or Temp.MDF but not both. More infos at:

http://msdn2.microsoft.com/en-us/library/ms254504

http://www.connectionstrings.com/

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF
"BravesCharm" <ma*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I am trying to connect to SQL Server 2005 Express with Visual C# 2005
Express using this code:
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\temp.MDF;User
ID=MyUsername;password=MyPass;DATABASE=Test;Integr ated
Security=False;User Instance=True");

conn.Open();

conn.Close();
}

I get an error while calling the method Open Here is the error
"Failed to generate a user instance of SQL Server. Only an integrated
connection can generate a user instance. The connection will be
closed."

so I changed the connection string to

SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\temp.MDF;User
ID=MyUsername;password=MyPass;DATABASE=Test;Integr ated
Security=False;User Instance=False");

and I get error when while calling Open:

"Database 'c:\\Program Files\\Microsoft SQL
Server\\MSSQL.1\\MSSQL\\DATA\\test.mdf' already exists.\r\nCould not
attach file 'C:\\Projects\\test1\\test\\bin\\Debug\\temp.MDF' as
database 'Test'."

The user account MyUsername has is part of Server Role sysadmin.

I can also connect to this database using Microsoft SQL Server
Management Studio Express without a problem with the same user.

So does anyone know what i'm doing wrong?

Thanks,
BravesCharm

Nov 17 '05 #2
Sylvain,
Thanks for the help. I read the links and have a better
understanding of User Instances now. Can't use User Instances with SQL
security(only integrated). I reworked my connection string to look
like this:

SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\test.MDF;User
ID=MyUsername;Password=MyPast;Integrated Security=False;User
Instance=False");
conn.Open();

and it still doesn't work! I get the error:

An attempt to attach an auto-named database for file
C:\\Projects\\test1\\test\\bin\\Debug\\test.MDF failed. A database with
the same name exists, or specified file cannot be opened, or it is
located on UNC share.

I also tried:
SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\test.MDF;User
ID=MyUsername;Password=MyPast;Initial Catalog=Test;Integrated
Security=False;User Instance=False");
conn.Open();

and got the same error

Nov 17 '05 #3
I was able to connect to the database with

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;User
ID=MyUsername;Password=MyPassword;Initial Catalog=Test;Integrated
Security=False;User Instance=False");
conn.Open();

However, I still can't seem to connect using AttachDbFilename and I
also can't seem to connect using Database Explorer(which I believe uses
AttachDbFilename). The connection strings I've used are:

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;User
ID=MyUsername;Password=MyPassword;AttachDbFilename =|DataDirectory|\test.mdf;DATABASE=Test;Integrated
Security=False;User Instance=False");

and
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;User
ID=MyUsername;Password=MyPassword;AttachDbFilename =|DataDirectory|\test.mdf;Integrated
Security=False;User Instance=False");

and both times I get the error:
An attempt to attach an auto-named database for file
C:\\Projects\\test1\\test\\bin\\Debug\\test.mdf failed. A database with
the same name exists, or specified file cannot be opened, or it is
located on UNC share."

Anyone know what I'm doing wrong?

Nov 17 '05 #4
I found the issue. For some reason I had to put the entire path for
AttachDbFilename and you have to put DATABASE= or Initial Catalog=

So it worked when I did:
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;User
ID=MyUsername;Password=MyPassword;Initial Catalog=Test;Integrated
Security=False;User Instance=False");
conn.Open();

Or

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;User
ID=MyUsername;Password=MyPassword;Database=Test;In tegrated
Security=False;User Instance=False");
conn.Open();

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ann Marinas | last post by:
Hi there, I am currently developing an ASP.NET program that connects to a SQL Server 2000 database. I also have SQL Server 2005 Express installed on the same local machine. Prior to...
2
by: hfk0 | last post by:
Hello, I'm a newbie here and was wondering anyone could help me with this. I have a simple ASP.NET 2 web application running perfectly fine with IIS and SQLServerExpress installed locally on...
14
by: bob | last post by:
Can anyone help me connect to SQL Server??? I am new to all this... it is been a titanic struggle consuming hours & days & .... I am trying to create an SQL database programatically. But my...
2
by: Patrick F | last post by:
Hi, i have SQL Server 2005 and a database set that is called, myCompany the problem is that i cant connect from my page to it, here is from the web.config: ( i have got this connectionstring from...
2
by: brian.newman | last post by:
I've got an app which provides data based on the user who is logged in. So, I need the user data to be on the same db server as the other data. I'm trying to do this using the membership API. ...
8
by: =?Utf-8?B?U3JpcmFtIE1hbGxhanlvc3VsYQ==?= | last post by:
Hi, I am not sure whether this is the correct newsgroup where I need to post my question. Let me know where I need to post for a quick response. I get the following error when I try to connect...
2
by: samadams_2006 | last post by:
Hello, I have a problem that I'm hoping someone will be able to help me resolve. 1) I have a C# Web Site in which I connect to the database: "Install Microsoft SQL Server 2005 Express...
3
by: Me LK | last post by:
I did a search but could not find an aswer that worked so here it goes. I just did an upgrade from 2003 to vs 2005 express. I am using a remote sql server 2000. I specifically upgrade to make...
0
by: mina | last post by:
My application which is written in vb.net 2005 uses sql server 2005 express as a database, this application is multi-user. So i am used 3 xp machine to install my application i can say...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.