简体   繁体   English

错误 26 - 定位服务器/指定实例时出错

[英]error 26- error locating server/instance specified

I designed a C# desktop app in visual studio 2019 and for database used sql server express 2019 edition.我在 Visual Studio 2019 中设计了一个 C# 桌面应用程序,并为数据库使用了 sql server express 2019 版。 i am trying to run this app on another pc.我正在尝试在另一台电脑上运行这个应用程序。 i have installed sql server express 2019 in the other pc also MS server management studio 2019 installed and restored the database.我已经在另一台电脑上安装了 sql server express 2019,还安装了 MS server management studio 2019 并恢复了数据库。 everything works fine like login,saving updating,deleting but when i try to fetch data to datagridview it shows "system.data.sqlclient.sqlexception - 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 instance name is correct and sql server is configured to allow remote connections.(provider: sql network interfaces, error: 26 - error locating server/instance specified)."一切正常,例如登录、保存更新、删除,但是当我尝试将数据提取到 datagridview 时,它显示“system.data.sqlclient.sqlexception - 与 sql 服务器建立连接时发生网络相关或实例特定错误。服务器不是找到或无法访问。验证实例名称是否正确,并且 sql 网络接口,错误:26 - 错误定位服务器/实例指定)。

all the ports are enabled and firewall rule is also enabled in the client pc.所有端口均已启用,并且在客户端 PC 中也启用了防火墙规则。

i am using the below connection string for the connection.我正在使用以下连接字符串进行连接。

class Connection
  {
       SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=icon;Integrated Security=True");

    public SqlConnection active()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        return con;
    }
}

Please help anyone as i am not able to get what is the problem going on.请帮助任何人,因为我无法解决问题所在。

Belowcode is working下面的代码正在工作

 private void loginBtn_Click(object sender, EventArgs e)
     {
        Connection con = new Connection();
        SqlCommand cmd = new SqlCommand("select * from [user] where 
        Username='" + usernameTxt.Text + "'and password='" + passwordTxt.Text 
        + "'", con.active());
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            MessageBox.Show("Login Successful", "Sucsess", 
              MessageBoxButtons.OK, MessageBoxIcon.Information);
            new dashboard().Show();
            this.Hide();
        }

but this is not working.it shows the error when i try to fetch the data.但这不起作用。当我尝试获取数据时它显示错误。

public partial class AllSudent : Form
{
    public AllSudent()
    {
        InitializeComponent();
    }

    Connection con = new Connection();
    public int studentID;
    private void AllSudent_Load(object sender, EventArgs e)
    {
       
        GetStudentsRecord();
    }

    public void GetStudentsRecord()
    {
        SqlCommand cmd = new SqlCommand("Select * From [student]", 
        con.active());
        DataTable dt = new DataTable();
        SqlDataReader sdr = cmd.ExecuteReader();
        dt.Load(sdr);

        sdataGridView.DataSource = dt;
    }

数据网格的数据集 应用程序的所有文件

Throw your Connection class away, and pass the connection string to the DataAdapter.将您的 Connection class 扔掉,并将连接字符串传递给 DataAdapter。 Don't bother opening or closing the connection;不要费心打开或关闭连接; DataAdapter knows how to open a connection if it's closed DataAdapter 知道在连接关闭时如何打开连接

Put the connectionstring into the Settings将连接字符串放入设置

在此处输入图像描述

Use parameters使用参数

 private void loginBtn_Click(object sender, EventArgs e)
 {
    using(var sda = new SqlDataAdapter("select * from [user] where Username=@user and password=@pass", Properties.Settings.Default.ConStr)
    {
      //USE PARAMETERS
      sda.SelectCommand.Parameters.Add("@user", SqlDbType.VarChar, usernameTxt.Text.Length).Value = usernameTxt.Text;
      sda.SelectCommand.Parameters.Add("@pass", SqlDbType.VarChar, passwordTxt.Text.Length).Value = passwordTxt.Text.GetHashcode(); //DO NOT store your passwords in plain text!!

      var dt = new DataTable();
      sda.Fill(dt);
      if (dt.Rows.Count > 0)
      {
        MessageBox.Show("Login Successful", "Sucsess", 
          MessageBoxButtons.OK, MessageBoxIcon.Information);
        new dashboard().Show();
        this.Hide();
      }

   }
}

Use parameters使用参数

Just in case you missed it: USE PARAMETERS .以防万一你错过了:使用参数 Never again, in your life ever, should you concatenate a value into an SQL string.在您的生活中,再也不会将一个值连接到 SQL 字符串中。 Ever.曾经。 There is no reason to do it, and doing it will result in the software you create being hacked / you getting fired / both没有理由这样做,这样做会导致您创建的软件被黑客入侵/您被解雇/两者兼而有之

Also, don't store passwords in plain text, ever.此外,永远不要以纯文本形式存储密码。 Salt and hash them.盐和 hash 他们。 I've used string.GetHashcode() for demo purposes, which is not good but better than plaintext我已将string.GetHashcode()用于演示目的,效果不好但比纯文本好


Do the same thing to the not working code:对不工作的代码做同样的事情:

public void GetStudentsRecord()
{
    using(var sda = new SqlDataAdapter("Select * From [student]", Properties.Settings.Default.ConStr)){
      var dt = new DataTable();
      sda.Fill(dt);

      sdataGridView.DataSource = dt; 
    }
}

this issue also confusing me a few days after the IT guy do some security settings to the SQL Server.在 IT 人员对 SQL 服务器进行一些安全设置后几天,这个问题也让我感到困惑。 i have an EntityFramework for the Web application and a desktop application.我有一个用于 Web 应用程序和一个桌面应用程序的 EntityFramework。 after i did some setting on the SQL Server, the Web application comeback to work, but the desktop still with issue.在我对 SQL 服务器进行一些设置后,Web 应用程序恢复工作,但桌面仍然存在问题。 but i used the some connection string for the both application, it make no sense one is work but the other doesn't.但我对这两个应用程序都使用了一些连接字符串,一个是工作但另一个没有意义是没有意义的。 then i searched a lot until i found some one said need add a port number 1433 after the $ServerName$DatabaseInstanceName,1433 at here http://www.windows-tech.info/15/9f6dedc097727100.php .然后我搜索了很多,直到我发现有人说需要在 $ServerName$DatabaseInstanceName,1433 之后添加一个端口号 1433 在这里http://www.windows-tech.info/15/9f6dedc097727100.ZE1BFD762321E409CEE4AC0B6E8416 . after i added it.在我添加之后。 the exception became: System.Data.SqlClient.SqlException: Login failed for user 'domain\name-PC$'.异常变为:System.Data.SqlClient.SqlException:用户“域\名称-PC$”登录失败。 then i found this link System.Data.SqlClient.SqlException: Login failed for user : System.Data.SqlClient.SqlException: Login failed for user it said need add Trusted_Connection=False;.然后我找到了这个链接System.Data.SqlClient.SqlException: Login failed for user : System.Data.SqlClient.SqlException: Login failed for user it said need add Trusted_Connection=False;. the whole connection string should be like: data source=XXXXX\SQLSERVER,1433;initial catalog=XXXDB;user id=UserID;password=PWD;Trusted_Connection=False;MultipleActiveResultSets=True;整个连接字符串应该是这样的:data source=XXXXX\SQLSERVER,1433;initial catalog=XXXDB;user id=UserID;password=PWD;Trusted_Connection=False;MultipleActiveResultSets=True;

hope this answer will help the ones out off Generic exception: "Error: 26-Error Locating Server/Instance Specified)希望这个答案能帮助那些摆脱一般异常的人:“错误:26-错误定位服务器/指定的实例)

暂无
暂无

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

相关问题 C#-错误26-定位指定的服务器/实例 - C#-Error 26- Locating Server/Instance Specified ASP.Net错误26错误定位指定的服务器/实例 - ASP.Net Error 26 Error Locating Server/Instance Specified 使用C#的SQL上的错误26,指定了错误定位服务器/实例 - Error 26 on SQL with C#, error locating server / instance specified 错误26的解决方案-指定错误的服务器/实例定位? - Solution for Error 26 - Error Locating Server/Instance Specified? SQL错误:26-查找指定的服务器/实例时出错 - SQL error:26 - Error Locating Server/Instance Specified SQL服务器错误(提供程序:SQL网络接口,错误:26-错误指定服务器/实例的位置) - SQL SERVER ERROR (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 在我的VM上:SQL网络接口,错误:26-查找指定的服务器/实例时出错 - on my VM: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified 连接数据库ASP .NET MVC 5 SQL网络接口时出现问题,错误:26-指定服务器/实例时出错 - Issue connecting to databases ASP .NET MVC 5 SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified SQL网络接口,错误:26-定位本地数据库上指定的服务器/实例时出错 - SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified on LocalDB 脚手架后的迁移问题。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例) - Migration issue after scafolding. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM