尝试在Windows应用程序中附加自动命名的数据库

时间:2012-11-23 20:22:37

标签: c# database ado.net enterprise-architect

尝试在C#中创建Enterprise Architect加载项时,在我完成代码并在VS 2010中运行它之后我可以将数据添加到数据库或删除但是当我使用Add-时在项目中,该项目是同一个项目但具有允许我访问Enterprise Architect事件的类库,我收到以下错误:

  

应用程序中的组件中发生了无法处理的异常。   如果单击继续,应用程序将忽略此错误并且   试图继续。

     

尝试为C:\ Program Files \ Sparx附加自动命名的数据库   Systems \ EA \ DataBase \ DBMetric.mdf失败。具有相同名称的数据库   存在,或指定的文件无法打开或位于UNC上   份额。

当我转到C\...EA时,没有数据库文件夹!

这是我的app.config文件

<configuration>
    <connectionStrings>
        <add name="WindowsFormsApplication19.Properties.Settings.DBMetricConnectionString" 
             connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DataBase\DBMetric.mdf;Integrated Security=True;User Instance=True" 
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

这是我的连接字符串代码:

public static string myConnectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication19.Properties.Settings.DBMetricConnectionString"].ConnectionString;

有什么想法吗?

提前致谢

这是班级图书馆

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EA;
using System.Windows.Forms;
using WindowsFormsApplication19;

namespace ClassLib
{
    public class Class
    {
        // define menu constants
        const string menuHeader = "-&Metrics";
        const string menuOpen = "&Open";

        // remember if we have to say hello or goodbye
        private bool OPEN_TOOL = true;

        ///
        /// Called Before EA starts to check Add-In Exists
        /// Nothing is done here.
        /// This operation needs to exists for the addin to work
        ///
        /// <param name="Repository" />the EA repository
        /// a string
        public String EA_Connect(EA.Repository Repository)
        {
            //No special processing required.
            return "a string";
        }

        ///
        /// Called when user Clicks Add-Ins Menu item from within EA.
        /// Populates the Menu with our desired selections.
        /// Location can be "TreeView" "MainMenu" or "Diagram".
        ///
        /// <param name="Repository" />the repository
        /// <param name="Location" />the location of the menu
        /// <param name="MenuName" />the name of the menu
        ///
        public object EA_GetMenuItems(EA.Repository Repository, string Location, string MenuName)
        {
            switch (MenuName)
            {
                // defines the top level menu option
                case "":
                    return menuHeader;
                // defines the submenu options
                case menuHeader:

                    string[] subMenus = { menuOpen };//, menuGoodbye// };
                    return subMenus;
            }

            return "";
        }

        ///
        /// returns true if a project is currently opened
        ///
        /// <param name="Repository" />the repository
        /// true if a project is opened in EA
        bool IsProjectOpen(EA.Repository Repository)
        {
            try
            {
                EA.Collection c = Repository.Models;
                return true;
            }
            catch
            {
                return false;
            }
        }

        ///
        /// Called once Menu has been opened to see what menu items should active.
        ///
        /// <param name="Repository" />the repository
        /// <param name="Location" />the location of the menu
        /// <param name="MenuName" />the name of the menu
        /// <param name="ItemName" />the name of the menu item
        /// <param name="IsEnabled" />boolean indicating whethe the menu item is enabled
        /// <param name="IsChecked" />boolean indicating whether the menu is checked
        public void EA_GetMenuState(EA.Repository Repository, string Location, string MenuName, string ItemName, ref bool IsEnabled, ref bool IsChecked)
        {
            if (IsProjectOpen(Repository))
            {
                switch (ItemName)
                {
                    // define the state of the hello menu option
                    case menuOpen:
                        IsEnabled = OPEN_TOOL;
                        break;
                    // define the state of the goodbye menu option
                    //case menuGoodbye:
                    //    IsEnabled = !OPEN_TOOL;
                    //    break;
                    // there shouldn't be any other, but just in case disable it.
                    default:
                        IsEnabled = false;
                        break;
                }
            }
            else
            {
                // If no open project, disable all menu options
                IsEnabled = false;
            }
        }

        ///
        /// Called when user makes a selection in the menu.
        /// This is your main exit point to the rest of your Add-in
        ///
        /// <param name="Repository" />the repository
        /// <param name="Location" />the location of the menu
        /// <param name="MenuName" />the name of the menu
        /// <param name="ItemName" />the name of the selected menu item
        public void EA_MenuClick(EA.Repository Repository, string Location, string MenuName, string ItemName)
        {
            switch (ItemName)
            {
                // user has clicked the menuOpen menu option
                case menuOpen:
                    this.sayHello();
                    break;
                    // user has clicked the menuGoodbye menu option
                    //case menuGoodbye:
                    //    this.sayGoodbye();
                    //    break;
            }
        }

        ///
        /// Say Hello to the world
        ///
        private void sayHello()
        {
            //MessageBox.Show("MS.C Project");
            Form1.frmMain.ShowDialog();
            this.OPEN_TOOL = true;
        }

        ///
        /// Say Goodbye to the world
        ///
        //private void sayGoodbye()
        //{
        //    MessageBox.Show("MS.C Project Close");
        //    Form1.frm1.Hide();
        //    this.OPEN_TOOL = true;
        //}

        ///
        /// EA calls this operation when it exists. Can be used to do some cleanup work.
        ///
        public void EA_Disconnect()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

    }
}

2 个答案:

答案 0 :(得分:2)

问题是安全性 - 在当前的Windows版本中,您在C:\Program Files\中没有写入权限。您需要设置EA-AddIn以便为数据库使用不同的文件夹 - 例如SpecialFolder.ApplicationData是一个好地方。

答案 1 :(得分:-1)

我一直试图解决这个问题,我设法做到了:

1)键入RUN以搜索Windows开始按钮。 2)在运行中键入%appdata% 3)打开本地文件夹上的Temp Windows文件,找到系统正在查找的路径,例如本地\ Apps \ 2.0 \ Data \ HXPRLJDX.YRE \ YQK631H5.H15 4)将数据库复制并粘贴到那里

运行程序并查看结果。它对我有用。