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

时间:2021-02-08 10:44:08

标签: c# sql-server

我在 Visual Studio 2019 中设计了一个 C# 桌面应用程序,并为数据库使用了 sql server express 2019 版。我正在尝试在另一台电脑上运行这个应用程序。我已经在另一台电脑上安装了 sql server express 2019,也安装了 MS server management studio 2019 并恢复了数据库。一切正常,例如登录、保存更新、删除,但是当我尝试将数据提取到 datagridview 时,它显示“system.data.sqlclient.sqlexception - 与 sql server 建立连接时发生网络相关或特定于实例的错误。服务器不是找到或无法访问。请验证实例名称是否正确,并且 sql server 已配置为允许远程连接。(提供程序:sql 网络接口,错误:26 - 指定的服务器/实例定位错误。”

所有端口都已启用,并且客户端电脑中的防火墙规则也已启用。

我正在使用以下连接字符串进行连接。

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

请帮助任何人,因为我无法了解发生了什么问题。

下面的代码正在运行

 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();
        }

但这不起作用。它在我尝试获取数据时显示错误。

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

dataset for datagridAll files of the app

1 个答案:

答案 0 :(得分:1)

丢弃您的 Connection 类,并将连接字符串传递给 DataAdapter。不要打扰打开或关闭连接;如果连接已关闭,DataAdapter 知道如何打开连接

将连接字符串放入设置中

enter image description here

使用参数

 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();
      }

   }
}

使用参数

以防万一您错过了:使用参数。在您的生活中,您再也不会将一个值连接到一个 SQL 字符串中。曾经。没有理由这样做,而且这样做will result in the software you create being hacked/你被解雇了/两者

另外,永远不要以纯文本形式存储密码。盐和散列它们。我已经将 string.GetHashcode() 用于演示目的,它不好但比纯文本好


对不工作的代码做同样的事情:

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; 
    }
}
相关问题