sql network interfaces error 26 - error locating server/instance specified
Open SQL Server Configuration Manager
Click on
SQL Server Network Configuration
and click onProtocols for Name
Right click on
TCP/IP
(make sure it is Enabled), click onProperties
Select
IP Addresses
Tab and go to the last entry:IP All
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
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
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g.
MyDatabase
)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
SQL Server Equivalent to Oracle's Nulls First
How to Select Bottom Most Rows
Join Two Spreadsheets on a Common Column in Excel or Openoffice
Extracting the Total Number of Seconds from an Interval Data-Type
Oracle Row Count of Table by Count(*) VS Num_Rows from Dba_Tables
SQL Server 2008 Thousands Separator for a Column
Execute Query on SQL Server Analysis Services with Ironpython
Minimizing SQL Queries Using Join with One-To-Many Relationship
How to Split Strings in SQL Server
Equivalent Function to Stuff in SQL (Group_Concat in Myssql/Listagg in Oracle)
Postgresql Calculate Difference Between Rows
SQL Statement with Multiple Sets and Wheres
How to Fill Missing Dates by Groups in a Table in SQL
Update Statement Using Nested Query
Join Statement Order of Operation
Can SQL Server SQL_Latin1_General_Cp1_Ci_As Be Safely Converted to Latin1_General_Ci_As