-1

I've been trying to make a connection with my SQL Server database (Mynewdatabase) using the default connection string that was generated (supposedly) when I added the data source for this project. However, I'm thus far unable to make a connection: I'm getting an error that "the server was not found or was not accessible".

This my code. Thanks so much for any help you can offer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace Parsevcf2sql
{
class Program
{
    static void Main(string[] args)
    {
        // here is where I get what I believe is an appropriate connection string
        string cs = Parsevcf2sql.Properties.Settings.Default.MynewdatabaseConnectionString;
        SqlConnection myconnection = new SqlConnection(cs);

        Console.WriteLine(cs);

        try
        {
            myconnection.Open();
            Console.WriteLine("This worked!");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2705143
  • 13
  • 1
  • 2
  • 4
    what is your connection string??? –  Sep 11 '13 at 20:51
  • What database engine? (There are many "SQL databases".) What connection string are you trying? Have you searched here for connection string problems? Have you tried [ConnectionStrings](http://connectionstrings.com)? – Ken White Sep 11 '13 at 22:22

1 Answers1

1

This might help you out, change localhost to your address and database name as well.. But Please check sql server must be in running mode

{
    // connection string!
    SqlConnection myConn = new SqlConnection("Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=Mynewdatabase;");

    try
    {
        myConn.Open();
        Console.WriteLine(myConn );
    }
    catch (System.Exception)
    {
       // some exception
    }
    finally
    {
        if (myConn.State == ConnectionState.Open)
        {
            myConn.Close();
        }
        myConn.Dispose();
    }
}

or check out this tutorial

Digital Alchemist
  • 2,324
  • 1
  • 15
  • 17
  • Everyone,First off, thank you so much for your very prompt replies! Unfortunately, I did not expect them so fast: so I went home shortly after posting this only to find many people asking for my Connection String is:Data Source=|DataDirectory|\Mynewdatabase.sdf;Password=********;Persist Security Info=True – user2705143 Sep 12 '13 at 17:53
  • Everyone,First off, thank you so much for your very prompt replies! Unfortunately, I did not expect them so fast: so I went home shortly after posting this only to find many people asking for more information. 1) My Database is SQL Server Management Studio (2012), and my Connection string is: is:DataSource=|DataDirectory|\Mynewdatabase.sdf;Password=********;Persist Security Info=True – user2705143 Sep 12 '13 at 18:00
  • @user2705143 just replace your values with [ new SqlConnection("Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=Mynewdatabase;"); ] in code – Digital Alchemist Sep 13 '13 at 04:40
  • Well, I've tried that, but it didn't work, and this seems to be because I'm not using SQLEXPRESS - is that correct? Since I'm using SQL Server Management Studio 2012, how can I adjust this. I don't know where my localhost link points to and whether that would really be able to connect to my SQL Server Management Studio instance. . . . – user2705143 Sep 13 '13 at 19:30
  • @user2705143 please visit these posts , they might be help to you [Post #1](http://stackoverflow.com/questions/16588503/unable-to-connect-to-sql-database-c-vs2012-sql-server-2012) and [Post #2](http://stackoverflow.com/questions/17265068/connect-to-sql-server-2012-database-with-c-sharp-visual-studio-2012) – Digital Alchemist Sep 16 '13 at 04:39
  • @user2705143 Make sure your Sql server is running and if not localhost , replace it with Computer Name or Login String that you uses to connect to Management Studio – Digital Alchemist Sep 16 '13 at 04:40
  • Thanks for your help, everyone! It appears that my problem was a combination of not knowing where the server was and using the wrong connection string as a result! All worked out now! – user2705143 Sep 21 '13 at 20:59