How to Connect to SQL Express "Error: 26-Error Locating Server/Instance Specified)

sql network interfaces error 26 - error locating server/instance specified

  1. Open SQL Server Configuration Manager

  2. Click on SQL Server Network Configuration and click on Protocols for Name

  3. Right click on TCP/IP (make sure it is Enabled), click on Properties

  4. Select IP Addresses Tab and go to the last entry: IP All

  5. Enter TCP Port 1433.

Now restart "SQL Server .Name." using services.msc (winKey + r)

error 26- error locating server/instance specified

Throw your Connection class away, and pass the connection string to the DataAdapter. Don't bother opening or closing the connection; DataAdapter knows how to open a connection if it's closed

Put the connectionstring into the Settings

Sample Image

Use parameters

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

}
}

Use parameters

Just in case you missed it: USE PARAMETERS. Never again, in your life ever, should you concatenate a value into an SQL string. Ever. There is no reason to do it, and doing it will result in the software you create being hacked / you getting fired / both

Also, don't store passwords in plain text, ever. Salt and hash them. I've used string.GetHashcode() for demo purposes, which is not good but better than plaintext


Do the same thing to the not working code:

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

SQL Error 26 Error Locating Server/Instance Specified

You have a named instance, KEVIN-WEBSERVER\SQLSERVER2016, and the named instance by default is listening on some dynamic port, that of course is not 1433.

In order to connect to a named instance you should provide IP,port or you can use SQL Server Browser and pass in your instance name.

Your connection string does the latter, i.e. you pass in only the instance name, so your SQL Server Browser must be started.

Here Overview of the SQL Server Browser service you find more about SQL Server Browser

SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified for MDF file

The whole User Instance and AttachDbFileName= approach is flawed - at best! Also, it's only supported by the Express edition of SQL Server - so if your hoster has anything other than Expres, you're basically screwed.

The real solution in my opinion would be to

  1. install SQL Server Management Studio Express

  2. create your database in SSMS Express, give it a logical name (e.g. MyDatabase)

  3. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=MyDatabase;Integrated Security=True

    and everything else is exactly the same as before...

Now, when you go and upload your web site to the hosting site, you need to get the necessary SQL scripts to create and populate your database on the hosting site - you can do this by using Tasks > Generate Scripts from the SSMS Express tool, or by some other means.

Then, on the hosting site, you need to execute those SQL scripts to create and populate your database (and later on: execute the change scripts to keep the database up to date).

Your connection string will also most likely change to something like:

Server=(ip-address-or-name);Database=MyDatabase;User ID=YourUserIdHere;Pwd=YourPassword

since the hosting provider doesn't use the SQLEXPRESS named instance (but most likely a default instance without any name). You need to ask your provider for the details - it could be that you have to provide some other instance name! That's totally up to the provider, we cannot know that - you'll have to ask and find out.



Related Topics



Leave a reply



Submit