Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to create a database using SMO. I get this exception on the Create():

Create failed for database.
An exception occurred while executing a Transact-SQL statement or batch.

I tried may solutions and nothing worked. I ran the VS as administrator, the MSSMS too, I set the permission to all the users. Also I tried running this query in the MSSMS, with my directory, and it actually did work:

SQL
USE [master]
GO
/****** Object:  Database [bioenergiasIpad]    Script Date: 08/07/2012 17:01:19 ******/
CREATE DATABASE [test1] ON  PRIMARY 
( NAME = N'test1', 
  FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1.mdf', 
  SIZE = 70656KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB)
 LOG ON 
( NAME = N'test1_log', 
  FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1_log.ldf', 
  SIZE = 164672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

Source http://dba.stackexchange.com/questions/22250/sql-server-create-file-encountered-operating-system-error-5-access-is-denied

My code is:
C#
Console.WriteLine("Enter username");
        string UserName = Console.ReadLine();
        Console.WriteLine("Enter password");
        string Password = Console.ReadLine();

        string connectionString = null;
        SqlConnection cnn ;

        Server server = new Server();
        server.ConnectionContext.LoginSecure = true;
        try
        {
            connectionString = @"Data Source=" + server.Name + @"\Administrator" +
                            @"AttachDbFilename=|DataDirectory|Resources\DataBase." + UserName + ".mdf;" +
                             "Integrated Security=True;";
            Database database = new Database(server, "DataBase." + UserName);
            database.FileGroups.Add(new FileGroup(database, "PRIMARY"));
            DataFile dtPrimary = new DataFile(database.FileGroups["PRIMARY"],
                     "PriValue", @"Resources\DataBase." + UserName + ".mdf;");
            dtPrimary.Size = 77.0 * 1024.0;
            dtPrimary.GrowthType = FileGrowthType.KB;
            dtPrimary.Growth = 1.0 * 1024.0;
            database.FileGroups["PRIMARY"].Files.Add(dtPrimary);

            LogFile logFile = new LogFile(database, "Log",
                    @"Resources\DataBase." + UserName + ".ldf");
            logFile.Size = 7.0 * 1024.0;
            logFile.GrowthType = FileGrowthType.Percent;
            logFile.Growth = 10.0;

            database.LogFiles.Add(logFile);
            database.Create();
            database.Refresh();
        }

I tried different connection strings.
Oh, and I am running a 2012 SQL.
What am I doing wrong?
Posted
Updated 5-Mar-15 11:54am
v2
Comments
CHill60 6-Mar-15 5:18am    
What is the rest of the error message?
Member 11502168 6-Mar-15 13:05pm    
Microsoft.SqlServer.Management.Smo.FailedOperationException: Create failed for Database 'DataBase.t'. ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. ---> System.Data.SqlClient.SqlException: A file activation error occurred. The physical file name 'Resources\DataBase.t.mdf;' may be incorrect. Diagnose and correct additional errors, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ExecuteNonQuery(StringCollection queries, Boolean includeDbContext)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImplFinish(StringCollection createQuery, ScriptingPreferences sp)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()
at Microsoft.SqlServer.Management.Smo.Database.Create()
at CustomSQLConnectionTest.Program.Main(String[] args) in d:\Visual Studio 2013\Projects\DatabaseCreationTest\DatabaseCreationTest\Program.cs:line 58
Kuthuparakkal 8-Mar-15 14:25pm    
check if Database.t already exists or not ?
Member 11502168 8-Mar-15 22:00pm    
No it doesn't.
But I solved the problem, I wrote the path in the place of the name, and also the path was written wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900