?
Solved

Cannot open SQL Express db using ADO

Posted on 2013-05-15
3
Medium Priority
?
278 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
[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 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 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 36

Accepted Solution

by:
Miguel Oz earned 1600 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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