Solved

Cannot open SQL Express db using ADO

Posted on 2013-05-15
3
261 Views
Last Modified: 2013-05-16
I am trying to manually build a simple windows app to read data from a table located on my local machine The program errors out on the con.Open(); line.  When I try hard coding an user id and password, the same error occurs.
Error message: Cannot open database "TeamAllocation" requested by the login. The login failed...
When I bind the data source using the DataSorces wizard in VS, I am able to view the data.

What am I doing wrong?


Using:
Windows Authentication
VS 2012 Pro
SQL Server Express 2012 advanced (Fully installed… I think)
COORHS29RRDS1\SQLEXPRESS ...  

namespace WinAppCSharp01ADO
{
   
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SqlConnection con = new SqlConnection("data source=.; database=TeamAllocation; integrated security=SSPI");
            SqlCommand cmd = new SqlCommand("Select * from EmployeeDetail", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            BindingSource Source = new BindingSource();
            Source.DataSource = rdr;
            dataGridView1.DataSource = Source;
            con.Close();  

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
 
        }
    }
}
0
Comment
Question by:Cainfarm
3 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 39169841
Here are some alternate connection strings: http://www.connectionstrings.com/sql-server-2012  I would look at the SQL Server Native Client 11.0 OLE DB Provider strings near the bottom.  This assumes that you are on the same machine as the database server because SQL Server Native Client 11.0 is installed with the database server.  If you are not on the same machine then you will probably need to download and install the SQL Server Native Client 11.0 on your machine.
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 400 total points
ID: 39169889
You are not specifying the server attribute (or data source) in your  connection string
The easiest way to get the connection string is go to your existing working data source (in server explorer), select it and look at the connection string property in the properties windows, Then copy that value into your code and it will work fine.

Note:The easiest way to get the connection string is using the Server explorer window in Visual Studio (View-->Server Explorer menu) and connect to the server from that window. Then you can see the connection string in the properties of the connected server (Alt+F4 with your connection selected).
0
 

Author Closing Comment

by:Cainfarm
ID: 39171788
Connections strings.com is a great resource and may help me in the future ... Copying the Data source string that Visual Studio provided the exact syntax.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

16 Experts available now in Live!

Get 1:1 Help Now