Solved

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

Posted on 2014-11-09
7
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 50

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 50

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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