Solved

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

Posted on 2014-11-09
7
345 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 46

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 46

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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

14 Experts available now in Live!

Get 1:1 Help Now