Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:45:24+00:00 2026-06-12T14:45:24+00:00

Background: I am using sql statements to create a Temp database on a server

  • 0

Background:

I am using sql statements to create a Temp database on a server which will store data until it is needed further by my client program.

Problem:

My sql statement to create the database works properly and creates the database with all the required specifications when run through Sql Management studio, on the other hand when my program executes the statement it only creates a database with the ‘Default’ settings except for the name.

Questions:

  1. Why is this?
  2. How can I make it create a database with my specifications

Sql statement:

CREATE DATABASE Temp ON PRIMARY(
NAME = Temp
, FILENAME = ‘C:\Temp.mdf’
, SIZE = 2MB
, FILEGROWTH = 10%) LOG ON (
NAME = Temp_Log
, FILENAME = ‘C:\Temp.ldf’
, SIZE = 1MB, MAXSIZE = 70MB
, FILEGROWTH = 10%)

Code:

public void AcuConvert()
        {
            using (DestD)
            {
                SqlCommand command = new SqlCommand();
                DestD.Open();
                command.Connection = DestD;
                foreach (var item in Entity.SqlDestinationQueries.ToList())
                {
                    command.CommandText = item.Query; 
                    command.ExecuteNonQuery();    //This is where the command is run
                }
                foreach (var item in Entity.SystemQueries.ToList())
                {
                    command.CommandText = item.Query.Replace("@Sys", SysD.Database);
                    command.ExecuteNonQuery();
                }
                foreach (var item in Entity.InsertQueries.ToList())
                {
                    command.CommandText = item.Query.Replace("@Source", SourceD.Database); ;
                    command.ExecuteNonQuery();
                }

            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:45:26+00:00Added an answer on June 12, 2026 at 2:45 pm

    Have you tried using SQL Server Management Objects instead of a raw SQL statement?

    For example:

    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Common;
    
    ...
    
    // Connect to the default instance
    Server server = new Server();
    // Establish new database details
    Database database = new Database(server, "MyTempDB");
    // Add primary filegroup details
    database.FileGroups.Add(new FileGroup(database, "PRIMARY"));
    
    // Set Primary datafile properties
    DataFile primaryFile = new DataFile(database.FileGroups["PRIMARY"],
        "MyTempDB_Data", "C:\\MyTempDB.mdf");
    primaryFile.Size = 2048;   // Sizes are in KB
    primaryFile.GrowthType = FileGrowthType.Percent;
    primaryFile.Growth = 10;
    // Add to the Primary filegroup
    database.FileGroups["PRIMARY"].Files.Add(primaryFile);
    
    // Define the log file
    LogFile logfile = new LogFile(database, "MyTempDB_Log", "C:\\MyTempDB_Log.ldf");
    logfile.Size = 1024;
    logfile.GrowthType = FileGrowthType.Percent;
    logfile.Growth = 10;
    logfile.MaxSize = 70 * 1024;
    // Add to the database
    database.LogFiles.Add(logfile);
    
    // Create
    database.Create();
    database.Refresh();
    

    You can connect to the server with specific credentials, too, of course:

    http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx

    If, however, you’re stuck with using text scripts to create your database, I’d ensure that your Initial Catalog is set correctly (i.e. to ‘master’) in your connection string and your user has the necessary permissions – CREATE DATABASE / CREATE ANY DATABASE / ALTER DATABASE. If this still doesn’t give you any joy, try stripping out the rest of your C# code and run the create SQL independently of the other statements – it could be that there’s a side-effect from a preceding query. Use Profiler to see exactly what’s running as you add them back in.

    Edit:

    I tested your script against a local SQL Server instance (2012 SqlLocalDB) via a small C# program using SqlClient and it ran just fine after changing the file paths to ones I had write access to (root of C is protected by default). The only other amendment was that the Primary size had to start at 3MB or more. Any smaller and the default tables could not be created. This may be another avenue of investigation for you to explore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background Create a series of SQL JOIN statements using two operands: primary and secondary.
Background: I'm using SQL Server Management Studio 2008 and I have a query window
I'm coming from a MS SQL Server background. Working on a new project using
Background: today, an issue arose while using SQL Server Reporting Services. It seems that
Background info: I'm coding with C#, using Microsoft SQL Server for databases. I didn't
Please help! Background info I have a WPF application which accesses a SQL Server
Background: I had a working database called WBPM which I renamed using the right-click
Background: Using jQuery 1.7 client side PHP server side Using json responses with json_encode
I'm trying to create a new spinner background using a 9 patch picture. I've
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.