Linq Windows 表单 C# System.Data.SqlClient.SqlException

Linq Windows Form C# System.Data.SqlClient.SqlException

我是一名正在做作业的学生,​​这是一个约会系统,我正在使用 Management Studio 作为数据库,我分离它并将 Visual Studio 链接到 .mdf 文件。

我找不到错误在哪里,以前也没有解决过这种类型的错误。

代码:

DataClasses1DataContext db = new DataClasses1DataContext();
        public frmAccountCreation()
        {
            InitializeComponent();
        }
    private void btnCreateAccount_Click(object sender, EventArgs e)
    {
        string email = txtEmail.ToString();
        string fullname = txtFullname.ToString();
        string username = txtUsername.ToString();
        string password = txtPassword.ToString();
        int userlevel;
        string contactnumber =txtContactNumber.ToString();
        if(ckbAdmin.Checked)
        {
            userlevel = 1;
        }
        else
        {
            userlevel = 0;
        }

        tblUserAccount account = new tblUserAccount
        {
            Username = username,
            Passcode = password,
            UserPrivelage = userlevel
        };

        tblVet vet = new tblVet
        {
            Email = email,
            FullName = fullname,
            ContactNumber = contactnumber
        };

        db.tblUserAccounts.InsertOnSubmit(account);
        db.tblVets.InsertOnSubmit(vet);
        db.SubmitChanges();//Error Is here
    }
}

错误:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.Linq.dll

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL

您的 SQL Server Service 似乎不是 运行。

在Windows中搜索"Services",应该有一个同名的Windows应用程序。

在应用程序中继续滚动直到找到 "SQL Server (MSSQLSERVER)" 并确保它是 运行。

查看这一行:

DataClasses1DataContext db = new DataClasses1DataContext("C:\Users\Luke\Desktop\OOP Assignment\OOP_Database.mdf");

您正在尝试直接打开 MDF 文件。你不能这样做。您需要一个 运行 SQL 服务器实例(可以在您的开发机器上,例如 SQL Express),并且您需要一个连接字符串来连接到该服务器。

我通过卸载与 visual studio community 2015 和我的防病毒软件相关的所有东西解决了这个问题,然后我重新安装了一切,一切正常