1 Replies - 264 Views - Last Post: 13 January 2014 - 05:46 AM Rate Topic: -----

#1 jcborland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-December 13

Connecting to an SQL Database 2008 r2

Posted 13 January 2014 - 05:36 AM

Hi,
I'm trying to connect to an SQL database from a c## program and select all the records from a table called companies. I've tried the following code: I've also tried "Server =" as opposed to "Data Source =".


private void btnGetCompanies_Click(object sender, EventArgs e)
        {
            try
            {
                // 
                // Manually set the connect string to the local copy of edoc.mdf
                //
                string selectCommandText;
                selectCommandText = "SELECT * FROM COMPANIES";

                // c:\Program Files\Microsoft SQL Server\MSSQL10_50.JBSOFT\MSSQL\DATA

                string selectConnection = @"Data Source = 127.0.0.1,50000; Database = c:\Program Files\Microsoft SQL Server\MSSQL10_50.JBSOFT\MSSQL\DATA\edoc.mdf; User Id = sa; Password = msl123;";
                SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommandText, selectConnection);
                DataTable companies = new DataTable();
                dataAdapter.Fill(companies);
                gridShowUsers.DataSource = companies;
            }


            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



I've attached a screen shot of the error message.
Attached Image

I can log in to this Database from SQL Server Management Studio using that username and password.

Any help would be great.

Thanks,
Jim.

Is This A Good Question/Topic? 0
  • +

Replies To: Connecting to an SQL Database 2008 r2

#2 jcborland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-December 13

Re: Connecting to an SQL Database 2008 r2

Posted 13 January 2014 - 05:46 AM

Hi,
I've got it sorted I just had to leave off the .mdf.
Doh!
Thanks,
Jim.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1