简体   繁体   English

如何在首次启动.NET Windows应用程序时创建Access数据库?

[英]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.

相关问题 我可以使用SQL Server安全性来控制对(数据库优先)ASP.net应用程序的访问吗? 怎么样? - Can I use SQL Server security to control access to my (database first) ASP.net application? How? 如何在.net windows窗体应用程序中将访问控制与我的ORM集成? - How to integrate access control with my ORM in a .net windows form application? 如何创建数据库并为Windows Mobile 5应用程序访问数据库? - How do i create a database and access it for my windows mobile 5 app? 如何在没有可见窗口的情况下启动.net Windows窗体应用程序? - How do I launch .net windows forms application with no visible windows? 如何创建第一个Windows Forms DevExpress应用程序 - How to create first Windows Forms DevExpress application 在远程计算机上使用.NET Windows应用程序安装Access数据库 - Install an Access database with a .NET windows application on a remote computer 如何使用数据库创建Windows应用程序安装程序? - How to create windows application setup with database? 如何在.Net上创建Access数据库备份? - How to create an Access database backup on .Net? 在Windows 8中启动任何应用程序时,如何启动我的C#应用​​程序? - How to launch my C# application when any application starts in Windows 8? 如何从 .NET 应用程序访问 android 应用程序的本地 sqlite 数据库 - How to access local sqlite database of android application from .NET application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM