Author Topic: PHPMyAdmin & C#  (Read 3187 times)

0 Members and 1 Guest are viewing this topic.

Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
PHPMyAdmin & C#
« on: November 16, 2010, 06:20:08 AM »
Is it possible to connect to a php database like PHPMyAdmin through C#? I know that you can include databases in your Windows Applications, but I was wondering if you can connect/query a database that is not on the localhost machine. BTW I am not trying to connect from an ASP.NET web page, but from a Windows Application.

I found this code online, but cannot get it to work.
Code found @ http://www.jonasjohn.de/snippets/csharp/sql-connection-example.htm
Code: C# [Select]
  1. // This example needs the
  2. // System.Data.SqlClient library
  3.  
  4. #region Building the connection string
  5.  
  6. string Server = "localhost";
  7. string Username = "my_username";
  8. string Password = "my_password";
  9. string Database = "my_database";
  10.  
  11. string ConnectionString = "Data Source=" + Server + ";";
  12. ConnectionString += "User ID=" + Username + ";";
  13. ConnectionString += "Password=" + Password + ";";
  14. ConnectionString += "Initial Catalog=" + Database;
  15.  
  16. #endregion
  17.  
  18.  
  19. #region Try to establish a connection to the database
  20.  
  21. SqlConnection SQLConnection = new SqlConnection();
  22.  
  23. try
  24. {
  25.     SQLConnection.ConnectionString = ConnectionString;
  26.     SQLConnection.Open();
  27.  
  28.     // You can get the server version
  29.     // SQLConnection.ServerVersion
  30. }
  31. catch (Exception Ex)
  32. {
  33.     // Try to close the connection
  34.     if (SQLConnection != null)
  35.         SQLConnection.Dispose();
  36.  
  37.     // Create a (useful) error message
  38.     string ErrorMessage = "A error occurred while trying to connect to the server.";
  39.     ErrorMessage += Environment.NewLine;
  40.     ErrorMessage += Environment.NewLine;
  41.     ErrorMessage += Ex.Message;
  42.  
  43.     // Show error message (this = the parent Form object)
  44.     MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  45.  
  46.     // Stop here
  47.     return;
  48. }
  49.  
  50. #endregion
  51.  
  52.  
  53. #region Execute a SQL query
  54.  
  55. string SQLStatement = "SELECT * FROM ExampleTable";
  56.  
  57. // Create a SqlDataAdapter to get the results as DataTable
  58. SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLStatement, SQLConnection);
  59.  
  60. // Create a new DataTable
  61. DataTable dtResult = new DataTable();
  62.  
  63. // Fill the DataTable with the result of the SQL statement
  64. SQLDataAdapter.Fill(dtResult);
  65.  
  66. // Loop through all entries
  67. foreach (DataRow drRow in dtResult.Rows)
  68. {
  69.     // Show a message box with the content of
  70.     // the "Name" column
  71.     MessageBox.Show(drRow["Name"].ToString());
  72. }
  73.  
  74. // We don't need the data adapter any more
  75. SQLDataAdapter.Dispose();
  76.  
  77. #endregion
  78.  
  79.  
  80. #region Close the database link
  81.  
  82. SQLConnection.Close();
  83. SQLConnection.Dispose();
  84.  
  85. #endregion
  86.  

Offline RLondon

  • Randy London
  • Charter Member
  • Full Member
  • *
  • Posts: 167
  • Field: -
  • Real Name: Randy P. L. London
  • Favorite OS: Windows
  • Programming Language: -
Re: PHPMyAdmin & C#
« Reply #1 on: November 17, 2010, 02:27:07 AM »
Well my guess would be that C# is meant to work with MSSQL, not MySQL (I'm assuming this is what your using, since PMA is a web-based front-end for MySQL).

I have only needed to query my MySQL database via C# once before, and I did it by first writing an "API" in php that resided on a webserver with access to the database server. This API would do the queries then return them in HTTP just like a webpage - then the C# application would just do a web request to that API page. It worked well for my purposes, HOWEVER, it was unsecured (if this data you want to pull is in any way sensitive, you will want to somehow encode this data before sending it).


That's my only suggestion as of now - I never really tried to connect directly to a MySQL database with C# before, so IDK how to do it... but I'm sure there is some way of doing it, you just have to find it! ;)

BTW - what kind of errors/issues did you get when trying to run/compile that code? :)

Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: PHPMyAdmin & C#
« Reply #2 on: November 17, 2010, 05:19:42 AM »
Yeah the database will contain user and pass information. Is there another way to go about this?
I was thinking about making it a database application, which would be fine if I could somehow add/edit/remove user/info from the database without changing it on all computers that the program is installed on. So is there a way to make the database sync up with a MySQL server?

Error:
Code: [Select]
A error occurred while trying to connect to the 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. 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)

Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: PHPMyAdmin & C#
« Reply #3 on: November 28, 2010, 08:30:56 PM »
Ok, well I pretty much scrapped the entire C# code. I am going with a different approach. Just like you said, I now have 2 webpages. One webpage will return the information I request and the other will update it. Anyways I am having trouble with encrypting it. The md5()  functions are returning different hashes. I am trying to check if the hashes are the same(kinda like if you are checking a login) but the PHP md5() and my C# md5() hashes are not the same.

Ok lets say I want to create a md5 hash of "password" (without the quotes)
PHP MD5: 696d29e0940a4957748fe3fc9efd22a3
 C# MD5: 5f4dcc3b5aa765d61d8327deb882cf99

Here is my C# code:
Code: C# [Select]
  1. public String MD5(String input)
  2. {
  3.     String result = "";
  4.  
  5.     System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  6.     byte[] bytes = Encoding.Default.GetBytes(input);
  7.     byte[] byteArray = md5.ComputeHash(bytes);
  8.     foreach (byte b in byteArray)
  9.         result += b.ToString("x2");
  10.  
  11.     return result;
  12. }

Once I can get the same MD5 hashes, then I should be able to encrypt all the sensitive data.

Thanks
« Last Edit: November 28, 2010, 08:34:03 PM by gbertoli3 »

Offline AJ32

  • TCC Webmaster
  • Administrator
  • Cube Head
  • *
  • Posts: 1,403
  • "TCC Fuhrer"
    • Fox Software
  • Field: Software & Web Development
  • Real Name: Andrew Warner
  • Favorite OS: Windows
  • Programming Language: PHP
Re: PHPMyAdmin & C#
« Reply #4 on: November 28, 2010, 11:17:10 PM »
My guess here is that PHP is encrypting the data for it's own encoding (UTF-8/7, ASCII, etc.) and C# is using something else.
Here's a page I found that has an example of this: MD5 in C# - works like php md5()....

Now, the part that I see different between your's and thier code is this:
Code: C# [Select]
  1.  foreach (byte a in hash) {
  2.          if (a<16)
  3.             ret += "0" + a.ToString ("x");
  4.          else
  5.             ret += a.ToString ("x");
  6.       }
  7.  

To be honest, I'm not sure what's going on here - but my guess is it has something to do with encoding...

If it works for you, great! But otherwise I would try to do some research on the different encoding systems between .NET and PHP. :)
Post your results!


P.S. Also, I've heard that MD5 is no longer considered secure, and I believe the "replacement" is SHA-256. So you may want to take that into consideration. :-/

"Data is eternal, immortal, immutable. Even when the engineers and intelligence
behind its creation are gone, information, facts, and knowledge never die."

Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: PHPMyAdmin & C#
« Reply #5 on: November 28, 2010, 11:39:47 PM »
Thanks, but it doesn't work. I'll do a little more research on it. Based on 1 or two of the comments people have gotten that to work, but sadly it doesn't work for me. Anyways I will look into SHA-256, but I may just stick with MD5 because the data isn't extremely sensitive, but you can never be too secure.