How to Connect to Remote MySQL Server with my C# Application - c#

I need to connect my C# application developed in my Standalone PC with my Hosted Linuux MySQL Server. How can i do it.. Is there any server configuration setup or any kind of Remote Connection Permission Setting have to be done? Please help with this..

Make sure that the server where MySQL is at can accept connections.
Read this to read how to configure your c# application to connect to MySQL.

The connection string code should like this
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
You need to allow the remote connection by this way

Mysql has very good documentation how to connect mysql server (local and remote).
It is here http://dev.mysql.com/doc/index-connectors.html
And your case may be this http://dev.mysql.com/doc/connector-c/en/index.html

Its Pretty Simple to Implement
1) Download My Sql Connector From https://dev.mysql.com/downloads/connector/net/6.9.html
2) In your C# Project add refrence of Mysql.Data.Dll
3) use this Connection String
string connectionString= "SERVER=000.000.000.000;DATABASE=testdb;UID=test;PASSWORD=test123;"
4) use same like this sample code
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
// Perform database operations
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
or Read this tutorials
https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-connection.html

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.

How to connect remotely to MySQL server

What is the best way to connect remotely to MySQL server on domain ?
I want to make an desktop app or maybe it is easier to do it with asp.net ?
Use Mysql Workbench for desktop use to connect to Mysql Server.
For coding: Get Mysql client library for .net . It's called mysql connector. Add
using Mysql.Data; namespace
https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html
Makes no difference what kind of application you are creating.
your code will look very similar like this one:
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test;";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
1- I usually use HeidiSQL tool to connect to MYSQL server otherwise on localhost or remotely servers on CPanels.
most of CPanels allow to remote connection by adding % at access hosts field to enable me connect remotely anywhere.
from your cpanel go to the database tools then -> you will find link for Remote mysql -> then you can any IP address to make server allow to connect, if you want connect anywhere you put %.
Note: CPanels are little different each others.
2- if you want connect to mysql in your code, you make a normal connection after doing step 1.

Connect desktop c# application to online mySql databse

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.

connect mysql with c#

I'm building a c# windows application which will connect with the mysql database in a remote server.
I'm using the following connect script
string connectionString;
connectionString = "SERVER = eu5.org;UID = myuserid; PASSWORD = mypassword; DATABASE = mydatabasename;";
connection = new MySqlConnection(connectionString);
It shows the error couldn't connect to the database.
P.S: Mysql database is at eu5.org server
Personally I prefer to use the MySql Workbench IDE for testing connections and working (Querying) the database directly where possible. Most hosted databases that I have worked with normally define the Server as Instance.[DomainName] so I would have expected your server URL to be something like MySql1.eu5.org
Below is a connection string that I tested using the MySql Connector, change the parameters.
<connectionStrings>
<add name="MySqlConnection" connectionString="server=INSTANCENAME.DOMAINNAME.COM;UID=USERNAME;password=PASSWORD;database=DATABASENAME;Persist Security Info=True;" providerName="MySql.Data.MySqlClient"/>
MAke sure that your server should be started...
Just read the FAQ:
Many people want to only use database, but their site is hosted elsewhere. We provide free database for websites hosted with us. For that reason, external access is blocked without exception.
--> http://www.freewebhostingarea.com/faq.html
so it's not possible to access your MySQL-DB from external sources.
only localhost will be allowed
Your code is correct.
Use a browser for MySQL, like http://mysql-query-browser-for-windows.apponic.com/ to check if your database is available.

What is the difference between Java and C# when it comes to connect to Sql Server Database?

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

Categories