Abdul Rahman
Abdul Rahman

Reputation: 712

ADO.NET connect results in Instance Failure

When I try to establish a connection to SQL Server using ADO.NET, it shows errors.

This is the code I use:

SqlConnection con = new SqlConnection(@"Data Source=.\\SQLEXPRESS;Initial Catalog=abdul;uid=sa;pwd=sqlserver");
SqlCommand cmd = new SqlCommand();

con.Open();

String str="select * from emp where empname='Abdul'";
cmd = new SqlCommand(str, con);

SqlDataReader dr = cmd.ExecuteReader();

if (dr == null || !dr.HasRows)
{
   MessageBox.Show("No Records found");
}
else
{
   while (dr.Read())
   {
      textBox1.Text = dr[0].ToString();
      textBox2.Text = dr[1].ToString();
   }
}

When I am running the project it shows the following error:

Instance failure.

What do I have to do?

Upvotes: 2

Views: 4145

Answers (1)

PaulB
PaulB

Reputation: 24372

If you are marking the connection string with @ to make it a literal you should only use one backslash in the data source

DataSource=.\SQLEXPRESS

Upvotes: 11

Related Questions