开发者

C# system.io.filenotfoundexception

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(开发者_StackOverflow); // 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜