c# - Connect to SQL Server Express database via HTTP handler -


i trying update table in database each time file downloaded. database "defaultconnection(websitename)" created when used sample website template. database stores registered users. have added table database.

create table [dbo].[download] (     [filename] nvarchar(50) null  ,      [counter] int not null default 0 ,  ); 

i created http handler gets fired when click download , working without sql connection part:

<%@ 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         {                         {                 return @"path 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 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 increment counter 1                 cmd = new sqlcommand("update counter set counter = counter+1 filename=@filename", conn);                 cmd.commandtype = commandtype.text;                 cmd.parameters.addwithvalue("@filename", "default");                  using (conn)                 {                     //open connection                     conn.open();                     //send query                     cmd.executenonquery();                 }                 conn.close();             }             else             {                 context.response.contenttype = "text/plain";                 context.response.write(filespath + filename + " invalid filename");             }         }          public bool isreusable {             {                 return false;             }         }     } 

i cannot connect connection strings can find. have tried 1 shown in "web.config". try connect while, throws exception on conn.open(); line saying couldn't connect:

a network-related or instance-specific error occurred while establishing connection sql server. server not found or not accessible. verify instance name correct , sql server configured allow remote connections. (provider: named pipes provider, error: 40 - not open connection sql server) .

my main question is, how connect default database can update information in table when file downloaded.

there no attached db file name in connection string. please connection string local db here http://msdn.microsoft.com/en-us/library/hh510202.aspx


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -