1

I have created a mdf database from visual studio 2013 and stored some informations in the table.

My problem is I can`t connect to my local database and select informations from table. This is my code for connect:

string con = "Data Source=intrebari.mdf;Integrated Security=True;";

string queryString = "Select * FROM dbo.intrebari"; //update as you feel fit of course for insert/update etc

using (SqlConnection connection = new SqlConnection(con))
{
    connection.Open();
    SqlDataAdapter adapter = new SqlDataAdapter();
    SqlCommand command = new SqlCommand(queryString, connection);

    command.ExecuteNonQuery();

    data = new DataSet();
    adapter.Fill(data);
    MessageBox.Show(data.ToString());

    connection.Close();
}

And the error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.

I already unblocked 1433 port with firewall but no succes. Here is a print to my visual studio work.

https://i.stack.imgur.com/tHCK9.jpg

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
user2985344
  • 71
  • 2
  • 2
  • 10
  • 1
    I think you might find some help here: http://stackoverflow.com/questions/8926512/how-do-i-connect-to-an-mdf-database-file Basically, you need to attach the MDF file to a SQL Server instance to use it – Samjongenelen Jan 15 '14 at 11:47

1 Answers1

2

You can't directly use database file. You have to access it through DB server. Update your query string, you can find the proper connection strings Here. If you don't have Management studio installed, you can use the SQLExpress instance installed by default in MS STUDIO, the connection string is HERE .

Naveen
  • 1,496
  • 1
  • 15
  • 24
  • Something like this? string con = "Server=.\SQLExpress;AttachDbFilename=C:\Users\Sebastian\Documents\Visual Studio 2013\Projects\vreisafiiiliardar\vreisafiiiliardar\intrebari.mdf;Database=intrebari;Trusted_Connection=Yes;"; – user2985344 Jan 15 '14 at 11:57
  • Seems I have some connection problem: https://www.dropbox.com/s/gain7tomvgktepx/Screenshot%202014-01-15%2013.53.14.png – user2985344 Jan 15 '14 at 12:02
  • Try this link,note the path given there to the mdf file. http://stackoverflow.com/questions/18443625/a-network-related-or-instance-specific-error-occurred-while-establishing-a-conne – Naveen Jan 15 '14 at 12:08