在Web服务中使用sql [英] use sql in web service

查看:111
本文介绍了在Web服务中使用sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#中在我的Web服务中运行一些sql代码

代码只是

 [WebMethod]

 公共 无效 GetCustomers()
    {
         SqlConnection MyConn =  SqlConnection(" );
        MyConn.Open();
          SqlCommand cmd =  SqlCommand();
       cmd.Connection = MyConn;
       cmd.CommandText = " ;

        cmd.ExecuteNonQuery();
 } 



现在我出现了类似
的错误
System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接. (提供者:命名管道提供者,错误:40-无法打开与SQL Server的连接)

解决方案

尝试使用AttachDbFilename属性.
看到这里
链接 [这里 [ [[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'"; cmd.ExecuteNonQuery(); }



now i got 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)

解决方案

try with AttachDbFilename property.
see here
Link[^]

Thanks
--RA


In ASP.Net, you should not provide an absolute address string for your database.
Have a look here[^] at how you would be setting up a database connection in ASP.Net.
This[^] describes using connection strings in the config file and should help you get started as well.


这篇关于在Web服务中使用sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆