Solved

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

Posted on 2014-11-09
7
365 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 47

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 47

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

813 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