Solved

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

Posted on 2014-11-09
7
381 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 34

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 48

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 34

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 48

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

829 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