Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

i´m new to visual c# and trying to connect my application with my database. I have installed a local Zend Server with PHPMyAdmin. My PHP version is 5.3.9 and MySqlConnector was added to my Project successful. After executing the Code below I get following Error- Message:

Ein Verbindungsversuch ist fehlgeschlagen, da die Gegenstelle nach einer bestimmten Zeitspanne nicht richtig reagiert hat, oder die hergestellte Verbindung war fehlerhaft, da der verbundene Host nicht reagiert hat

In English:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond



My Code:
C#
//Connection String with Server, Database, User and Password
           string connection_string = "SERVER=127.0.0.1;DATABASE=test;UID=*********;PASSWORD=********;PORT=3306";
           //Set Connection configs
           MySqlConnection connection = new MySqlConnection(connection_string);

           try
           {
               //Open Connection
               connection.Open();
               textbox1.AppendText("Connected Server: localhost. \nConnected Database: test");


               MySqlCommand command = connection.CreateCommand();
               command.CommandText = "SELECT * From persons";
               MySqlDataReader reader = command.ExecuteReader();
               while (reader.Read())
               {
                   textbox1.AppendText("\n\n"+reader["text"].ToString());
               }
               reader.Close();
               connection.Close();

           }
           catch (Exception ex)
           {
               textbox1.AppendText(ex.Message);

           }


SQL- Querys works fine in cmd and this php- script runs without any error to:
PHP
<?php $con = mysql_connect("localhost","*******","*******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM personen"); 

$row = mysql_fetch_array($result);


echo "<pre>";
var_dump($row);


mysql_close($con); ?>


Do you have any suggestions for solving my problem?
Thanks for your help!:)
Posted
Updated 4-Dec-12 2:41am
v2
Comments
Orcun Iyigun 4-Dec-12 8:23am    
You might want to translate that error to English :) cuz not all people know German.
sinnix 4-Dec-12 8:31am    
Sorry:)

Error: (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) (Microsoft SQL Server, Error: 10060
Zubair Alie 4-Dec-12 8:54am    
it seems that your connection string is not correct or may be the port.

1 solution

I agree with zaibshah - it's probably the port - especially as your PHP script is working fine.

This link might be useful too
Connect C# to MySQL[^]
 
Share this answer
 
Comments
sinnix 4-Dec-12 10:30am    
Thanks a lot for the replies!:)
Where i can find out the right port? The default MySql Server port is 3306 isn´t it?
I think the application can connect to the database, because this code :textbox1.AppendText("Connected Server: localhost. \nConnected Database: test"); is running and shown in my textbox.
CHill60 4-Dec-12 10:49am    
That line of code is simply adding text to a text box - it won't indicate that you're connected - even if the text box DataBindings are trying to use this connection.
I didn't think you needed to specify the port at all in the connection string - I've never done so (but I'm a bit rusty on this)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900