c# - Failed to connect to SQL database -
i have attach database file (.mdf) application. file in folder bin\debug\database. when create setup file , install app in other computers, have error when connecting database, such as: "attemped perform unauthorized operation", "access denied" or "database_log.mdf existed"... app.config:
<?xml version="1.0"?> <configuration> <configsections> </configsections> <connectionstrings> <add name="xtopazconnectionstring" connectionstring="data source=.;attachdbfilename=|datadirectory|\database\xtopaz.mdf;initial catalog=xtopaz;integrated security=true" providername="system.data.sqlclient" /> </connectionstrings> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.0"/> </startup> </configuration>
my connection string , connecting function:
public static void openconnect() { try { if (file.exists(application.startuppath + @"\database\xtopaz.mdf")) { if (file.exists(application.startuppath + @"\database\xtopaz_log.ldf")) //delete current ldf file if existed { file.delete(application.startuppath + @"\database\xtopaz_log.ldf"); } string s = configurationmanager.connectionstrings["xtopazconnectionstring"].connectionstring; //set full permisstion access database file directoryinfo dinfo = new directoryinfo(application.startuppath + @"\database\xtopaz.mdf"); directorysecurity dsecurity = dinfo.getaccesscontrol(); dsecurity.addaccessrule(new filesystemaccessrule(new securityidentifier(wellknownsidtype.worldsid, null), filesystemrights.fullcontrol, inheritanceflags.objectinherit | inheritanceflags.containerinherit, propagationflags.nopropagateinherit, accesscontroltype.allow)); dinfo.setaccesscontrol(dsecurity); dinfo.setaccesscontrol(dsecurity); con.connectionstring = s; con.open(); } else { exception ex; ex = new exception("database file not found"); throw ex; } } catch (exception ex) { messagebox.show("error occured when trying connect database\r\ndetail: " + ex.message, "error", messageboxbuttons.ok, messageboxicon.error); }
so how can create setup file , install application on others computer without error?
have tried running application in administrator mode? add manifest file project project -> add new item -> application manifest file. modify requestedexecutionlevel match line:
<requestedexecutionlevel level="requireadministrator" uiaccess="false" />
Comments
Post a Comment