Best practise while using 'SQlDataAdapter' - sqldataadapter

There is common function I am using in my project for returning dataset. I am using a 'SQLDataAdapter' here. This works fine for 3 other projects. But in one of the projects, this function threw an error when run from one of the client machines. (Works fine on server machine).
Error:-
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or 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)
Here is the source code of the function:-
Public Function Fn_Return_Dataset(ByVal sqlquerys As String) As DataSet
' This function is used to return a dataset for given query
If mydb.State <> ConnectionState.Closed Then
mydb.Close()
End If
Dim dap As New SqlDataAdapter(sqlquerys, mydb)
mydataset = New DataSet()
Try
dap.Fill(mydataset)
Catch Exdat As Exception
writelog(Exdat.Message.ToString, "MYDATA", "FN_RETURN_DATASET")
showmsg("Sorry.....Unable to return dataset")
Finally
If mydb.State <> ConnectionState.Open Then
mydb.Open()
End If
End Try
Return (mydataset)
End Function

If your code works fine on server machine and not on client machine then you have communication problem. Check if your SQL engine has TCP enabled and if firewall is opened. You may have some DNS related/name resolution related problem - check server name in connection string - do simple ping with server name to check if client machine can resolve server name to IP.

Related

Invalid connection string when connecting to SQL Server Express

I tried every single variation that I could find on the internet. Everything. And still every time I get the very same exception:
System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 25 - Connection string is not valid)'
With this weird inner exception:
SocketException: The operation completed successfully
When I specify the port directly, then I end up with this wonderful exception:
An attempt was made to access a socket in a way forbidden by its access permissions
No matter what, it doesn't work. Moreover I'd like to have MS SQL Management Studio open while testing (that was the only process which could block the port).
So why does it not work? Management Studio can connect with IP\InstanceName without a problem. Why is the System.Data.Sql.SqlConnection class incapable of doing so?
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "tcp:***.***.***.***\\SQLEXPRESS";
builder.InitialCatalog = "MyDB";
builder.Password = "MyPass";
builder.UserID = "ID";
builder.IntegratedSecurity = false;
_connection = new SqlConnection(builder.ToString());
FYI: I am using the ConnectionStringBuilder, so there shouldn't be any issue. Server name, instance name, SQL user and password are valid and I did explicitly set Integrated Security = False.
The problem was (which was not really clear) that UWP requires a capabilitiy in order to run that code. I read somewhere that you would need Enterprise Authentication which was not the one. Since my program and server are in a closed, private network I enabled Private Networks (Client & Server).
The one you would need is Internet (Client & Server). Sadly the exceptions don't hint to that and the fact that SqlClient on UWP requires this to work is nearly nowhere mentioned or not stressed enough so you wouldn't overlook it.
Solution found in this .Net Standard issue.
First of all try to connect to this database using SQL Server Management Studio.
If you get the same error, it means that there is a problem with the server not (or not only) with your connection string.
Here is some of possibilities :
1- Remove 'tcp:' from this part: builder.DataSource = "tcp:...\SQLEXPRESS";
2- On the server the SQL Server services (SQL Server or SQL Server browser) are stopped.
To activate go Services and start stopped services.
3- May be the server doesn't accept remote connections:
In this case on the server open SQL Server Configuration Manager
And activate tcp ip.
You should restart SQL Server service after this operation.
Tell me if it works for you.

How to connect to SQL Server 2000 from .net 4

Here's my connection string...
Data Source=MYMACHINE\SERVER2000; Initial Catalog=MyDatabase; User Id=sa; Password=p;
Here's the error....
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
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)
I can connect fine with Enterprise Manager. Also I have upgraded the SQL Server to Serivce Pack 4.
Any ideas what the issue might be?
I'd suggest testing your connection string using a .UDL file. I've answered a similar question here. After creating your connection, you can rename the extension of the file to .txt, open it and then copy and paste the working connection string in.
string connectionString=#"Data Source=MYMACHINE\SERVER2000; Initial Catalog=MyDatabase; User Id=sa; Password=p;";
SqlConnection connection=new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("Select * From Table",connection);
SqlDataReader reader= command.ExecuteReader();
GridView1.DataSource=reader;
GridView1.DataBind();
Use this.... this may help you?

Cannot connect to local instance of SQL Server database using Entity Framework Code First - error: 26 - Error Locating Server/Instance Specified

I have an MVC web application using EF 5.0 (Code first) that connects to a local database (on the same server) using this connection string:
"Data Source={server}\{instance};Database={database};user id=sa;password={password}"
The database is dropped and re-created when the DbContext is first initialised. After that it throws the following exception:
System.Data.ProviderIncompatibleException: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct. ---> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or 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)
I've hosted another application on the same server, using exactly the same connection string to access the database using a SqlConnection object in old-school ADO.Net fashion which connects fine.
I've checked for similar posts and all suggest a number of things which I have tried but have not solved the problem:
Add TCP/IP aliases for server instances
Check firewall allows inbound connectivity via TCP:1433 and UDP:1434 ports
Check that SQL Server Browser is running on the server
Check that the connection string is valid and that the specified user has access to the SQL Server instance
Just to add a odd twist to the whole thing: I can connect to the same database on the same server from my workstation running IIS Express but as soon as it's hosted on the server I get the above problem.

Type provider cannot connect to localDB

I have the following F# script. I copied the connection string from a working Asp.net Mvc project. However, it got the following error. The connection string should be correct since the Asp.Net Mvc project works fine. Or does F# type provider not accept the connection string?
#r "System.Data.Entity.dll"
#r "FSharp.Data.TypeProviders.dll"
#r "System.Data.Linq.dll"
open System.Data.Linq
open System.Data.Entity
open Microsoft.FSharp.Data.TypeProviders
type private EntityConnection = SqlEntityConnection<ConnectionString=
"Data Source=(LocalDb)\v11.0;Initial Catalog=MyDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyDb.mdf",
Pluralize = true>
Script1.fsx(9,33): error FS3033: The type provider 'Microsoft.FSharp.Data.TypeProviders.DesignTime.DataProviders' reported an error: Error reading schema. error 7001: The provider did not return a ProviderManifestToken string. A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) The network path was not found
You might want to check that v in v11.0, it's been known to cause some problems. Even if your connection isn't set to be case sensitive, try upper-casing it and see if it works: V11.0.

Connecting to remote SQL Server 2008 from Windows Azure

I am trying to connect to a SQL Server 2008 instance from a Windows Azure worker role (it's not SQL Azure, but a remotely hosted SQL Server 2008 Standard Edition), but I get the following error message
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or 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) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at ...
The code snippet is the following:
using (var connection = new SqlConnection("MY CONNECTION STRING")
{
connection.Open();
var command = new SqlCommand("Select 1", connection);
try
{
var res = command.ExecuteScalar();
}
catch (Exception e) {
Log.Log("error", e.ToString());
}
}
When I run the snippet from my local command-line (directly accessing the remote SQL Server), it works OK, yet when run from Windows Azure, I end-up with the error message outline here above. In order to make sure there were no problem with the connection string sure, I re-try by hardcoding the string in the source code pushed toward Azure (just to be 100%, but I am still hitting the very same problem).
I have no firewall setup on the SQL Server 2008 instance, and I running out of ideas.
Can someone spot what I am doing wrong here?
I've noticed some issues with the SQL Server Resolution Protocol (or the SQL Server Browser) from Azure worker roles when connecting to external non-Azure SQL Server instances running on ports other than 1433. In these cases it can help to explicitly specify the TCP port of the instance in the connection string:
Data Source={domain/ip},{port};
Network Library=DBMSSOCN;
Initial Catalog={dbname};
User ID={user};
Password={pw}
See also Connect from Azure to an SQL Server Named Instance
Can you control your Azure instance through Remote Desktop?
If you can, log on, and try:
telnet yoursqlserverhostname 1433
If this blocks, you know that the network is not configured correctly. Usually a firewall issue, on either host or the network in between.

Resources