Solved

Why I'm getting error when using conn.Open();

Posted on 2014-11-09
7
331 Views
Last Modified: 2014-12-03
I attached to a database that was sent to me in form of mdf and log files.  I put these 2 files in the sql 2012 express' data folder.  I then was able to attach to the file.  It's just odd that the database would show up with name as c:\.....\its-database-name in Sql Management Studio.  It doesn't just show the database name.

In this database Property, File, I set myself(log in) as the owner of this database.  I try configuring myself with all data access role under the Security since I'm not able to connect to the database in the code.  I attached a screen shot for this.  

Here is my code:
                  SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=seatingdb;Integrated Security=SSPI");

                  // create a command object
                  SqlCommand cmd  = new SqlCommand(
                        "select * from customers", conn);

            try
            {
                // open the connection
                conn.Open();
In my C# code I get error when I try to connect to it.  but it won't open . error below;
{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}
0
Comment
Question by:lapucca
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:lapucca
ID: 40431791
I changed my connection string but this time I get message that it cannot open the database but it seems that I am connected to the server.
Message = "Cannot open database \"Customers\" requested by the login. The login failed.\r\nLogin failed for user 'NewPC\\Jaxx'."

                  SqlDataReader rdr = null;

                  // create a connection object
                  SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=seatingdb;Integrated Security=SSPI";

                  // create a command object
                  SqlCommand cmd  = new SqlCommand(
                        "select * from Customers", conn);

            try
            {
                // open the connection
                conn.Open();
0
 

Author Comment

by:lapucca
ID: 40431809
In Sql Management Studio, Under the Secruity, Logins, my user name double click, it does show me as the database owner.  However, if I try to check all the checkboxes below it would give me error.
Attached please see the screen shots.

from the database property, click on Permission and there is nothing there.  I then click on the link to "server permission" and from there I selected my userid on the top panel, at the bottom, it shows "Connect SQL" is checked.  However, I can't check and save any other check boxes.

appreciate help here on how can I connect to this database in my code.
sql-user.jpg
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40431902
Select the same database, right click/Task/detach/check drop option.

Redo per: http://msdn.microsoft.com/en-us/library/ms165673.aspx
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40432247
Repeated question.

You souldn't use that name for a DB. Detach it and attach with a different name (without '/' characters).
0
 

Author Comment

by:lapucca
ID: 40433524
Vitor,
I didn't get to choose the database name.  It is there when I attached it.  In the tree it shows the full path to the mdf file as the name of the database, which I thought was very strange.

egh, What's the difference of attaching that file from management studio vs. cmd?  IS that why it didn't work?  but then why there is an Attach menu item for attaching database if it doesn't work?
0
 
LVL 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 250 total points
ID: 40433643
Hi lapucca,

re:>  It's just odd that the database would show up with name as c:\.....\its-database-name in Sql Management Studio.

It really has to show the database name. Do you have multiple instances installed in your database. Please check to see you have placed in the Data folder of the matching instance.

To answer your question on what is the difference between the attached methods we are discussing, they both ought to be doing the same thing as far as I know. The reason I suggested was sort of trail and error approach in case there are some differences there.

Mike
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 40434450
What's the difference of attaching that file from management studio vs. cmd?  IS that why it didn't work?  but then why there is an Attach menu item for attaching database if it doesn't work?
No differences. In the Attach screen you can click in the script option and will give you the command that SSMS will run in the background.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now