title: Exception thrown trying to create a DB with SMO C#
body: <p>I am trying to create a database using SMO. I get this exception on the <code>Create()</code>:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>I tried may solutions and nothing worked. I ran the VS as administrator, the SQL Server Management Studio too, I set the permission to all the users. Also I tried running this query in the SSMS, with my directory, and it actually did work:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Source here</p>
  
  <p>My code is:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>I tried different connection strings.</p>
  
  <p>Oh, and I am using SQL Server 2012.</p>
  
  <p>What am I doing wrong?</p>