Visual Basic 内置数据库连接字符串语法错误

Visual Basic Built in database Connection String Syntax Error

我正在为我的程序使用内置数据库。当我尝试放入连接字符串时,VB 无法检测到连接字符串,并在新的 SqlConnection 之后的第 7 行显示语法错误。我确信我从属性页面复制了完整的连接字符串。

我读过this post,但这似乎是一个不同的问题。下面是我的连接代码。我的代码有什么错误吗?感谢大家的帮助!

    Imports System.Data.SqlClient 
    Public Class Login
    Dim cmd As SqlCommand
    Dim dr As SqlDataReader
    Dim da As SqlDataAdapter
    Dim sql As String
    Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")

这显然会向您显示语法错误,请查看您的以下行:

"C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"

将双引号替换为 ""<abc>"" 以获得类似 "<abc>" 的执行,因为你已经在 [=16= 中使用了 "<abc>" ].

而不是:

Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")

你应该有:

Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"";Integrated Security=True")