簡體   English   中英

連接SQL數據庫失敗

[英]Failed to connect to SQL database

我已將數據庫文件(.mdf)附加到應用程序。 該文件位於文件夾bin \\ debug \\ database中。 但是,當我創建安裝文件並在其他計算機上安裝我的應用程序時,在連接數據庫時出現一些錯誤,例如:“試圖執行未經授權的操作”,“訪問被拒絕”或“存在數據庫_log.mdf” ...我的的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>

我的連接字符串和連接功能:

 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);
        }

那么,如何創建我的安裝文件並在其他計算機上安裝應用程序而不會出現任何錯誤呢?

您是否嘗試過以管理員模式運行該應用程序? 將清單文件添加到項目項目->添加新項->應用程序清單文件。 然后修改requestedExecutionLevel以匹配此行:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM