2013-04-06 88 views
2

我试图在每次下载文件时更新数据库中的表。数据库是使用示例网站模板时创建的“DefaultConnection(WebsiteName)”。它是存储所有注册用户的数据库。我已将此表添加到该数据库。通过HTTP处理程序连接到SQL Server Express数据库

CREATE TABLE [dbo].[Download] (
    [filename] NVARCHAR(50) NULL , 
    [counter] INT NOT NULL DEFAULT 0 , 
); 

我创造当我点击下载的是被解雇,工作没有SQL连接部分HTTP处理程序:

<%@ WebHandler Language="C#" Class="Download" %> 

using System; 
using System.Web; 
using System.IO; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Configuration; 

public class Download : IHttpHandler { 

    SqlConnection conn; 
    SqlCommand cmd; 

     private string FilesPath 
     { 
      get 
      { 
       return @"path to directory holding files"; 
      } 
     } 

     public void ProcessRequest(HttpContext context) 
     { 
      string fileName = context.Request.QueryString["filename"]; 
      if (!string.IsNullOrEmpty(fileName) && File.Exists(FilesPath + fileName)) 
      { 
       context.Response.ContentType = "application/octet-stream"; 
       context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName)); 
       context.Response.WriteFile(FilesPath + fileName); 

       //connect to the db 

       conn = new SqlConnection(
       "Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-websiteName-20130405020152;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-websiteName-20130405020152‌​.mdf"); 
       //the sql command to increment counter by 1 
       cmd = new SqlCommand("UPDATE counter SET counter = counter+1 WHERE [email protected]", conn); 
       cmd.CommandType = CommandType.Text; 
       cmd.Parameters.AddWithValue("@filename", "Default"); 

       using (conn) 
       { 
        //open the connection 
        conn.Open(); 
        //send the query 
        cmd.ExecuteNonQuery(); 
       } 
       conn.Close(); 
      } 
      else 
      { 
       context.Response.ContentType = "text/plain"; 
       context.Response.Write(FilesPath + fileName + " Invalid filename"); 
      } 
     } 

     public bool IsReusable { 
      get { 
       return false; 
      } 
     } 
    } 

我不能让它与任何连接字符串我能找到连接。我试过了“web.config”中显示的那个。它总是会尝试连接了一段时间,但随后抛出的conn.Open();线异常说它无法连接:

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) .

我的主要问题是,如何连接到这个默认的数据库,这样我可以下载文件时更新此表中的信息。

+1

是什么错误消息说? – usr 2013-04-06 09:06:23

+0

@usr使用连接字符串,我可以通过右键单击 - >属性:建立与SQL Server的连接时发生网络相关或实例特定的错误。服务器未找到或无法访问。验证实例名称是否正确,并将SQL Server配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接) – 2013-04-06 09:17:35

回答

0

我设法连接到它最后用这样的:

conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); 
0

连接字符串中没有附加的db文件名。请从这里获取本地数据库连接字符串http://msdn.microsoft.com/en-us/library/hh510202.aspx

+0

对不起,OP是最后一次尝试之一。就像我说过的,我试图使用“web.config”中给出的连接字符串,我仍然得到相同的错误。我也无法完全理解这一联系,对不起。 – 2013-04-06 09:27:24

+0

链接对我来说工作正常。我有兴趣在web.config中看到你的连接字符串。 – qamar 2013-04-06 09:30:34

+0

我的意思是我不能非常清楚地提供该链接中提供的信息。 “web.config”中的连接字符串为:“Data Source =(LocalDb)\ v11.0; Initial Catalog = aspnet-websiteName-20130405020152; Integrated Security = SSPI; AttachDBFilename = | DataDirectory | \ aspnet-websiteName-20130405020152.mdf”通过右键单击 - > Properties可以看到的是:“Data Source =(LocalDb)\ v11.0; AttachDbFilename =”C:\ Users \ userName \ Documents \ Visual Studio 2012 \ WebSites \ websiteName \ App_Data \ aspnet -websiteName-20130405020152.mdf“; Initial Catalog = aspnet-websiteName-20130405020152; Integrated Security = True” – 2013-04-06 09:34:25

相关问题