在运行时创建数据库作为首次执行 [英] Create Database in Runtime as first execution

查看:120
本文介绍了在运行时创建数据库作为首次执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

正如我在上一篇文章中所说,我正在学习VB.net,我非常喜欢它.

现在我到达了数据库部分.在我拥有的所有书籍和所有教程中,我发现了有关创建数据库的所有示例的说明,这些内容来自
视觉工作室.问题是,我希望我的应用程序在首次运行时创建具有所有字段和键的新的空数据库文件.

我在设计期间的工作方式.现在,我正在寻找一个具有以下字段的简单数据库:

名称为字符串,国家/地区为字符串,代码为字符串或数组.

所以可以帮我找到解决方案吗?您可以发送验证码吗?

我也看到使用vb.net代码和/或MySql是可能的.看到它们之间的差异将非常有趣.

请帮助我
乔卡克斯

已从答案中移除:
大家好,
有人可以帮我吗?
我需要研究vb.net中的代码以创建一个简单的本地数据库.
很抱歉,如果我不理解以前的例子.
我需要什么?
1-简单的连接字符串(也许我可以使用Visual Studio wizzard)
2-Vb.net代码(不使用MySql)创建本地数据库(甚至文件* .mdf)
3-如何创建表的另一种字段
我在线上找到了许多示例,但并不像我所想的那样清晰(http://www.vb-helper.com/howto_net_runtime_dataset.html)
请帮助我
您甚至可以亲自与我联系
我能认出您的帮助
谢谢您.

Hello everybody,

As I said in a previous post, I am learning VB.net and I like it very much.

Now I have arrived at the database part. In all books I have and all tutorials I found all the examples of making a database are explained creating database from
Visual Studio. The problem is, I would like my application when it runs for the first time to create a new and empty database file with all Fields and Keys.

How I did during the design. Now I am looking for to make a simple database with fields like:

Name as string, Country as String, codes as string or Array.

So can some help me to find the solution? Can you please send the code?

Also I saw that it could be possible using vb.net code and/or MySql. It would be very interesting to see the difference.

Please help Me
Jorkax

Moved from answer:
Hi everybody,
can someone help me?
I need to study the code in vb.net to create a simple local database.
I am sorry if I did not understand previuos examples.
what I need?
1 - a simple connection strings ( maybe I can use the visual studio wizzard )
2 - Vb.net code ( not using MySql ) to create local database ( even the file *.mdf )
3 - how to create a table a differnt kind of fields
I found many examples on line but not as clear as I look for ( http://www.vb-helper.com/howto_net_runtime_dataset.html )
please help me
you may contact me even personally
I can recognize your help
Thank you

推荐答案

大多数数据库都附带了将为您生成所需SQL的客户端,这些客户端将创建表,视图,存储的proc等.基本上需要在目标数据库服务器上执行此自动生成的SQL.

也可以在SQL中包含实际的表数据(如果您想输入一些初始数据).

或者,您可以创建默认数据库的备份,并在安装过程中(在目标计算机上)将其还原.尽管不同的数据库用不同的术语称呼它,但是SQL Server使用备份/还原. MySQL可能使用类似的术语(将其谷歌搜索出来).
Most databases come with clients that will generate the required SQL for you, which will crate the tables, views, stored procs etc. On first run, you basically need to execute this auto generated SQL on the target database server.

It''s also possible to include actual table data in the SQL (if you wish to input some initial data).

Alternatively, you can create a backup of the default DB and restore it during installation (on the target machine). Different databases call this by different terms though, SQL server uses backup/restore. MySQL may use similar terminology (google it out).


您提到了MySql,但没有说这是必需条件,所以我想指出这个技巧/Trick具有创建Access/Jet数据库文件的几种方法:

创建空白的Jet数据库 [
You mentioned MySql, but didn''t say it was a requirement...so I thougth I''d point out this Tip/Trick that has several methods for creating an access/Jet database file:

Create a blank Jet database[^]

But in general what you are asking should be possible in virtually any language and with a variety of different types of databases. Once you get the blank database you just need the proper SQL to build the files...for example, this would build a table like what you are asking in an access db:
CREATE TABLE myTable(
  Name TEXT (80) NOT NULL DEFAULT='''', 
  Country TEXT (80) NOT NULL DEFAULT='''',
  Codes  TEXT (20) NOT NULL DEFAULT=''''
);


您应该研究


You should research the SQL Create Table Statement[^].


大家好!

当然这是可能的,但是我建议您使用SQL脚本跟踪更改.这些脚本包含您在开发环境中所做的所有更改.如果运行这些脚本,将复制数据库,这听起来像是您想要的.

若要创建新数据库并运行脚本,您可能需要使用SQL Server管理对象(SMO)

Hey guys!

Ofcourse this is possible, I would however recommend you track your changes using SQL Scripts. Those scripts contain all changes you''ve made in your development environment. If you run those scripts, your database will be reproduced, which sounds like exactly what you want.

To create a new database, and run the scripts, you may want to use SQL Server Management Objects (SMO)

ServerConnection serverConnection = new ServerConnection();
serverConnection.ConnectionString = ConnectionString;
serverConnection.Connect();

Microsoft.SqlServer.Management.Smo.Server sqlServer = new Microsoft.SqlServer.Management.Smo.Server(serverConnection);
                
Database createdDatabase = new Database();
createdDatabase.Name = DatabaseName;
createdDatabase.Parent = sqlServer;
createdDatabase.Create();
databaseCreated = true;

serverConnection.Disconnect();



祝你好运!



Good luck!


这篇关于在运行时创建数据库作为首次执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆