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.
Related
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
I am facing some issues with connectivity .
Question-
1)Is it possible that Web application access the databse but not console application?
2)When I am connecting my console app to our databse on server-"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible ",this error is coming though when I test the connection using setting tab in VS2010 for console application,it says Connection succeed,but as soon as i write Con.open,I get the error mentioned.
3)Also,If i am not able to connect to my db with new application,how come previous build web application on production are working fine.?
NOTE-There has been massive change in Server-Sql instances and all servers were upgraded.
Do I make sense?
Code I am using-
SqlConnection con;
SqlDataReader reader;
try
{
int id;
con = new SqlConnection("Data Source=DAN\\C1;Initial Catalog=tablenames;User ID=user;Password=123");
con.Open();//Error comes here
Exact same connection string works for webapplication but not for Console.Is it have something to do with sql server upgarde and changes.
Any help or guidance?
okay well from what i can see you may want to check that you have imported and used all the required namespaces involved in an SQL connection , you will need to import the following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
After ensuring all the correct namespaces have been imported then you should check your connection string maybe there is a typing error or something there , remember if you make a mistake on the connection string the connection will be refused , and im not sure if this code will work but here goes try this method of connecting :
SqlConnection con;
SqlDataReader reader;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
con = new SqlConnection(connetionString);
try
{
int id;
con.Open();
Console.WriteLine("Connection Open ! ");
cnn.Close();
}
catch (Exception ex) { Console.WriteLine("Can not open connection ! "); }
If the above code gives you problems you can try this other method of doing a SQL connection in a C# console project as seen in the link below:
http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-create-single-connection-string-in-console-applicatio/
First check in the Server Explorer whether you are connected to the Server or database.If u connected to the server, server name will be visible under data connection, right click on it go to properties there u can find connection string value. copy paste that value in your sql connection code. Try this it may solve your connection string problem.
Thanks everyone.But it was some security issue.Once I deploy my code on server,its working.Remote connectivity was somehow restricted in the sql server.
I have a windows form application in C# and sometimes I want to push some data table from this application to an online mySql server which is hosting all data for my website in PHP. To do that I've installed :
1- MySql for visual studio version 1.2.3
2- MySql Connector.Net 6.9
Also, I have enabled Remote MySql on the server so I can make the connection. I used the '%' wildcard for the meantime because my IP address is dynamic.
my basic c# connection code is as below :
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "Server=*******;Database=*******;Uid=******;Pwd=********;";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
MessageBox.Show("connected successfully..");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
Unfortunately, every time I run this code I am getting an error which says
"Unable to connect to any of the specified MySQL hosts".
I don't' know where the problem is. Is it something on the UNIX server which
blocks the connections or some other thing which I need to do. I am also new to the CPanel interface and how to deal with it.
I appreciate all the help provided.
Many Thanks.
http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL
Did you see that? You can try it.
I know this is an old question but I had the same issue many years ago and the only way I could get it to work was to enable remote MySql on the server I was running. This basically means allowing remote connections to access the DB. This of course is very risky and poses a major security problem. You can limit connections from specific IPs, but in my opinion its best to have a local DB and then perhaps push updates to an online source somehow.
I am helping my friend in his college final year project, Its an C# application in a client machine and it uses SQL server in the windows server 2008 environment.
VM ware environment:
3 clients(windows 7) and 1 SQL database (in windows 2008)
The connection between he server and the client is working, the port 1433 for SQL is open and its listening.
When I try to run the program am getting the following error.
This is the connection string which am using.
cn = new SqlConnection(#"Data Source=192.168.1.2,1433;Initial
Catalog=Test;Integrated Security=True");
And i have also tried to refer this website for connection strings, but i don't know how it works.
http://www.connectionstrings.com/sql-server-2008
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN; Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;
I am getting an error. Tried to give a simple username and password in the SQL server and tried it, still the same issue. please check the image. The Username and password which i used is the System login(windows authentication)
I just want to access the SQL server in the Windows 2008 machine.
Where should i check the credentials for this connection string ?
Thank you soo much for reading this patiently.
You can go to Connectionstrings.com to get more information on connection strings and its usages.
Try removing the port from your connection string (1433 is the default SQL port):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=True");
For a connection using sql authentication (which would eliminate trust issues between machines) use the following (replacing xxxx and yyyy with the appropriate user name and password):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=False; User=xxxx; Password=yyyy");
I am writing an application requires to connect to sql server. I prefer to write the App in Java. but When I try to connect to the server I got the connection refused error. I am using JTDS JDBC driver. I think it is due to the port 1433 or 1434 are not open. The server is in my work place and I can not change the ports. But funny enough, I use c# writing the same thing, it connects successfully. is it because SqlConnection class in C# library works better with ms sql server? or am I doing something wrong here? FYI, the server we are using in work is MS SERVER 2003.
Sorry I did not provide any code earlier. One tricky part is that the server we have is called "SERVER" on the local network.
C#:
SqlConnection objConnection = new SqlConnection("Data Source= SERVER\\SQLEXPRESS;Initial Catalog=SSS;Persist Security Info=True;User ID=user;Password=pass");
SqlCommand objcommand = new SqlCommand();
string strSQL;
objcommand.Connection = objConnection;
strSQL = "select * from company where companyid = #companyID ";
try
{
objConnection.Open();
SqlDataReader Query = objcommand.ExecuteReader();
while (Query.Read())
{
MessageBox.Show(Convert.ToString(Query["clientRef"]));
}
objConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error Retreiving info: " + ex.ToString(), "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
objConnection.Close();
}
As I am not really familiar with C#. I have got the code above from one of the colleges. and it returns the info correctly.
Java:
try{
Connection connection;
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:jtds:sqlserver://network.local/SERVER\\SQLEXPRESS:1433/SSS","user","pass");
System.out.println("Connection succeed!");
}
catch (Exception e) {
e.printStackTrace();
}
The java code above got Network error IOException: Connection refused error.
FYI: I can ping server.network.local successfully. But when I telnet server.network.local 1433/1434, I got telnet: Unable to connect to remote host: Connection refused.
Check how your .NET app is connecting. It may be as simple as it using named pipes, which JTDS supports as well.
It might be that the defaults are different, are you setting the port when you initialise your driver
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
Something like this jdbc:jtds:sqlserver://nameofyourdatabaseserver.or.ipaddress:port/yourdatabasename
Try using the full dns name for your server or the IP address
At the following link you will find a useful tutorial which describes in detail how to connect to MS SQLServer database, both from Java and C#. It also describes how to query the database, pass and retrieve data and much more. Hope you find it useful: http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html