![](/img/trans.png)
[英]How to add columns to an existing SQL Server 2008 database table in asp.net MVC 3
[英]Add database to existing SQL Server 2008
我花了很多時間嘗試研究外部資源,但不同的解決方案似乎比我希望的更多。
我想要實現以下目標:
這是我第一次嘗試與我的程序進行數據庫集成。 使用SQLite或SMO似乎有點壓倒性的開始。 我一直在嘗試修改此示例代碼以使其工作:
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();
}
}
}
有關如何創建簡單查詢字符串或創建數據庫的任何想法? 如果有更好的方法,那很好,但請記住,我試圖讓它盡可能簡單地開始。
使用SMO:
Server server = new Server("servername");
Database db = new Database(server, "MyDatabaseName");
db.Create();
要構建SMO應用程序,需要引用SMO程序集。 單擊“添加引用”並導航到該文件夾
C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies
添加對這些程序集的引用:
將此using語句添加到包含上述代碼的文件中:
using Microsoft.SqlServer.Management.Smo;
在旁注:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.