简体   繁体   English

Linq Windows窗体C#System.Data.SqlClient.SqlException

[英]Linq Windows Form C# System.Data.SqlClient.SqlException

I'm a student doing an assignment which is an appointment system, I'm using Management Studio for the database, I detached it and linked Visual Studio with the .mdf file. 我是一名学生,正在做一个约会系统的作业,我在数据库中使用Management Studio,我将其分离,并将Visual Studio与.mdf文件链接。

I can't find where the error is and never solved this type of error before. 我找不到错误所在,也从未解决过此类错误。

Code: 码:

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
    }
}

Error: 错误:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.Linq.dll System.Data.Linq.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理异常

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. 附加信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL 验证实例名称正确和SQL

Looking at this line: 看这行:

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

You are trying to open the MDF file directly. 您正在尝试直接打开MDF文件。 You cannot do that. 你不能这样做。 You need a running SQL server instance (which can be on your development machine, like SQL Express), and you will need a connection string to connect to that server. 您需要一个正在运行的SQL Server实例(可以在开发计算机上,例如SQL Express),并且需要一个连接字符串来连接到该服务器。

重新安装一切后,我通过卸载与Visual Studio Community 2015和防病毒软件有关的所有内容解决了该问题

It seems your SQL Server Service is not running. 看来您的SQL Server Service未运行。

In Windows, search for "Services" and there should be a Windows application with the same name. 在Windows中,搜索“服务”,然后应该有一个具有相同名称的Windows应用程序。

In the application keep scrolling untill you find "SQL Server (MSSQLSERVER)" and make sure it's running. 在应用程序中继续滚动,直到找到“ SQL Server(MSSQLSERVER)”并确保它正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.SqlClient.SqlException C# - System.Data.SqlClient.SqlException C# C#MySQL错误System.Data.SqlClient.SqlException: - C# MySQL error System.Data.SqlClient.SqlException: c#System.Data.SqlClient.SqlException(0x80131904) - c# System.Data.SqlClient.SqlException (0x80131904) System.Data.SqlClient.SqlException:','附近的语法不正确。 C# - System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.' c# System.Data.SqlClient.SqlException - System.Data.SqlClient.SqlException 'System.Data.SqlClient.SqlException' - 'System.Data.SqlClient.SqlException' 在System.Data.dll中发生了C#MS Access数据库连接System.Data.SqlClient.SqlException” - C# MS Access Database Connection System.Data.SqlClient.SqlException' occurred in System.Data.dll 错误System.Data.SqlClient.SqlException:'将数据类型nvarchar转换为数字时出错。 在c#中 - Error System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in c# 尝试从C#代码运行脚本SQL时System.Data.SqlClient.SqlException - System.Data.SqlClient.SqlException when try to run script SQL from C# code C# System.Data.SqlClient.SqlException 无法将值 NULL 插入列 ERROR FOREIGN KEY - C# System.Data.SqlClient.SqlException Cannot insert the value NULL into column ERROR FOREIGN KEY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM