如何在VB.net中按代码添加数据库表 [英] How to add a database table by code in VB.net

查看:125
本文介绍了如何在VB.net中按代码添加数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在VB.net上开发了一个项目,将SQL Server compact Edition 4.0作为数据库并在许多机器上部署,但现在我我将为数据库中有更多表的应用程序提供更新。



这里的问题是我的客户在数据库中有他们的数据,我不希望他们放松它。但是如果我在后端保留相同的数据库,那么我的新更新将无法在该数据库上运行,我需要将表添加到他们现有的数据库中,以允许我的应用程序在他们的系统上工作。



我愿意在安装项目中向数据库添加新表,这意味着当我的客户端运行设置时,表将被添加到现有数据库中。



我认为上面的问题很明确且可以理解,所以请帮助我。



谢谢你提前。

Hi All,

I have developed a project on VB.net with SQL server compact edition 4.0 as a database and deployed it in many machines, but now I am going to provide an update to the application which has more tables in database.

The issue here is that my clients have their data in database and i dont want them to loose it. But if i kept the same database in backend so my new update will not work on that database i need to add table to their existing database to allow my application to work on their system.

I am willing to add new tables to database in setup project, means when my client runs the setup the tables are added to their existing database.

I think so the question above is well clear and understandable, so please help me.

Thank you in advance.

推荐答案

如果我理解正确,一种可能性是你的应用程序在启动时添加了缺少的表。添加表基本上与更新行相同,只需执行SqlCommand即可。只有这次你在CommandText中有一个CREATE TABLE语句。



发出命令的人只需要拥有适当的权限才能创建表(dbo)例如)。
If I understood correctly, one possibility is that your app adds the missing tables when starting. To add a table is basically the same as for example updating a row, you simply execute a SqlCommand. Only this time you have a CREATE TABLE statement in the CommandText.

The person who is issuing the command just needs to have proper privileges in order to create the table (dbo for example).


由于您使用的是精简版,我的方式是提供一个完整的新数据库,并传输数据,而不是尝试添加任何内容。现有文件。这样,如果更新中有任何问题,客户不会丢失任何东西。



理论上,以编程方式将表添加到数据库不应该导致无论如何都有问题:这只是发出CREATE TABLE命令的情况:

Since you are using Compact edition, the way I'd go is to provide a complete new database, and transfer their data over, rather than trying to add anything to the existing file. That way, if there is any problem in the update, the customer doesn't lose anything.

In theory, adding a table programmatically to a DB shouldn't cause any problems anyway: it's just a case of issuing the CREATE TABLE command:
string sql = "CREATE TABLE NewTable (LastName NVARCHAR(40) NOT NULL, FirstName NVARCHAR(40) NOT NULL, EmailAddress NVARCHAR(256))";



这不会影响现有数据 - 但数据库可能很精致,依赖它们的人也是如此!我会完全创建一个新数据库,并确保他们保持高兴。


And that won't affect existing data - but Databases can be delicate, as can the people who depend on them! I'd create a new DB completely, and make sure they are kept happy.


这篇关于如何在VB.net中按代码添加数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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