Connection from C# app to MySQL database

Hi,

For the last few hours, I have been trying to find out why I am getting the following error:
“Unable to connect to any of the specified MySQL hosts”.

I am getting the above exception after correctly using the username, password and etc in my C# program.

Please assist…

Thanks!

Hello,

000webhost free servers does not allow external connections to the server database.

You can only use your database from your PHP scripts stored on the server.

Hello,

000webhost free servers does not allow external connections to the server database.

You can only use your database from your PHP scripts stored on the server.

Ok, thanks.

Does this mean that I need to interact with the PHP script stored on the server from my C# app in order to send and retrieve data?

I don’t think you can run your c# App on apache or litespeed web servers. You need IIS server to do this and I know none that offers free hostings.

Good luck.

He’s talking about an application… e.g. an .exe file you download an run from your desktop environment… i believe.

Anyway, yes, you need to setup a php script that runs the commands, then use some webconnector to fetch the data and then you may use it within your program :wink:

Ok, so I will need to prepare a PHP script that can return the MySQL data from the database to my application. In other words, I will call the PHP script from my visual studio application which will return the data to my application and my application will display it. Am I right?

Yes, that’s correct :slight_smile:

Getting the data returned by a PHP page is the best way to do this - connecting to the database directly from your program will surely create some security flaws, because you’re providing the username/password. (C# is compiled to byte code so the program can be reverted back to source code)

However, this is how I would to this: send the data from your program to the webpage using POST/GET method and retrieve the page’s source - and check if the response is what you want.

If you can’t find useful information on Google, I wrote an article that explains how to do this, by using WebClient.

This is a great tactic as well, however this presents its own set of flaws, if the C# is decompiled and it the user notices how it is one he could spoof that, the safest way to do this would be sending the hash for evaluation. However if this is a private program going to only a few people you could always do a mac address filter or IP filter along with it.

thanks,

I have started doing this by adding the following test file (dataRequest.php) to the public_html folder in the server:

<?php
$variable1 = $_POST[‘var1’];

$variable2 = $_POST[‘var2’];

if(strtoupper($variable1) == strtoupper(“hello”) && strtoupper($variable2) == strtoupper(“server!”))

echo("Hello CSharp Program!"); 

else
echo(“What do you want?”);

?>

My question is what should be the URL that I need to put into my C# code? Is following correct?

string URL = “http://mywebsite.webuda.com/public_html/dataRequest.php”;

You don’t need public_html in your URL - it should be ‘http://mywebsite.webuda.com/dataRequest.php’ if dataRequest.php is in the root directory (in public_html).

*All your website files should be into public_html.

@snowizgr8 : a hash would be good, but will not be 100% safe - if someone decompiles the program and finds the salt, he can bruteforce the hash.
But…this will take him some time :smiley:
I don’t actually know what kind of data is sent to the page.

You do have a good point there, although it would be more secure it could actually open a bigger security gap for a hacker :confused:
Did not think about that

@snowizgr8: nothing is safe - but most of the times, those methods will discourage people from trying to crack the program.

Well, let’s keep it simple for the moment - there’s actually no need to involve security measures now - it will only make the project more complicated and harder to understand.

@JBOY: if there are any problems, feel free to ask.

Thank you guys for your help and insight but I am still facing issues, when I run the below code, I see some web page information printed in the console not the things from my php script, so it means it is not communicating. My c# code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string requestMethod = “POST”;
string postData = “var1=Hello&var2=Server!”;

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        string URL = "http://mywebsite.webuda.com/dataRequest.php";

        string contenttype = "application/x-www-form-urlencoded";

        string responseFromServer = null;

        WebRequest request = WebRequest.Create(URL);

        Stream dataStream;

        WebResponse response;

        StreamReader reader;

        request.Method = requestMethod;

        request.ContentType = contenttype;

        request.ContentLength = byteArray.Length;

        dataStream = request.GetRequestStream();

        dataStream.Write(byteArray, 0, byteArray.Length);

        dataStream.Close();

        response = request.GetResponse();
        dataStream = response.GetResponseStream();
        reader = new StreamReader(dataStream);
        responseFromServer = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();
        Console.WriteLine(responseFromServer);
        Console.ReadLine();
    }
}

}

I’m sorry but I don’t actually understand the problem. It should print the page’s source. I tested it on localhost using:

<?php
	echo $_POST["var1"];
	echo $_POST["var2"];
?>

It showed in the console HelloServer!. Maybe I didn’t understand the problem :confused:

Thanks, I just figured out the problem, it was to do with the file name. Appreciate your help.

I have another question, is it ok if I try to learn to use web services to pass the data from my c# app to the PHP script which grabs the data and insert them into the MySQL database?

It’s ok to do this, but you’ll need additional security - someone might exploit this by sending corrupted data to the php page, that can harm your database (Sql Injection). If you really need to do this, try sanitizing the input data first (remove any character that can modify your MySql query).

I don’t really have to do this, but I am just trying to find out the best way to approach this, it seems that POST/GET is the common and safest way to use.
Question about the files in the web server host, right now, I am putting every PHP script inside the public_html folder but this is not right since anyone can view these files by going to my website. So where do I put the files so that I am the only one who can see them by logging in and I can still use them.
For example, if I try to run the dataRequest.php from my c# app, I want to put the dataRequest.php in some directory apart from the public_html. I am not sure where to put it.