C# 实体框架(Web API)中的System.Data.SqlClient.SqlException
我的web控制器代码:C# 实体框架(Web API)中的System.Data.SqlClient.SqlException,c#,asp.net,entity-framework,asp.net-web-api,dapper,C#,Asp.net,Entity Framework,Asp.net Web Api,Dapper,我的web控制器代码: using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Account;
namespace Account.Controllers
{
public class ORDER_DETAILController : ApiController
{
private restaurantEntities db = new restaurantEntities();
[HttpGet]
[Route("api/ORDER_DETAIL/PendingOrders/{id}")]
[ResponseType(typeof(ORDER_DETAIL))]
public IQueryable<Orders> PendingOrders(int id)
{
Class1 obj = new Class1();
return obj.GetOrders(id);
}
}
}
我得到这个错误:
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接)
System.Data.SqlClient.SqlException
我知道我在创建EF时已经打开了连接,但是如果没有名为conn
的SqlConnection
变量,我如何在GetOrders()
中执行查询
注意:连接建立在普通控制台应用程序和其他预定义方法(如在帐户控制器中获取、搜索)中工作,但在我的预定义方法中不工作。请检查您的连接字符串,尤其是在空格上 连接字符串的格式示例:
`connetionString = @"Data Source=WIN-50GP30FGO75; integrated security = true; Initial Catalog=Demodb;User ID=sa;Password=demol23";`
我怀疑你的机器名是:“RAHUL-PAVILION”@埃里基同意了。该连接字符串肯定会导致该错误。您有一个SQL注入问题:您正在将
id
连接到查询中,您应该使用SQL参数,就像这样@id
您不能混合集成安全性和用户名/password@ErikEJ只是显示格式,即使在指定了用户ID和密码且集成安全性设置为true时可以混合使用,用户ID和密码也将被忽略,并将使用集成安全性。
https://localhost:44331/api/ORDER_DETAIL/PendingOrders/1
`connetionString = @"Data Source=WIN-50GP30FGO75; integrated security = true; Initial Catalog=Demodb;User ID=sa;Password=demol23";`