c# – 尝试查询DB时收到错误“未选择数据库”

我是职业高中的学生,我还是编程的新手,我有一个使用c#创建应用程序的任务,我在连接3个表以显示datagridview时遇到问题.

我已经尝试了mySql上的查询它工作正常,但是当我在我的c#代码行中应用它时它没有工作它显示“没有选择数据库”,有人可以帮我这个,这是我的完整代码

string constring = "datasource=localhost;port=3306;username=root;password=root";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand("select book_detail.id_bookdetail, location.location_id, location.location_name, book.book_id, book.title from location inner join book_detail on location.location_id = book_detail.location_id inner join book on book_detail.book_id = book.book_id; ", conDataBase);
        try
        {
            MySqlDataAdapter sda = new MySqlDataAdapter();
            sda.SelectCommand = cmdDataBase;
            DataTable dbdataset = new DataTable();
            sda.Fill(dbdataset);
            BindingSource bSource = new BindingSource();

            bSource.DataSource = dbdataset;
            transfer_view.DataSource = bSource;
            sda.Update(dbdataset);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

最佳答案 您的连接字符串应指定数据库名称:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
                       ^^^^^^^^^^^^^^^^^^^

(端口3306是默认的MySql端口)

参考. MySQL connection strings

点赞