如果是SQLSERVER,在WEB程序中可以连接数据源,直接用CREATE DATABASE创建。而Access可以用ADOX创建。
自己有个创建数据库的Helper.cs(部分代码):
/// <summary>
/// 创建SqlServer数据库
/// </summary>
/// <param name="dbName">数据库名</param>
/// <param name="dbPath">数据库存放路径:"d:\\cs\\"</param>
/// <returns></returns>
public static bool CreateSqlServerDb(string dbName, string dbPath)
{
string dbDir = dbPath;
string createStr = "Data Source=" + StrHelper.GetItemValueFromConnStr(connStr, "Source") + ";Initial Catalog='master';Persist Security Info=True;User ID=" + StrHelper.GetItemValueFromConnStr(connStr, "ID") + ";Password=" + StrHelper.GetItemValueFromConnStr(connStr, "Password");
String createDb;
SqlConnection createConn = new SqlConnection(createStr);
createDb = "CREATE DATABASE " + dbName + " ON PRIMARY " +
"(NAME = " + dbName + ", " +
"FILENAME = '" + dbDir + dbName + ".mdf', " +
"SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = " + dbName + "_Log, " +
"FILENAME = '" + dbDir + dbName + "_Log.ldf', " +
"SIZE = 1MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";
SqlCommand createCmd = new SqlCommand(createDb, createConn);
try
{
createConn.Open();
createCmd.ExecuteNonQuery();
}
catch (System.Exception)
{
return false;
}
finally
{
if (createConn.State == ConnectionState.Open)
{
createConn.Close();
}
}
return true;
}
#endregion
Access:
。。。
ADOX.CatalogClass buildDb = new ADOX.CatalogClass();
buildDb.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + dbName + ";" + "Jet OLEDB:Engine Type=5");
ADODB.Connection cn = new ADODB.Connection();//建立链接
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + dbName, null, null, -1);
buildDb.ActiveConnection = cn;
ADOX.Table tableTemp = null;//创建临时表
。。。
创建SQL server 没问题。
我这儿有个源码在线管理SQL Server,可以发给你
3w@live.cn
用SQL语句创建:
Create database