C# system.io.filenotfoundException
我正在为我的工作创建一个应用程序,以跟踪整个学年的行为管理。为此,我显然需要一个数据库。我构建了该程序,以便在打开时检查数据库是否存在,如果不存在,则创建一个数据库并输入架构。这在我的开发计算机上运行得很好,但是当我将其切换到使用 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;
}
我不明白为什么该应用程序没有创建数据库,它似乎可以在某些计算机上运行,而不能在其他计算机上运行。任何帮助都会很棒!谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎想在与程序相同的目录中创建数据库。是的,这可以在您的开发机器上运行,但不能在普通机器上运行。典型的安装目录 (c:\program files\etc) 不允许对文件进行写访问。
您将需要使用Environment.GetFolderPath() 来获取可以写入的ApplicationData 目录。
让安装程序创建 appdata 目录并将初始 .sdf 文件复制到其中通常会更容易且不易出错。尽管 Express 版本不支持安装项目。确实到了某个时候,通过破解代码来解决 Express 版本的限制就会降低产品许可证的价格。你离这里很近了。
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.
确保您已部署 Sql Server Compacts 所需的所有 dll。我不确定您在安装 .NET Framework 时是否获得它们。所需的 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 :
You can find more details on this MSDN page about Sql Compact Deployment for various versions of the database engine.