如何在新安装的软件中添加数据库. [英] How to add database in newly installed software.

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

问题描述



在我的软件中,我使用SQL Server作为数据库.首次安装(或运行)软件时,我需要创建一个包含两个表的新数据库.现在我该怎么办,应该通过检查数据库是否已经存在来编写类似于查询的创建数据库,或者有更好的方法来执行此操作.请建议我如何在新安装的计算机中添加数据库.

关于

Hi,

In my software I am using SQL server as database. When my software first time installs (or runs), I need to create new database with two tables in it. Now what should I do, should I write Create Database like query by checking if database already exists or not or, there is any better way of doing this. Kindly suggest how I add database in newly installed machine.

Regards

推荐答案

假设您正在谈论创建数据库本身,而不是在安装SQL Server,那么您可以在SQL Server实例上运行查询,该查询将检查:是最安全的方法.

Assuming you are talking about creating the database itself, rather than installing SQL Server, then you can run a query on your SQL Server instance which will check: that would be the safest way.

if not exists(select * from sys.databases where name = 'Testing')
    create database testing


您可以将数据库文件部署到目标系统并执行附加操作的解决方案:

首先分离旧的:
In addition to other solutions you can deploy your DB files to destination system and commit an attach action :

First detach old one :
use master
   go
   sp_detach_db 'olddb'
   go



然后附加一个新的:



Then attach new one :

use master
  go
  sp_attach_db 'mydb','E:\Sqldata\mydbdata.mdf','E:\Sqldata\mydblog.ldf'
  go



有关更多信息,请访问:
http://support.microsoft.com/kb/224071 [



read more at:
http://support.microsoft.com/kb/224071[^]

and another way is complete restore if the previous data is not important for you :

RESTORE DATABASE AdventureWorks
   FROM DISK = 'Z:\SQLServerBackups\AdventureWorks.bak'



了解更多:
http://msdn.microsoft.com/en-us/library/ms186858%28v = sql.90%29.aspx [ ^ ]

这两种方法都适用于不需要现有数据的情况.但是,如果要保留现有数据,则需要深入观察数据库并应用所需的更改,这可能非常复杂...



read more :
http://msdn.microsoft.com/en-us/library/ms186858%28v=sql.90%29.aspx[^]

Both of these methods are suitable for the condition that you don''t need existing data. But if you want preserve existing data it needs a deep observation of the database and applying needed changes which would be probably very complicated...


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

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