简体   繁体   中英

How to solve System.Data.SqlClient.SqlException:

I'm getting this error whenever I run my code:

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 Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I tried using the solutions provided in the internet, however nothing seems to work. Like the one using the windows services, I can't find the "SQL SERVER (NAME)" in the list of Services Local.

SqlConnection connect = new SqlConnection (@"Data Source = DESKTOP-766P0MJ; Initial Catalog = LoginCredentials; Integrated Security = True");
SqlDataAdapter adapt = new SqlDataAdapter("SELECT COUNT(*) FROM LoginCredentials where username = '" + txtbox_username.Text + "' AND password = '" + txtbox_password.Text + "'", connect);

DataTable dt = new DataTable(); //this is creating a virtual table  
adapt.Fill(dt);

if (dt.Rows[0][0].ToString() == "1")
{
    /* I have made a new page called home page. If the user is successfully authenticated then the form will be moved to the next form */
    this.Hide();
    new InternRequest().Show();
}
else
    MessageBox.Show("Invalid username or password");

如果您的 sql server 服务已启动并且您再次出现此错误,则启动另一个名称为SQL Server Browser sql server 服务,当您在服务器名称中运行 sql server management studio 时,您可以选择Browse for more并在数据库引擎中显示您的实例sql server的名称点击它并连接它并检查您的用户名和密码以进行连接。

open run and type services.msc. The services window will open . In that window find MSSQLSERVER and its related files. Start it or you may restart it if if it is started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM