[英]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: 我想要实现以下目标:
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: 添加对这些程序集的引用:
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.