请教C#创建SQL SERVER数据库的问题
网上拷的一份代码
public static string DB_NAME = "mydb";
//public static string DB_PATH = "C:\\data\";
public static string DB_PATH = "C:\";
public static bool CreateDatabase()
{
bool stat = true;
string sqlCreateDBQuery;
SqlConnection myConn =
new SqlConnection("Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=master;");
sqlCreateDBQuery = " CREATE DATABASE "
+ DB_NAME
+ " ON PRIMARY "
+ " (NAME = " + DB_NAME + "_Data, "
+ " FILENAME = '" + DB_PATH + DB_NAME + ".mdf', "
+ " SIZE = 3MB,"
+ " FILEGROWTH = 10%) "
+ " LOG ON (NAME =" + DB_NAME + "_Log, "
+ " FILENAME = '" + DB_PATH + DB_NAME + "Log.ldf', "
+ " SIZE = 1MB, "
+ " FILEGROWTH = 10%) ";
SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, myConn);
//try
//{
myConn.Open();
// 下面这句报了异常
//CREATE FILE encountered operating system error 5(error not found) while attempting
//to open or create the physical file 'C:\mydb.mdf'.
//这是什么原因呢?
myCommand.ExecuteNonQuery();
return stat;
}
//to open or create the physical file 'C:\mydb.mdf'.
这个问题很清楚的啊,5表示Access is denied,没有权限。确保SQL Server服务有权限写到你创建的文件夹下面。
[解决办法]
[解决办法]
//CREATE FILE encountered operating system error 5(error not found) while attempting
//to open or create the physical file 'C:\mydb.mdf'.
这个可能是由于你的sql express的启动账户的权限不足导致的,你试试,修改一下启动账户,然后重启一下sql 的服务: