我已经创建了一个c#程序来在localhost ms sql中创建一个数据库,如下所示:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "SERVER = " + serverName + "; DATABASE = master; Integrated Security=SSPI";
string query = " CREATE DATABASE " + dbName + " ON PRIMARY "
+ " (NAME = " + dataFileName + ", "
+ " FILENAME = '" + schemaPath + "', "
+ " SIZE = 2MB,"
+ " FILEGROWTH = 10%) "
+ " LOG ON (NAME =" + logFileName + ", "
+ " FILENAME = '" + logFilePath + "', "
+ " SIZE = 1MB, "
+ " FILEGROWTH = 10%) ";
try
{
conn.Open();
SqlCommand sqlCmd = new SqlCommand(query, conn);
sqlCmd.ExecuteNonQuery();
Console.WriteLine("Database has been created successfully!");
}
catch (System.Exception ex)
{
Console.WriteLine("Failed to create the db. " + ex.ToString());
Environment.Exit(1);
}
它在我的电脑上完全正常,但在另一台机器上失败: 我对我指定的文件夹有写权限。
无法创建数据库。 System.Data.SqlClient.SqlException (0x80131904):文件的目录查找 操作失败“C:\ Users \ Administrator \ Desktop \ test2.mdf”失败 系统错误5(访问被拒绝。)。 CREATE DATABASE失败。一些文件 无法创建列出的名称。检查相关错误。在 System.Data.SqlClient.SqlConnection.OnError(SqlException异常, Boolean breakConnection)at System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION exception,Boolean breakConnection)at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,Tds ParserStateObject stateObj)at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串 methodName,Boolean async)at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result,String methodName,Boolean sendToPipe)at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()at com.appneta.test.DB_DataLoader.createDB(String dbName)
有什么想法吗?
答案 0 :(得分:2)
当您说“我对我指定的文件夹具有写入权限”时。 - 感觉就像你在谈论用户。
您需要确保SQL Server正在运行的服务帐户对您尝试写入的文件夹路径具有写入权限。
答案 1 :(得分:0)
您是否以管理员身份运行visual studio?