trouble with connection to a db

C

Claudia Fong

Hi,

I'm trying to connect to a sql db with my C# applciation but I'm having
trouble to connect..
It said - Error accessing the database: Cannot open database "LibDB"
requested by the login. The login failed.
but I didn't create any username and password, so I don't know how to
solve the problem? Can someone help me? It's my first time trying to
connnect to a sql database.

Here's my code:

SqlConnection cnn1 = new SqlConnection();

cnn1.ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=LibDB;" +
"Data Source= .\\SQLExpress";

try
{
//open connection
cnn1.Open();
Console.WriteLine("Connection Suceeded");

//open connection
cnn1.Close();
Console.WriteLine("Connection Closed");
}
catch(Exception e)
{
Console.WriteLine("Error accessing the database: " +
e.Message);
}


Cheers!

Claudi
 
S

Stefan Delmarco

What happens if you remove "Initial Catalog=LibDB" from the connection string?
 
C

Claudia Fong

If I remove it, it seems fine.. but does that mean that I don't need to
tell the system the name of the db?

Connection Suceeded
Connection Closed

Cheers!

Claudi
 
W

Willy Denoyette [MVP]

Claudia Fong said:
Hi,

I'm trying to connect to a sql db with my C# applciation but I'm having
trouble to connect..
It said - Error accessing the database: Cannot open database "LibDB"
requested by the login. The login failed.
but I didn't create any username and password, so I don't know how to
solve the problem? Can someone help me? It's my first time trying to
connnect to a sql database.

Here's my code:

SqlConnection cnn1 = new SqlConnection();

cnn1.ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=LibDB;" +
"Data Source= .\\SQLExpress";

try
{
//open connection
cnn1.Open();
Console.WriteLine("Connection Suceeded");

//open connection
cnn1.Close();
Console.WriteLine("Connection Closed");
}
catch(Exception e)
{
Console.WriteLine("Error accessing the database: " +
e.Message);
}


Cheers!

Claudi



If it's the first time you are trying to connect, how comes that you have a user DB called
LibDB? How did you create that DB without connecting to SQL?

Willy.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Claudia said:
I created the db inside the VS 2005..

Cheers!

Claudi

Then you get a connection to the database in the server explorer window,
right?

Right click on that connection and choose Properties, there you can see
what connection string VS uses.
 
W

Willy Denoyette [MVP]

Claudia Fong said:
I created the db inside the VS 2005..

Cheers!

Claudi


Ok, that means you did add a "Dataconnection" from within VS, so it should be possible to
use VS to inspect the connection properties and take them over in your ConnectionString.

Willy.
 
G

Geek

Hi,

plz,check out ur connection string once again.
I think the problem might be Integrated security=true/sspi;
b'coz in ur sql server u may have sql authentication.
check this first.

Regards
Kannan.P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top