【问题标题】:MySQL ConnectionString " No Database Selected"MySQL ConnectionString“未选择数据库”
【发布时间】:2015-06-13 05:12:49
【问题描述】:

我目前正在尝试用 c# 做一个 mysql 工具,用于从我的数据库中导出数据。

我对 mysql.Connectionstring 进行了硬编码:

private MySqlConnection _conn; mycon = "server=127.0.0.1;uid=tools;pwd=tools;database=t_data;"; _conn.ConnectionString = mycon;

这完全没有问题。但正如你所知,你希望它是可编辑的。所以我在我的表单中创建了 3 个文本框。添加了一个类。

当我点击导出时,我将文本框值分配给我的班级

        StringExporter.user.ip = textBox1.Text;
        StringExporter.user.usr = textBox2.Text;
        StringExporter.user.pwd = textBox3.Text;
        StringExporter.user.db = textBox4.Text;

这是我的课

public static class user
{
    public static string ip;
    public static string usr;
    public static string pwd;
    public static string db;

}

但是每次我想导出时,我都会收到一个 Mysql 错误,即没有选择数据库,我不明白为什么。我输出了所有的字符串,它们都很好。

如果有人可以帮助我,那就太好了:)

【问题讨论】:

  • 您能否发布您用于导出数据的整个代码?

标签: c# mysql connection-string


【解决方案1】:

如果我理解正确,您想重新创建连接字符串。如果是这样,那应该很简单。更新连接字符串,然后执行插入、更新、选择或删除

try{
    mycon = "server="+user.ip+";uid="+user.usr+";pwd="+user.pwd+";database="+user.db+";"
    _conn.ConnectionString = mycon;
    _conn.Open();
    //Do your stuff
    _conn.Close();
} catch(Exception e){ MessageBox.Show("Failed due to :" + e.Message); }

【讨论】:

  • 是的,这就是我想要做的。我已经做了你提到的事情:mycon2 = "server=" + StringExporter.user.ip + ";uid=" + StringExporter.user.usr + ";pwd=" + StringExporter.user.pwd + ";database= " + StringExporter.user.db + ";";但我收到错误“未选择数据库”//////////////我也无法仅使用 StringExporter.user.ip 访问带有 user.ip 的字符串
  • 好的。 “未选择数据库”是我假设的自定义错误消息。请粘贴您面临的异常错误消息...
  • 好的。请截取并分享一个没有错误弹出窗口的屏幕截图。所以,我会检查你的代码。
猜你喜欢
  • 2017-11-03
  • 2012-12-06
  • 2013-11-24
  • 2012-01-20
  • 2014-05-14
  • 2010-10-25
  • 2014-03-25
  • 2011-02-16
相关资源
最近更新 更多