[英]error 26- error locating server/instance specified
我在 Visual Studio 2019 中设计了一个 C# 桌面应用程序,并为数据库使用了 sql server express 2019 版。 我正在尝试在另一台电脑上运行这个应用程序。 我已经在另一台电脑上安装了 sql server express 2019,还安装了 MS server management studio 2019 并恢复了数据库。 一切正常,例如登录、保存更新、删除,但是当我尝试将数据提取到 datagridview 时,它显示“system.data.sqlclient.sqlexception - 与 sql 服务器建立连接时发生网络相关或实例特定错误。服务器不是找到或无法访问。验证实例名称是否正确,并且 sql 网络接口,错误:26 - 错误定位服务器/实例指定)。
所有端口均已启用,并且在客户端 PC 中也启用了防火墙规则。
我正在使用以下连接字符串进行连接。
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;
}
将您的 Connection class 扔掉,并将连接字符串传递给 DataAdapter。 不要费心打开或关闭连接; DataAdapter 知道在连接关闭时如何打开连接
将连接字符串放入设置
使用参数
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 字符串中。 曾经。 没有理由这样做,这样做会导致您创建的软件被黑客入侵/您被解雇/两者兼而有之
此外,永远不要以纯文本形式存储密码。 盐和 hash 他们。 我已将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;
}
}
在 IT 人员对 SQL 服务器进行一些安全设置后几天,这个问题也让我感到困惑。 我有一个用于 Web 应用程序和一个桌面应用程序的 EntityFramework。 在我对 SQL 服务器进行一些设置后,Web 应用程序恢复工作,但桌面仍然存在问题。 但我对这两个应用程序都使用了一些连接字符串,一个是工作但另一个没有意义是没有意义的。 然后我搜索了很多,直到我发现有人说需要在 $ServerName$DatabaseInstanceName,1433 之后添加一个端口号 1433 在这里http://www.windows-tech.info/15/9f6dedc097727100.ZE1BFD762321E409CEE4AC0B6E8416 . 在我添加之后。 异常变为:System.Data.SqlClient.SqlException:用户“域\名称-PC$”登录失败。 然后我找到了这个链接System.Data.SqlClient.SqlException: Login failed for user : System.Data.SqlClient.SqlException: Login failed for user it said need add Trusted_Connection=False;. 整个连接字符串应该是这样的:data source=XXXXX\SQLSERVER,1433;initial catalog=XXXDB;user id=UserID;password=PWD;Trusted_Connection=False;MultipleActiveResultSets=True;
希望这个答案能帮助那些摆脱一般异常的人:“错误:26-错误定位服务器/指定的实例)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.