SQL异常存在同名数据库,或无法打开指定文件
SQL Exception A database with the same name exists, or specified file cannot be opened
这是我遇到的问题:
Exception occurred
epos\SQLPlaygroundV1\SchoolDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
SQL 脚本创建 table:
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();
}
}
我已确认不存在其他同名文件,连接字符串是从服务器资源管理器属性中复制并粘贴的 window。
我可以看到它存在并且在正确的路径中等等,我不明白为什么找不到它/无法访问它。
问题是连接查询,数据库路径,之前repos
你只有一个反斜杠\
你需要再加一个
这是我遇到的问题:
Exception occurred
epos\SQLPlaygroundV1\SchoolDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
SQL 脚本创建 table:
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();
}
}
我已确认不存在其他同名文件,连接字符串是从服务器资源管理器属性中复制并粘贴的 window。
我可以看到它存在并且在正确的路径中等等,我不明白为什么找不到它/无法访问它。
问题是连接查询,数据库路径,之前repos
你只有一个反斜杠\
你需要再加一个