UWP SQL connection Error [c#] - c#

I try to connect to a SQL Server 2008 instance with SQL Server credentials.
With a WPF app, I have no problem.
I follow this example:
https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases
I have read this:
How to connect to SQL server database from a Windows 10 UWP app
My connection string is:
static private string connectionString =
#"Data Source=SERVER\SQLSERVER;Initial Catalog=Something;
Integrated Security=false;Persist Security Info=True;User ID=user;Password=pass";
conn = new SqlConnection(connectionString);
try
{
conn.Open();
}
catch(Exception z)
{
Debug.WriteLine(z.ToString());
}
I get this error
System.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: TCP Provider, error: 0 - L’opération a réussi)
If I try
static private string connectionString =
#"Data Source=SERVER\SQLSERVER;Initial Catalog = Something;
Integrated Security=true;User ID = user;Password= pass";
I get this error
System.Data.SqlClient.SqlException (0x80131904): Failed to generate SSPI context. ErrorCode=DowngradeDetected
The same error with Integrated Security=SSPI
If someone has an idea please?
Thanks
Benoit

You need to check the appropriate capabilities in your appx manifest that are required for accessing the resource on your network. Open Package.appxmanifest in VS and go to the 'Capabilities' tab.

Try this code
optionsBuilder.UseSqlServer("Data Source = SERVER\SQLSERVER; Initial Catalog = Something; Integrated Security = False; User Id = user; Password = pass; MultipleActiveResultSets = True");
Make sure that the TCP is enabled in the SQL Server Configuration Manager
I remember
  The case can be solved by installing a small repair for the server
Before that, try another server
I remembered the third case
Install Kerberos
See link
https://support.microsoft.com/en-us/help/811889/how-to-troubleshoot-the-cannot-generate-sspi-context-error-message
Download and install from the link
https://www.microsoft.com/en-us/download/details.aspx?id=39046

I try with a new VM and SQL EXPRESS 2014
It's working.
I think UWP and SQL SERVER 2008 are not compatible.
Thanks for your help Rami.
Benoit

Related

ADOMD connection to SSAS

I tried to connect my c# app to the SQL Server Analysis Services server, to execute some MDX query.
The connection is done as follows :
try{
string connetionString;
connetionString = #"Data Source=localhost;Catalog=SalesCubeDB;";
AdomdConnection cnn;
cnn = new AdomdConnection(connetionString);
cnn.Open();
Console.WriteLine("Connection established !!");
}catch(Exception e)
{
Console.WriteLine(e.ToString());
}
This connection throws the following error :
AdomdConnectionException: A connection cannot be made. Ensure that the server is running
I tried Servername instead of 'localhost', I added rules in the firewall for the traffic and also made sure that the Server Browser uses 'Local System'. If somebody has any ideas i will be grateful, THANKS.
Environment : Windows 10 VM, SQL server 19, ADOMD version 19.26.1.2
I solved my issue and this answer to everyone who might stumble on the same issue.
The client app is a console app, so I added the following packages :
System.Configuration.ConfigurationManager
Microsoft.ServiceFabric.Services.Remoting
I also added the following to the Connection String (I use Windows authentification) :
persist security info=True;Integrated Security = SSPI;
I also tried connecting using IIS 8,0 and worked perfectly.

Unable to connect to MySql Server in Visual C#

I am trying to create a local database using MySql in Visual C# but connection is not getting established in the code but when i add the server in the server explorer under data connections its working. I tried test connection it says connection succeeded but its not working in the code.
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
con = new SqlConnection(connStr);
This throws an error saying
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)
You cannot use the SqlConnection class to connect to a MySQL server; that is for Microsoft SQL Server only.
Instead, install the MySqlConnector NuGet package and use the following code:
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
using (var con = new MySqlConnection(connStr))
{
// ...
}
Your connection string is wrong.. change keys "user" to "Uid" and "password" to "Pwd".
follow eg:
string connStr = "server=localhost;Uid=root;database=entry_database;Pwd=superadmin";
con = new SqlConnection(connStr);
Correct, the format used by you to create the connection string is incorrect, giving you the error you mentioned above.
Actually, the way the connection string is written in MS-SQL is very different from MY-SQL. Every database has its own way of connecting and its individual connection tags:-
Please find the list of connection details for MS_SQL below. This would help you not only in the current mentioned scenario but also will provide a reference for future changes:-
https://www.connectionstrings.com/mysql/

ASP.NET Connection string error when using ip address

I'm trying to debug an ASP.NET page I have trying to connect to a local SQLServer DB, when the connection string I'm using has the name of the local machine, it works fine like in below
string ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=ARTICLEDB;" +
"Data Source=MYMACHINENAME\\MYSQLSVR;";
... later on...
conn = new SqlConnection(ConnectionString);
// Open the connection
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
//don't work here when using ##.##.##.##\MYSQLSVR as datasource
}
return true;
}
catch
{
return false;
}
when I use the IP machine as the value for the datadource such as: ###.###.###.###\\\MYSQLSVR the I get an exception saying:
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: 28 - Server doesn't support requested
protocol)
This problem originated from trying to run the site from IIS and getting a similar error where the connection was not opened.
How can I reference the DB in the connection string so that IIS can find the DB?
I've also tried adding the connection string in IIS for the site but not sure how to reference that IIS connection string from my project/site
go to SQL Server Configuration Manager and turn on "Named Pipes" and "TCP/IP"
and your connection string should look like below
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
http://www.connectionstrings.com/sql-server-2008
Check the following it is working for me.
Data Source=190.190.200.100,1433;Initial Catalog=DBName;Integrated Security=False;user id=userid;password=password

Connection string for SQL Server?

I am working with SQL Server. I used all connection via :
SqlConnection con = new SqlConnection("data source = .; database = xyzDatabase; integrated security = true");//developed with developer edition.
Now I want to run my application on SQL Server Express, so I used this :
SqlConnection con = new SqlConnection("data source = .\\sqlexpress; initial catalog = xyzDatabase; user id = sa; password = 123");
Point to remember is, I am creating my xyzDatabase at runtime on form load event. But when I execute the program I get this 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: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server)
(.Net SqlClient Data Provider)
Import the namespaces
using System.Data.SqlClient (if using C#)
or
Imports System.Data.SqlClient (if using VB#)
Your Sql connection object with connection would be like this:
SqlConnection con = new SqlConnection("Data Source=.;Database=xyzDatabase;User Id=sa;Password=admin#123;");
If still your error not resolved, it might be the problem of incorrect installation of MS-SQL SERVER instances.
http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/
Hope it might be helpful.
you must active sa user in sql express edition.
SqlConnection cn = new SqlConnection("datasource=.\sqlexpress; initialcatalog=yzDatabase; userid=sa;password=123");

ADO.NET: Can't connect to mdf database file

I'm writing an application that uses a SQL Server 2005 database. In the connection string I'm specifying the mdf file like this:
connstr = #"Data Source=.\SQLEXPRESS; AttachDbFilename=" + fileLocation + "; Integrated Security=True; User Instance=True";
When I execute the code:
public static void forceConnection()
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if(conn != null)
conn.Close();
}
}
I receive an exception:
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)
This code works on XP but not in Vista. I tried to run Visual Studio in admin mode and moved the mdf file to "user data" folders but the error persists.
Any help?
Can you connect to the sql server db in your command prompt? I would make sure you can actually connect first.
Try open the cmd prompt and type sqlcmd -S .\SQLEXPRESS -d your_dbase
If I have mssql-connect problems with my dotnet-sourcecode I try to connect to the database with a different program. I use queryexpress that is also written in dotnet. If this program works then I know that the problem is with my program code else the problem is with connection string, proxy, network, sqlserver or user permissions.
Do you actually have sqlexpress installed? does it use the machineName\sqlexpress or does it run as a default instance?
You have to verify these cases.
and you might wanna use the actual machineName\instance name if you aren't using the default instance.

Categories