Solved

Cannot open SQL Express db using ADO

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

20 Experts available now in Live!

Get 1:1 Help Now