我正在尝试以编程方式创建一个数据库并在其中创建一个表。
这是我的代码:
string connectionstr = string.Format(@"Data Source=.\sqlexpress;Initial Catalog={0};Integrated Security=True", "books");
SqlConnection myConn = new SqlConnection(connectionstr);
DirectoryInfo dir = new DirectoryInfo(@"D:\Data");
dir.Create();
string str3 = @"CREATE DATABASE [books] ON PRIMARY
( NAME = N'books', FILENAME = N'D:\DATA\books.mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'books_log', FILENAME = N'D:\DATA\books_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY')
ALTER DATABASE [books] MODIFY FILEGROUP [PRIMARY] DEFAULT";
SqlCommand myCommand = new SqlCommand(str3, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
但我收到错误:
无法打开登录请求的数据库“书籍”。登录失败。用户“Master-PC\Master”登录失败。
我能做些什么来解决这个问题?