C#system.io.filenotfoundexception
我正在为我的工作创建一个应用程序,以跟踪学校一年中的行为管理。要做到这一点,我显然需要一个数据库。我构建了程序,以便在打开时检查数据库是否存在,如果不存在,则创建一个,然后输入模式。这在我的开发计算机上完美工作,但是当我将它切换到使用Windows XP的计算机时,它给出了一个错误,指出system.io.filenotfoundexception。我无法弄清楚为什么它不会创建数据库。 我正在使用Visual C#2010 Express。而数据库是在sql服务器ce。C#system.io.filenotfoundexception
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;
}
我想不通,为什么应用程序不使数据库,它似乎在某些计算机上工作,而不是别人的工作。任何帮助将是伟大的!谢谢。
确保你已经部署了所有的dll Sql Server Compacts需要。当你安装.NET框架时,我不确定你会得到它们。所需的DLL是:
- sqlceca35.dll
- sqlcecompact35.dll
- sqlceer35EN.dll
- sqlceme35.dll
- sqlceoledb35.dll
- sqlceqp35.dll
- sqlcese35.dll
You ca n在这个MSDN页面上找到关于Sql Compact Deployment各种版本的数据库引擎的更多详细信息。
如果(!File.Exists( “chart.sdf”))
你似乎想在同一目录下的程序来创建数据库。是的,这将在你的开发机器上工作,但这不适用于普通机器。典型的安装目录(c:\ program files \ etc)不允许对文件进行写入访问。
您将需要使用Environment.GetFolderPath()来获取可写入的ApplicationData目录。
这通常会让安装程序创建appdata目录并将最初的.sdf文件复制到那里更容易,更容易出错。虽然Express版本不支持安装项目。黑客代码工作在Express版本的限制之下就是破坏了产品许可证的价格。你在这里很亲密。
是否安装了最低系统要求?例如,.NET框架? – jsmith 2010-09-20 20:13:54
您的代码中没有任何内容会导致'FileNotFoundException'。正如'jsmith'所提到的,它可能是一个缺失的外部文件(即.NET框架)。 – 2010-09-20 20:16:16
我安装了.NET框架4,我是否需要更新版本?它最初告诉我我需要.NET框架4.3,但是一旦我安装了4,没有说构建,它会运行该程序,但它给出了错误。 – 2010-09-20 20:19:41