简体   繁体   中英

SQL Server EXPRESS: Error: 26 - Error Locating Server/Instance Specified after setup

(C#/ .Net Framework4.0 / VS2017). made ac# windows form with sqlcon expressconnection database,but rly not sure how to create a setup for other client...

i have made a windows form application with a database with sqlconnection in it. im using advance installer to create a setup so i can put the .mdf and .idf files with prerequsites easily.

added connection string as:

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

When it trying to execute the following code:

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

gets an error popup window in other client after run the .exe program

Unhandled exception has occured in your application. if you click Continue, the application will ignore this error and and attempt to continue. A network -related or instance-specific error occured while establish a connection to SQL server.The server was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified).

what im doing wrong here ?

well.. this is the solution

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)

Please try to Connect SQL Server With SSMS from Your system. After Successfully Login Please Edit your string strConnect = "Server=PC\\\\SQLEXPRESS;Database=MyLoginApp;Trusted_Connection=True;"; connection string as per SSMS Connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM