我使用ASP.NET Query MYSQL数据库
但他没有表现出来。只有"等待localhost'我等了,但没有改变
但是形式相同的代码是可行的
--- ASP.NET
protected void Button3_Click(object sender, EventArgs e)
{
string connStr = "server=localhost;port=3306;uid=root;pwd=123123;database=testdb;";
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
try
{
conn.Open();
String cmdText = "SELECT NAME FROM playdata WHERE SID = '001'";
command = new MySqlCommand(cmdText, conn);
SL1.Text = (string)command.ExecuteScalar();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
switch (ex.Number)
{
case 0:
SL1.Text = "Can not connect";
break;
case 1045:
SL1.Text = "Input error";
break;
}
}
Console.ReadLine();
conn.Close();
}
我不知道如何解决这个问题,而在ASP.NET中,插入数据是成功的 所以我不知道哪里错了
---插入数据
string connStr = "server=localhost;port=3306;uid=root;pwd=123123;database=testdb;";
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
try
{
conn.Open();
command.CommandText = "INSERT INTO `testdb`.`playdata` (`SID`, `NAME`,`PWD`,`EMAIL`) VALUES('S01', 'MYNAME','RRR01','E4913')"; //重點
command.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
switch (ex.Number)
{
case 0:
SL1.Text ="Connection fail";
break;
case 1045:
SL1.Text ="pwd error.";
break;
}
}
Console.ReadLine();
conn.Close();
*编辑
我使用视觉工作室社区2015
我试过了
127.0.0.1
本地主机
String cmdText ="从playdata中选择名称WHERE SID =' 001'&#34 ;;
String cmdText ="从testdb
选择名称。playdata
WHERE SID =' 001' &#34 ;;
String cmdText ="从playdata中选择名称WHERE SID =' 001'";
答案 0 :(得分:3)
将查询更新为:
String cmdText = "SELECT NAME FROM playdata WHERE SID = 'S01'";
您插入了SID' S01'并且之前正在查询SID' 001'
答案 1 :(得分:1)
将您的查询更新为:
"SELECT NAME FROM `testdb`.`playdata` WHERE SID = '001'";
我在php上遇到了撇号问题。这可能是同一个问题。
答案 2 :(得分:0)
string connStr = "server=127.0.0.1;port=3306;uid=root;pwd=123123;database=testdb;";
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
String cmdText = "SELECT NAME FROM playdata WHERE SID = 'S01'";
command = new MySqlCommand(cmdText, conn);
conn.Open();
try {
string str = command.ExecuteScalar().ToString();
SL1.Text = str;
} catch {
SL1.Text = "error";
}
问题的原因来自" Console.ReadLine();"
谢谢大家帮助我!