简体   繁体   中英

ADO.NET: Open SQL Connection

i want to do some sql code run in my webservice in c#

the code is just

    [WebMethod]
    public void GetCustomers()
    {
        SqlConnection MyConn = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Documents and Settings/Administrator/My Documents/Visual Studio 2008/Projects/WebService2/WebService2/App_Data/Database1.mdf;Integrated Security=True;User Instance=True");

        MyConn.Open();

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = MyConn;

        cmd.CommandText = "delete from t1 where name='1'";   //just see ["+month
        // [mon+"] it's imp

        cmd.ExecuteNonQuery();
    }

now i get error like

System.Data.SqlClient.SqlException: 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)

Correct Format:
Server=.\\SQLExpress;AttachDbFilename=c:\\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Try:

"Data Source=./SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\Projects\\WebService2\\WebService2\\App_Data\\Database1.mdf; Database=YourDatabasName;Integrated Security=True;User Instance=True"
For more details see:
http://connectionstrings.com/

You might want to check the configuration of your SQL Server as well.

The error remarks the issue could be the named pipes not being active , this is the default setting.

You can always test this fix by going;

Start -> SQL Server xxxx -> Configuration Tools -> SQL Server Configuration Manager

Where xxxx is the version of SQL Server you are using.

In the tree look at the

-> SQL Native Client xx Configuration -> Client Protocols

Named Pipes is listed.

Set it to being Enabled

Then under

SQL Server Network Configuration -> Protocols for xxxxxx

xxxxxx Being the name of your SQL Server database instance name.

Check that Named Pipes are Enabled there also.

You will need to restart your SQL Server database in order for it to accept Named Pipe calls.

Might be worth a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM