简体   繁体   English

无法使用创建的Web服务连接到SQL Server

[英]Can't connect to SQL Server with a created web service

I am trying to teach myself some basics on web services and set up a simple app that just accesses SQL server and pulls an item from the database. 我试图自学一些关于Web服务的基础知识,并建立一个简单的应用程序,该应用程序仅访问SQL Server并从数据库中提取一个项目。

But I get this error when trying to connect: 但是尝试连接时出现此错误:

" Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: The network path was not found" “命名管道提供程序,错误:40-无法打开与SQL Server的连接)---> System.ComponentModel.Win32Exception:找不到网络路径”

I use this connection string for other solutions and it works fine, but not for the web service, and when searching Stack I can't find solutions that fit this issue. 我将此连接字符串用于其他解决方案,并且可以正常工作,但不适用于Web服务,并且在搜索Stack时找不到适合此问题的解决方案。

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

And here is the web service: 这是Web服务:

    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;

        }

    }

}
    }

in Case you are not using Local SQL Server 如果您不使用本地SQL Server
1-Check your Services if the SQL Browser is Started 1-检查您的服务是否已启动SQL浏览器
2-On your SQl Server configuration manager Check if you enabled TCP/IP and Name-pipes to be able to access your database remotely 2在SQl Server配置管理器上,检查是否启用了TCP / IP和名称管道以能够远程访问数据库
3-if your sql server is not local you can access it by using the IP and Port such as xxx.xxx.xxx.xxx\\SQLEXPRESS,1029 , where you can change the Port from your sql configuration manager. 3-如果您的sql服务器不在本地,则可以使用IP和端口(例如xxx.xxx.xxx.xxx \\ SQLEXPRESS,1029)来访问它,您可以从sql配置管理器中更改端口。
4-make sure in your connection string you put the username and password ( uid=sa;password=mmmm) 4-确保在连接字符串中输入用户名和密码(uid = sa; password = mmmm)
hope those notes will help you 希望这些笔记对您有帮助

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

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