在运行时创建SQL Server数据库 [英] Creating a sql server database at runtime

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

问题描述

我想在运行时在我的vb .net项目中创建一个sql server数据库。我知道如何实际编码数据库,但我想知道我应该在哪里实际放置代码?我应该将代码放在启动表单中还是应该自己进入一个类?此外,该项目将在特定站点上运行多个pc,因此我只希望在项目第一次激活时创建数据库,然后才能在不同的PC上查询数据库。我该怎么做呢?关于这个问题的所有帮助将不胜感激。

I want to create a sql server database at runtime in my vb .net project. I know how to actually code the database but I am wondering where should I actually put the code? Should I be putting the code in the start up form or should it go into a class on it's own? Also, this project will be going on more than one pc at a particular site, so I only want the database to be created the first time the project is activated and then just be able query the database on different pcs after that. How do I do this? All help on this matter would be greatly appreciated.

推荐答案

首先,检查SQL Server实例以查看数据库是否存在:这很简单,你可以得到一个SQL中所有数据库的列表如下:

First, check the SQL Server instance to see if the DB is there: that's pretty easy, you can get a list of all DB's from SQL like this:
Using con As New SqlConnection("Data Source=GRIFFPC\SQLEXPRESS;Integrated Security=True")
	con.Open()
	Using cmd As New SqlCommand("sp_databases", con)
		cmd.CommandType = CommandType.StoredProcedure
		Dim read As SqlDataReader = cmd.ExecuteReader()
		While read.Read()
			Console.WriteLine(DirectCast(read("DATABASE_NAME"), String))
		End While
	End Using
End Using

如果您的数据库在列表中,则它存在。排序。



如果不是,那么你需要创建它:最简单的方法是将SQL命令放入一个文件中,并将其发送到新的SQL实例作为命令字符串 - SSMS将为您生成文件(如果需要,可以使用最少或样本数据集)。



放在哪里?嗯 - 这是一个很好的问题!创建数据库可能需要一些时间,所以当用户可以看到正在发生的事情时,您希望它发生。我自己,我创建了一个小的初始化表单,它由启动表单作为模态对话框运行,它向用户显示正在发生的事情,并且仅在数据库准备好摇滚时才返回。 (我还在该表单中的后台工作程序中运行代码,以便UI保持响应)

If your DB is in the list, it exists. Sorted.

If it isn't, then you need to create it: and the easiest way is to put the SQL commands into a file, and send that to the new SQL instance as a command string - SSMS will generate the file for you (complete with a minimum or sample set of data if needed).

Where to put it? Well - that's a good question! Creating the DB could take some time, so you want it to happen when the user can see that something is going on. Myself, I'd create a small "Initializing" form which is run by the startup form as a modal dialog and which shows the user what is happening, and only returns when the DB is ready to rock'n'roll. (I'd also run the code in a background worker within that form so that the UI remains responsive)


在构建面向对象的代码时,我发现最好遵循SOLID原则。 />


第一个是单一责任原则。



这表明你的每个编程对象都应该只只需一个目的。



按照这种方法,然后是的,你应该创建单独的类来创建你的数据库,让你只需要一个表格。



在界面设计中,有许多编程模式可以让你将逻辑与界面分开。一种运行良好的模式是MVVM。模型视图 - 模型。



模型 - 视图 - 视图模型(MVVM)解释 [ ^ ]
When building object oriented code I find it best to follow the SOLID principles.

The first of which is the Single Responsibility Principle.

This suggests that each of your programming objects should only have a single purpose.

Following this methodology then yes you should create separate classes for creating your database and let you form simply be a form.

In interface design there are a number of programming patterns which will allow you to separate your logic from your interface. One such pattern which works well is MVVM. Model View View-Model.

Model-View-ViewModel (MVVM) Explained[^]


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

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