Solved

Cannot open SQL Express db using ADO

Posted on 2013-05-15
3
267 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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