C#数据库连接错误 [英] C# Database Connection Error
问题描述
嘿伙计们,
我是visual c#的新手并尝试将我的应用程序与我的数据库连接。我已经使用PHPMyAdmin安装了本地Zend服务器。我的PHP版本是5.3.9,MySqlConnector已成功添加到我的项目中。在执行下面的代码后,我得到以下错误消息:
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
英文:
连接尝试失败,因为连接方在一段时间后未正确响应,或已建立的连接失败,因为已连接的主机未能响应
我的代码:
// 具有服务器,数据库,用户和密码的连接字符串
string connection_string = SERVER = 127.0.0.1; DATABASE = test; UID = *********; PASSWORD = ********; PORT = 3306\" 跨度>;
// 设置连接配置
MySqlConnection connection = new MySqlConnection(connection_string);
尝试
{
// 打开连接
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\\\
+ reader [ 文本跨度>]的ToString());
}
reader.Close();
connection.Close();
}
catch (例外情况)
{
textbox1.AppendText(ex.Message);
}
SQL- Querys在cmd中工作正常,这个php脚本运行没有任何错误:
<?php $ con = mysql_connect( localhost, *******, *******跨度>);
if (!$ con)
{
die(' 无法连接:' .mysql_error());
}
mysql_select_db( test,$ con) ;
$ result = mysql_query( SELECT * FROM personen);
$ row = mysql_fetch_array($ result);
echo <预> 中跨度>;
var_dump($ row);
mysql_close($ con); ?>
您对解决我的问题有什么建议吗?
感谢您的帮助!:)
con = mysql_connect( localhost, *** ****, *******);
if (!
con)
{
die(' 无法连接:' .mysql_error());
}
mysql_select_db( test,
CON);
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:
//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 $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!:)
con = mysql_connect("localhost","*******","*******"); if (!
con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test",
con);
这篇关于C#数据库连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!