3

背景:

我正在使用 sql 语句在服务器上创建一个临时数据库,该数据库将存储数据,直到我的客户端程序进一步需要它为止。

问题:

我创建数据库的 sql 语句正常工作,并在通过 Sql Management Studio 运行时创建具有所有必需规范的数据库,另一方面,当我的程序执行该语句时,它只创建一个具有“默认”设置的数据库,但名称除外.

问题:

  1. 为什么是这样?
  2. 我怎样才能让它根据我的规范创建一个数据库

sql语句:

在 PRIMARY 上创建数据库温度(名称 = Temp,文件名 = 'C:\Temp.mdf',大小 = 2MB,文件增长 = 10%)登录(名称 = Temp_Log,文件名 = 'C:\Temp.ldf',大小 = 1MB,MAXSIZE = 70MB,文件增长 = 10%)

代码:

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();
                }

            }
        }
4

2 回答 2

5

您是否尝试过使用SQL Server 管理对象而不是原始 SQL 语句?

例如:

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();

当然,您也可以使用特定凭据连接到服务器:

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.

于 2012-10-10T21:06:01.163 回答
0

Your alternative option could be to use the Process class to run the sqlcmd.exe Eg. var process = Process.Start(WORKING_PATH, argument);

%WORKING_PATH% being "C:\tools\sql\sqlcmd.exe"

%argument% being "C:\scripts\Create.sql"

I use this strategy to dump test data into test environments when bootstrapping acceptance test fixtures.

Cheers

于 2012-10-16T08:51:04.363 回答