Connect to a local database

128 views Asked by At

I've been trying for a few hours now to setup a local database in my new app.

I created a new local database (project->add->local database), defined the table columns and primary keys.

Then I've moved to the Data source explorer to create a new connection to the DB.

But whenever I try to connect to the DB it says:

"A network-related or instance-specific error occurred while establishing a connection to SQL server. The server was not found or was not accessible...."

This is my initial code just for the connection:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace WindowsFormsApplication4
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            string constr = WindowsFormsApplication4.Properties.Settings.Default.Database1ConnectionString;

            try
            {
                SqlConnection con = new SqlConnection(constr);
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

I'd really appreciate a little help here, I've tried to Google this question without any success.

2

There are 2 answers

2
Jota WA On

Is the value on your ConnectionString correct/valid?Debug it and confirm.

0
Kaspars Ozols On

The error suggests that code can not reach the database. You should check following:

1) Is your database up and running?

2) Check value of constr variable. Is this the right connection string you expect?