这是我遇到的问题:发生异常epos\SQLPlaygroundV1\SchoolDB.mdf 失败。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。创建表的 SQL 脚本:CREATE TABLE [dbo].[Student]( [Id] INT IDENTITY NOT NULL PRIMARY KEY, [FirstName] NCHAR(16) NULL, [LastName] NCHAR(16) NULL, [Age] NCHAR(3) NULL, [DateOfBirth] DATE NULL, [Enrolled] BIT NULL)C#用于写入数据库:using (SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Admin-PC\\source\repos\\SQLPlaygroundV1\\SchoolDB.mdf;Integrated Security=True")) { using (SqlCommand command = new SqlCommand()) { command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = "INSERT into Student (FirstName, LastName, Age, DateOfBirth, Enrolled)" + " VALUES (@FirstName, @LastName, @Age, @DateOfBirth, @Enrolled)"; command.Parameters.AddWithValue("@FirstName", "Bob"); command.Parameters.AddWithValue("@LastName", "Bobson"); command.Parameters.AddWithValue("@Age", "21"); command.Parameters.AddWithValue("@DateOfBirth", "1990-01-01"); command.Parameters.AddWithValue("@Enrolled", false); try { connection.Open(); int recordsAffected = command.ExecuteNonQuery(); Console.WriteLine(recordsAffected); } catch (Exception e) { Console.WriteLine("Exception occurred"); Console.WriteLine(e.Message); } finally { connection.Close(); } Console.ReadKey(); }}我已经验证不存在其他同名文件,连接字符串是从服务器资源管理器属性窗口复制和粘贴的。我可以看到它存在并且它在正确的路径中等等,我不明白为什么它没有找到它/无法访问它。
添加回答
举报
0/150
提交
取消