[英]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.