2016-06-22 43 views
0

我搜索了很多关于这个计算器错误,而是来自我的错误远远受到这些问题......asp.net - 的SQLException是由用户代码未处理的

我与“TUTS +”教程编程CMS而我每次我试图解决这个问题得到这个代码... 错误:

Additional information: 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) I tried for restart sql services and turn off firewall and... but!... and the code :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text.RegularExpressions; 
using System.Web; 
using WebMatrix.Data; 


/// <summary> 
/// Summary description for PostHandler 
/// </summary> 
public class PostHandler : IHttpHandler 
{ 
    public PostHandler() 
    { 
     // 
     // TODO: Add constructor logic here 
     // 
    } 

    public bool IsReusable 
    { 
     get { return false; } 
    } 

    public void ProcessRequest(HttpContext context) 
    { 
     var title = context.Request.Form["postTitle"]; 
     var content = context.Request.Form["postContent"]; 
     var slug = CreateSlug(title); 


     using (var db = Database.Open("DefaultConnection")) 

     { 

      var sql = "SELECT * FROM Posts WHERE Slug = @0"; 

      var result = db.QuerySingle(sql, slug); 




      if (result != null) { 

       throw new HttpException(409, "SLUG IS ALREADY IN USE!"); 

      } 

      sql = "INSERT INTO Posts (Title, Content, AuthorId, Slug" + 
        "VALUES (@0, @1, @2, @3)"; 
      db.Execute(sql, title, content, 1, slug); 
     } 
    } 


    private static string CreateSlug(String title) 
    { 
     title = title.ToLowerInvariant().Replace(" ", "-"); 
     title = Regex.Replace(title, @"[^0-9a-z-]", string.Empty); 

     return title; 
    } 
} 

我上线这个错误:

var result = db.QuerySingle(sql, slug);

+1

此错误表示您的连接字符串错误。它存储在web.config中。有关如何放置连接字符串,请参阅https://www.connectionstrings.com/sql-server/。没有通用的方法来排除故障连接字符串 – MatthewMartin

回答

-1

转到控制面板/搜索服务找到SQL Server(XXX)服务,并检查它是否正在运行。如果它没有运行,那么启动它并尝试再次在SSMS中登录。

阅读全文Here带图片插图

+0

所有这些都在运行好友... – Mohammadreza

相关问题