C# system.io.filenotfoundException

发布于 2024-09-24 20:14:02 字数 2581 浏览 2 评论 0 原文

我正在为我的工作创建一个应用程序,以跟踪整个学年的行为管理。为此,我显然需要一个数据库。我构建了该程序,以便在打开时检查数据库是否存在,如果不存在,则创建一个数据库并输入架构。这在我的开发计算机上运行得很好,但是当我将其切换到使用 Windows XP 的计算机时,它会出现错误,提示 system.io.filenotfoundexception。我不明白为什么它不会创建数据库。 我正在使用 Visual C# 2010 Express。数据库是用sql server ce制作的。

        if (!File.Exists("chart.sdf"))//Checks if file is already in existance when app is opened
        {
            dbCreated = createDb();//If there is no database, creates one
        }


public bool createDb()//Creates the database if needed
    {
        bool success = true;
        //Holds sql schema for the table
        string[] tableCreateArr ={"create table childNameId"
                 + "(childId INT IDENTITY PRIMARY KEY, "
                 + "childFName nchar(40), "
                 + "childLName nchar(40));",
                "create table leaderNameId"
                + "(leaderId INT IDENTITY PRIMARY KEY, "
                + "leaderFName nchar(40), "
                + "leaderLName nchar(40));", 
                "create table TagPulledId"
                 + "(tagId INT IDENTITY PRIMARY KEY, "
                 + "childId INT, "
                 + "leaderId INT, "
                 + "day TINYINT, "
                 + "month TINYINT, "
                 + "year INT);",
                 "CREATE TABLE tagInfo"
                 + "(tagId INT, "
                 + "color nchar(10), "
                 + "description ntext)"};

        SqlCeEngine dbCreate = new SqlCeEngine(connectString());  // creates the database obj

        dbCreate.CreateDatabase(); // creates the database file
        SqlCeConnection tempConnect = new SqlCeConnection(connectString());//Connects to the new database
        SqlCeCommand objCmd = new SqlCeCommand();//Creates an sql command obj
        tempConnect.Open(); // opens the connection
        objCmd.Connection = tempConnect; //Connects the command obj

        foreach (string strCmd in tableCreateArr)//Iterates throught he sql schema to create the tables
        {
            try
            {
                objCmd.CommandText = strCmd; //sets the CommandText to the next schema entry in the array
                objCmd.ExecuteNonQuery(); //Executes the create query
            }
            catch (SqlCeException sqlError)
            {
                MessageBox.Show(sqlError.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                success = false;
            }
        }
        tempConnect.Close();
        return success;
    }

我不明白为什么该应用程序没有创建数据库,它似乎可以在某些计算机上运行,​​而不能在其他计算机上运行。任何帮助都会很棒!谢谢。

I am creating an app for my work to track behavior management over the course of a school year. To do this, I obviously needed a database. I built the program so that it would, when opened, check to see if the database exists, and if it doesn't, it creates one, and inputs the schema. This works perfectly on my dev computer, but when I switch it to a computer using windows XP it gives an error saying system.io.filenotfoundexception. I can't figure out why it won't create the database.
I am using Visual C# 2010 Express. And the database is made in sql server ce.

        if (!File.Exists("chart.sdf"))//Checks if file is already in existance when app is opened
        {
            dbCreated = createDb();//If there is no database, creates one
        }


public bool createDb()//Creates the database if needed
    {
        bool success = true;
        //Holds sql schema for the table
        string[] tableCreateArr ={"create table childNameId"
                 + "(childId INT IDENTITY PRIMARY KEY, "
                 + "childFName nchar(40), "
                 + "childLName nchar(40));",
                "create table leaderNameId"
                + "(leaderId INT IDENTITY PRIMARY KEY, "
                + "leaderFName nchar(40), "
                + "leaderLName nchar(40));", 
                "create table TagPulledId"
                 + "(tagId INT IDENTITY PRIMARY KEY, "
                 + "childId INT, "
                 + "leaderId INT, "
                 + "day TINYINT, "
                 + "month TINYINT, "
                 + "year INT);",
                 "CREATE TABLE tagInfo"
                 + "(tagId INT, "
                 + "color nchar(10), "
                 + "description ntext)"};

        SqlCeEngine dbCreate = new SqlCeEngine(connectString());  // creates the database obj

        dbCreate.CreateDatabase(); // creates the database file
        SqlCeConnection tempConnect = new SqlCeConnection(connectString());//Connects to the new database
        SqlCeCommand objCmd = new SqlCeCommand();//Creates an sql command obj
        tempConnect.Open(); // opens the connection
        objCmd.Connection = tempConnect; //Connects the command obj

        foreach (string strCmd in tableCreateArr)//Iterates throught he sql schema to create the tables
        {
            try
            {
                objCmd.CommandText = strCmd; //sets the CommandText to the next schema entry in the array
                objCmd.ExecuteNonQuery(); //Executes the create query
            }
            catch (SqlCeException sqlError)
            {
                MessageBox.Show(sqlError.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                success = false;
            }
        }
        tempConnect.Close();
        return success;
    }

I can't figure out why the app isn't making the database, it seems to work on some computers, while not working on others. Any help would be great! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2024-10-01 20:14:02

if (!File.Exists("chart.sdf"))

您似乎想在与程序相同的目录中创建数据库。是的,这可以在您的开发机器上运行,但不能在普通机器上运行。典型的安装目录 (c:\program files\etc) 不允许对文件进行写访问。

您将需要使用Environment.GetFolderPath() 来获取可以写入的ApplicationData 目录。

让安装程序创建 appdata 目录并将初始 .sdf 文件复制到其中通常会更容易且不易出错。尽管 Express 版本不支持安装项目。确实到了某个时候,通过破解代码来解决 Express 版本的限制就会降低产品许可证的价格。你离这里很近了。

if (!File.Exists("chart.sdf"))

You seem to want to create the database in the same directory as your program. Yes, that will work on your dev machine but that's not going to work on a regular machine. The typical install directory (c:\program files\etc) does not permit write access to files.

You will need to use Environment.GetFolderPath() to get an ApplicationData directory that you can write to.

It is often a lot easier and less error prone to let an installer create the appdata directory and copy the initial .sdf file in there. Albeit that Setup projects are not supported by the Express edition. There does come a point where hacking code to work around the Express edition's restrictions is defeating the price of the product license. You're pretty close here.

怀中猫帐中妖 2024-10-01 20:14:02

确保您已部署 Sql Server Compacts 所需的所有 dll。我不确定您在安装 .NET Framework 时是否获得它们。所需的 dll 是:

  • sqlceca35.dll
  • sqlcecompact35.dll
  • sqlceer35EN.dll
  • sqlceme35.dll
  • sqlceoledb35.dll
  • sqlceqp35.dll
  • sqlcese35.dll

您可以在此 MSDN 页面上找到有关 Sql 紧凑部署,适用于各种版本的数据库引擎。

Make sure you have deployed all the dll Sql Server Compacts need. I'm not sure you get them when you install .NET framework. The dll needed are :

  • sqlceca35.dll
  • sqlcecompact35.dll
  • sqlceer35EN.dll
  • sqlceme35.dll
  • sqlceoledb35.dll
  • sqlceqp35.dll
  • sqlcese35.dll

You can find more details on this MSDN page about Sql Compact Deployment for various versions of the database engine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文