2

I am trying to connect a .sdf file with my winform. here's what i am trying to do :

SqlConnection con = new SqlConnection();
        con.ConnectionString=@"Data Source=D:\TestWinform\MyDB.sdf;Persist Security Info=True";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into User(Name) values('" + txt.Text + "')";
        cmd.Connection = con;


        con.Open();   // giving exception in this line
        cmd.ExecuteNonQuery();
        con.Close();

but i am facing a problem, on con.Open() its giving this exception

A network-related or instance-specific error occurred while establishing a connection
to SQL Server. The server was not found or was not accessible. Verify that the instance
name is correct and that SQL Server is configured to allow remote connections. 
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 

what should i do, i guess that it couldn't find the file but the .sdf file is there on that location So pls help me on this

Arindam Das
  • 699
  • 4
  • 20
  • 39
  • Well how do you login into the database? You have no login credentials in your connection string. – Arran Jun 04 '13 at 11:31
  • that's also one of my query, i am getting that filepath as connection string but i also have a password which is not coming in the connection string – Arindam Das Jun 04 '13 at 11:34

1 Answers1

7

Your connection object is wrong. You are using Sql compact so you have to use SqlCeConnection. The SqlConnection used to connect to SQL Server.

Follow the link to sort your problem.

Here is sample.

Community
  • 1
  • 1
Toan Vo
  • 1,270
  • 9
  • 19
  • @ArindamDas: Code sample at here : http://arcanecode.com/2007/04/13/sql-server-compact-edition-with-c-and-vbnet/ – Toan Vo Jun 04 '13 at 11:43