简体   繁体   English

ADO.NET:打开SQL连接

[英]ADO.NET: Open SQL Connection

i want to do some sql code run in my webservice in c# 我想在C#中在我的Web服务中运行一些sql代码

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. System.Data.SqlClient.SqlException:建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与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/ http://connectionstrings.com/

You might want to check the configuration of your SQL Server as well. 您可能还需要检查SQL Server的配置。

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 开始-> SQL Server xxxx->配置工具-> SQL Server配置管理器

Where xxxx is the version of SQL Server you are using. 其中xxxx是您使用的SQL Server的版本。

In the tree look at the 在树上看看

-> SQL Native Client xx Configuration -> Client Protocols -> SQL本机客户端xx配置->客户端协议

Named Pipes is listed. 列出了命名管道。

Set it to being Enabled 设置为启用

Then under 然后在

SQL Server Network Configuration -> Protocols for xxxxxx SQL Server网络配置-> xxxxxx的协议

xxxxxx Being the name of your SQL Server database instance name. xxxxxx作为您的SQL Server数据库实例名称的名称。

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. 您将需要重新启动SQL Server数据库,以使其接受命名管道调用。

Might be worth a try. 也许值得尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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