我已将我的数据库文件(.mdf)附加到我的应用程序。该文件位于文件夹bin \ debug \ database中。但是,当我创建安装文件并在其他计算机上安装我的应用程序时,连接到数据库时出现错误,例如:“试图执行未经授权的操作”,“访问被拒绝”或“Database_log.mdf存在”... 我的app.config:无法连接到SQL数据库
<?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>
我的连接字符串和连接功能:
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 it existed
{
File.Delete(Application.StartupPath + @"\Database\XTOPAZ_log.ldf");
}
string s = ConfigurationManager.ConnectionStrings["XtopazConnectionString"].ConnectionString;
//Set full permisstion access for 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 to connect to database\r\nDetail: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
那么,如何可以创建我的安装文件,并没有任何错误安装其他计算机上的应用程序?
发布解决方案的映像并显示.mdf文件的位置。 –
不知道你为什么试图删除LDF文件。似乎不是一个正确的举措。 – Steve
@Steve,我尝试删除ldf文件,因为当我打开与数据库的连接时,出现“Database_log.mdf exists”错误。我不知道为什么,但当我删除它时,此错误消失。 –