gpt4 book ai didi

c# - 使用 t-SQL 创建数据库而不指定其文件名

转载 作者:太空狗 更新时间:2023-10-29 23:31:45 25 4
gpt4 key购买 nike

我尝试运行以下 SQL 语句来创建数据库:

string strSQL = "CREATE DATABASE " + strDatabaseName +
" ON PRIMARY " +
"(" +
"SIZE = 10MB, FILEGROWTH = 20%) " +
"LOG ON (" +
"SIZE = 5MB, " +
"FILEGROWTH = 20%)" +
" COLLATE SQL_Latin1_General_CP1_CI_AS;";

我想使用默认的 .mdf 和 .ldf 文件位置,但指定大小和文件增长参数。问题是当我运行它时出现错误:

File option FILENAME is required in this CREATE/ALTER DATABASE statement.

那么有什么方法可以实现我想要实现的目标吗?

最佳答案

试试这个。

      string strSql = " DECLARE @data_path nvarchar(256); "+
"SET @data_path = (SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)"+
" FROM master.sys.master_files"+
" WHERE database_id = 1 AND file_id = 1);"+


"EXECUTE ('CREATE DATABASE " + strDataBaseName +
"ON PRIMARY "+
"("+
" NAME = FileStreamDB_data "+
" ,FILENAME = ''' + @data_path + '" + strDataBaseName +"_data.mdf''"+
" ,SIZE = 10MB"+
" ,MAXSIZE = 50MB"+
" ,FILEGROWTH = 15%"+
" )LOG ON ("+
" NAME = FileStreamDB_log"+
" ,FILENAME = ''' + @data_path + '" + strDataBaseName + "_log.ldf''" +
" ,SIZE = 5MB, "+
" FILEGROWTH = 20%)"+
" COLLATE SQL_Latin1_General_CP1_CI_AS')";

关于c# - 使用 t-SQL 创建数据库而不指定其文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com