我已將我的數據庫文件(.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」錯誤。我不知道爲什麼,但當我刪除它時,此錯誤消失。 –