2

i m using vs2010 and i have tried all the possible ways nearest to me to connect with the database from visualstudio plz help me heres the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cs = new SqlConnection("Data Source=Sql Server(SQLEXPRESS); Initial Catalog=school");
            cs.Open();
                MessageBox.Show(cs.State.ToString());
                cs.Close();
        }
    }
}

it doesnt connet and give error: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

help

1
  • The exception you get says exactly what to do. Check your sql server instance name, check your connection string etc. Commented Dec 2, 2010 at 18:06

3 Answers 3

3

Hey i have done it its very simple i research for escape sequence and the solution is instead of one backslash we should place 2 backslash lol maybe this would aswell help others and is necessary if u r running express

SqlConnection cs = new SqlConnection("Data Source=Your PC Name\\SQLEXPRESS; Initial Catalog=school;Integrated Security=true")

Remember to put 2 backslash after ur PC name :) :)

thanks for ur answers pals

Sign up to request clarification or add additional context in comments.

1 Comment

You can use a "." instead of "Your PC Name" - that will also work when you install program+database on another machine.
1

Your connectionstring is wrong. Have a look at http://www.connectionstrings.com. Also, if you just installed an SQL server (or SQL Express), make sure you have the server set up for connections over IP. You can check that in the SQL Server Configuration Manager:

Open the SQL Server Config Manager and select SQL Native Client Configuration > Client Protocols from the tree. In the window on the right side, make sure TCP/IP is enabled.

Check the same thing for SQL Server Network Configurations > Protocols for SQLEXPRESS

2 Comments

tcp ip was enable for native client but not for network configuration i enable it and i also enable Named pipes and VIA aswell in protocols for SQLEXPRESS, but same problem it is unable to connect with the server .. i also want to know what should be the name of a server i write my computer name aswell Arsalan-PC but same prob help buddy
(1) What version of SQL? 2000/2005/2008. (2) SQL or SQL Express? (3) Did you specify an instance name at installation? Try to connect using "Data Source=Arsalan-PC\SQLEXPRESS; ..."
1

Try connecting this way:

SqlConnection cs = new SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=school;Integrated Security=true")

I assume SQL Server is installed on your pc, if not then try:

SqlConnection cs = new SqlConnection("Data Source=SomeIpAddress\SQLEXPRESS; Initial Catalog=school;Integrated Security=true")

2 Comments

when i tried the first one after back slash and "\SQLEXPRESS" error comes up on "S" of sql says "Unrecognized escape sequence" and yeah i have sql server 2008 but i tried aswell second one but same prob as the first one plz tell me what to do ??
Sorry. When you try to set connection string from code, you should use double slashes, like .\\SQLEXPRESS, from configuration file, you should use single slash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.