[英]SQL Server EXPRESS: Error: 26 - Error Locating Server/Instance Specified after setup
(C#/ .Net Framework4.0 / VS2017)。 用sqlcon expressconnection數據庫制作了ac#windows表單,但是還不確定如何為其他客戶端創建安裝程序...
我做了一個Windows窗體應用程序,其中帶有sqlconnection的數據庫。 即時通訊使用高級安裝程序來創建安裝程序,因此我可以輕松地將.mdf和.idf文件放在先決條件下。
添加的連接字符串為:
public static class DAL
{
public static DataTable ExecSP(string spName, List<SqlParameter> sqlParams = null)
{
string strConnect = "Server=PC\\SQLEXPRESS;Database=MyLoginApp;Trusted_Connection=True;";
SqlConnection conn = new SqlConnection();
DataTable dt = new DataTable();
try
{
//Connect to the database
conn = new SqlConnection(strConnect);
conn.Open();
//Build an sql command / query
SqlCommand cmd = new SqlCommand(spName, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(sqlParams.ToArray());
//Execute command
SqlCommand command = conn.CreateCommand();
SqlDataReader dr = cmd.ExecuteReader();
//fill datatable with the results
dt.Load(dr);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//No matter what happends this will run
conn.Close();
}
return dt;
}
}
當它嘗試執行以下代碼時:
private void btnLogin_Click(object sender, EventArgs e)
{
List<SqlParameter> sqlParams = new List<SqlParameter>();
sqlParams.Add(new SqlParameter("Username", TxtUsername.Text));
sqlParams.Add(new SqlParameter("Password", txtPassword.Text));
DataTable dtLoginResults = DAL.ExecSP("ValidateLogin", sqlParams);
string eUser;
string ePass;
eUser = TxtUsername.Text;
ePass = txtPassword.Text;
if (dtLoginResults.Rows.Count == 1)
{
//We know login is valid
string user = dtLoginResults.Rows[0]["Username"].ToString();
MessageBox.Show(user + " Berhasil Masuk!");
this.Hide();
ListMeja lm = new ListMeja();
lm.ShowDialog();
}
else
{
//invalid login
MessageBox.Show("Password Salah");
}
}
運行.exe程序后在其他客戶端中獲取錯誤彈出窗口
您的應用程序中發生了未處理的異常。 如果單擊“繼續”,則應用程序將忽略此錯誤並嘗試繼續。 建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。該服務器不可訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:SQL網絡接口,錯誤:26-查找指定的服務器/實例時出錯)。
我在這里做錯了什么?
好..這是解決方案
SqlConnection con = new SqlConnection(@"Data Source = .\SQLEXPRESS;" +
@"AttachDbFilename=|DataDirectory|\MyLoginApp.mdf;Integrated Security = True;User Instance=True");
private void Form1_Load(object sender, EventArgs e)
請嘗試從您的系統將SQL Server與SSMS連接。 成功登錄后,請編輯您的string strConnect = "Server=PC\\\\SQLEXPRESS;Database=MyLoginApp;Trusted_Connection=True;";
根據SSMS已連接的連接字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.