简体   繁体   English

将数据库添加到现有SQL Server 2008

[英]Add database to existing SQL Server 2008

I've spent a lot of time trying to research outside resources, but different solutions seem to be more involved than I was hoping. 我花了很多时间尝试研究外部资源,但不同的解决方案似乎比我希望的更多。

I am trying to achieve the following: 我想要实现以下目标:

  • create simple database on existing SQL instance on this local computer. 在此本地计算机上的现有SQL实例上创建简单数据库。
  • security should just be a simple user name and password 安全性应该只是一个简单的用户名和密码
  • other options are unnecessary (log, growth, max size, etc) 其他选项是不必要的(日志,增长,最大尺寸等)

This is my first attempt at database integration with my program. 这是我第一次尝试与我的程序进行数据库集成。 using SQLite or SMO seemed slightly overwhelming to start. 使用SQLite或SMO似乎有点压倒性的开始。 I've been trying to modify this sample code to work: 我一直在尝试修改此示例代码以使其工作:

private void createDatabase()
{
    String str;
    SqlConnection myConn = new SqlConnection("Server=" + serverName + ";Integrated security=SSPI;database=master");

    str = "CREATE DATABASE " + dbName + " ON PRIMARY " +
           "(NAME = " + dbName + "_Data, " +
           "FILENAME = 'C:\\" + dbName + ".mdf', " +
           "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
           "LOG ON (NAME = " + dbName + "_Log, " +
           "FILENAME = 'C:\\" + dbName + ".ldf', " +
           "SIZE = 1MB, " +
           "MAXSIZE = 5MB, " +
           "FILEGROWTH = 10%)";

    SqlCommand myCommand = new SqlCommand(str, myConn);
    try
    {
        myConn.Open();
        myCommand.ExecuteNonQuery();
        MessageBox.Show("DataBase is Created Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
        if (myConn.State == ConnectionState.Open)
        {
            myConn.Close();
        }
    }
}

Any ideas on how to create a simple query string or something to create a database? 有关如何创建简单查询字符串或创建数据库的任何想法? If there's a better way, that's fine but keep in mind I'm trying to keep it as simple as possible just to start with. 如果有更好的方法,那很好,但请记住,我试图让它尽可能简单地开始。

Using SMO: 使用SMO:

    Server server = new Server("servername");
    Database db = new Database(server, "MyDatabaseName");
    db.Create();

To build an SMO application, you need to reference the SMO assemblies. 要构建SMO应用程序,需要引用SMO程序集。 Click 'Add Reference' and navigate to the folder 单击“添加引用”并导航到该文件夹

C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies

Add references to these assemblies: 添加对这些程序集的引用:

  • Microsoft.SqlServer.ConnectionInfo.dll Microsoft.SqlServer.ConnectionInfo.dll
  • Microsoft.SqlServer.Smo.dll Microsoft.SqlServer.Smo.dll
  • Microsoft.SqlServer.Management.Sdk.Sfc.dll Microsoft.SqlServer.Management.Sdk.Sfc.dll
  • Microsoft.SqlServer.SqlEnum.dll Microsoft.SqlServer.SqlEnum.dll

Add this using statement to the file containing the above code: 将此using语句添加到包含上述代码的文件中:

using Microsoft.SqlServer.Management.Smo;

On a Side Note: 在旁注:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net MVC 3中将列添加到现有SQL Server 2008数据库表中 - How to add columns to an existing SQL Server 2008 database table in asp.net MVC 3 具有现有SQL Server 2008 R2数据库的C#应用 - C# App with Existing SQL Server 2008 R2 Database 如何将DataGrid中的所有列添加到SQL Server 2008数据库 - How to add all colums in a datagrid to SQL Server 2008 database 有没有一种方法可以将现有的SQL Server数据库文件添加到项目中以进行集成测试? - Is there a way to add an existing SQL Server database file to a project for integration tests? 将JSON字符串插入SQL Server 2008数据库 - Insert json string into SQL Server 2008 database 如何将SQL Server 2008 R2数据库添加到Visual Studio 2010安装文件中? - How to add my SQL Server 2008 R2 database to my Visual Studio 2010 Setup File? 在SQL Server 2008数据库中添加/删除/更新任何选定表的数据 - Add/Delete/Update any chosen table’s data in SQL server 2008 database SQL Server Management Studio 2008 插件 - SQL Server Management Studio 2008 add in 在云端将SQL Azure数据库从SQL Server 2008迁移到2014 - Migrating a SQL Azure database from SQL Server 2008 to 2014 in the cloud 如何设置现有SQL Server数据库的所有者 - How to set owner of existing SQL Server database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM