无法使用创建的Web服务

时间:2016-01-25 18:56:21

标签: asp.net sql-server web-services

我正在尝试自学一些关于Web服务的基础知识,并设置一个只访问SQL服务器并从数据库中提取项目的简单应用程序。

但是我在尝试连接时遇到了这个错误:

“命名管道提供程序,错误:40 - 无法打开与SQL Server的连接”---> System.ComponentModel.Win32Exception:找不到网络路径“

我将此连接字符串用于其他解决方案并且工作正常,但不适用于Web服务,并且在搜索Stack时我找不到适合此问题的解决方案。

      <connectionStrings>
<add name="Snafoo" connectionString="Data Source=HOCHBAUM|SQLEXPRESS; Initial Catalog=Snafoo;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />

这是网络服务:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;


    namespace SnafooWebService
    {
/// <summary>
/// Summary description for SnafooService
/// </summary>
[WebService(Namespace = "Snafoo")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class SnafooService : System.Web.Services.WebService
{


    [WebMethod()]
    public Snacks GetSnackByID(int ID)
    {
        //Retrieve connection String from Web.config
        string cs = ConfigurationManager.ConnectionStrings["Snafoo"].ConnectionString;
        //Create a SQL connection object
        using (SqlConnection con = new SqlConnection(cs))
        {
            //This object executes stored procedure in SQL
            SqlCommand cmd = new SqlCommand("spGetSnackByID", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter("@Id", ID);
            cmd.Parameters.Add(parameter);
            Snacks snack = new Snacks();
            //Opens the cconection
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            //Retrieve column values and populate properties of snack object
            while (reader.Read())
            {
                snack.id = Convert.ToInt32(reader["ID"]);
                snack.name = reader["name"].ToString();
                snack.location = reader["location"].ToString();

            }
            return snack;

        }

    }

}
    }

1 个答案:

答案 0 :(得分:0)

如果您没有使用本地SQL Server
1 - 如果SQL浏览器已启动,请检查您的服务 2 - 在SQl服务器配置管理器上检查是否启用了TCP / IP和名称管道以便能够远程访问数据库
3 - 如果你的sql server不是本地的,你可以使用IP和端口访问它,例如xxx.xxx.xxx.xxx \ SQLEXPRESS,1029,你可以在sql配置管理器中更改端口。
4 - 确保在连接字符串中输入用户名和密码(uid = sa;密码= mmmm)
希望那些笔记能帮到你

相关问题