[英]How to create an Access database on first launch of my .NET Windows application?
How can I create an Access database when my .NET Windows application runs for the first time? 我的.NET Windows应用程序首次运行时,如何创建Access数据库? I want to create a database with tables used in my project when I install my .exe on a client machine, ask the path for db and create db used in my application.
在客户端计算机上安装.exe时,我想使用项目中使用的表创建数据库,询问db的路径并创建应用程序中使用的db。
From the asker's comment to the question 从提问者的评论到问题
is there any suggestion to create all the tables with db automatically used in my application or I have to crate tables programmatically using c# code
有没有建议使用我的应用程序中自动使用db创建所有表,或者我必须使用c#代码以编程方式创建表
If you want to avoid writing all of the DDL code (CREATE TABLE, CREATE INDEX, ...) to create the table structures "from scratch" you could create the database and all of the tables, etc., in Access. 如果要避免编写所有DDL代码(CREATE TABLE,CREATE INDEX等)来“从头开始”创建表结构,则可以在Access中创建数据库和所有表等。 Then you could add that "blank" database file (table structures but no data) as a Resource in your .NET project.
然后,您可以在.NET项目中将“空白”数据库文件(表结构但无数据)添加为资源 。
When your application launches it can check for the existence of a local (non-"blank") copy of the database. 当您的应用程序启动时,它可以检查数据库的本地(非“空白”)副本的存在。 If one doesn't exist then it can copy the "blank" database from the application's Resources to the desired location on the hard drive.
如果不存在,则可以将“空白”数据库从应用程序的“资源”复制到硬盘上的所需位置。
For example, if I create a "blank" database named "resTestData.accdb" and save it as a File
Resource in my project I can copy it to the local hard drive like so: 例如,如果我创建一个名为“ resTestData.accdb”的“空白”数据库并将其另存为项目中的
File
资源,则可以将其复制到本地硬盘中,如下所示:
string persistentDbLocation = @"C:\Users\Gord\Desktop\resTestUserData.accdb";
if (File.Exists(persistentDbLocation))
{
Console.WriteLine("Local persistent copy of database already exists.");
}
else
{
// "resTest" is the name I gave to my C# Project, hence resTest.Properties
byte[] dbResource = resTest.Properties.Resources.resTestData; // .accdb File Resource
using (FileStream output = new FileStream(persistentDbLocation, FileMode.Create, FileAccess.Write))
{
output.Write(dbResource, 0, dbResource.Length);
}
Console.WriteLine("Local persistent copy of database saved to \"{0}\".", persistentDbLocation);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.