C# WPF mysql 字符串

标签 c# mysql wpf string

我正在创建一个连接到本地主机数据库的 wpf 应用程序,它有 2 个表,现在我遇到了错误,但我不确定代码中出了什么问题。有人可以帮忙吗?

我收到此错误:

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'left join author on book.author_id=author.id' at line 1

private void Filter_TextChanged(object sender, TextChangedEventArgs e)
    {
        connection.Open();
        MySqlCommand cmd = connection.CreateCommand();  

        cmd.CommandText = "SELECT * FROM book where book.name like ('" + Filter.Text + "%') left join author on book.author_id=author.id";   

        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        da.Fill(dt);
        _dataView = new System.Data.DataView(dt);
        _dataView.Sort = "name ASC,id ASC";
        BooksGrid.DataContext = dt;
        connection.Close();
    }

最佳答案

将您的查询更改为

cmd.CommandText = "SELECT * FROM book left join author on book.author_id=author.id where book.name like ('" + Filter.Text + "%') and book.author_id=author.id"; 

末尾附加的“book.author_id=author.id”子句是为了确保您只获取与author_id匹配的记录。

此外,您应该尝试使用 cmd.ExecuteReader(),而不是 cmd.ExecuteNonQuery(),因为您正在检索行。

关于C# WPF mysql 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399061/

相关文章:

php - codeigniter 3 get_where sql查询函数

java - Java MySQL executeUpdate() 为 INSERT ON ON DUPLICATE KEY UPDATE 返回什么?

php - 如何在 laravel 代码中嵌入视频

.net - WPF 中的窗口所有者没有始终位于顶部的行为

c# - 如何更改文本框背景颜色?

c# - BitmapCache 导致 WPF 应用程序锁定

c# - 棱镜 WPF : How to moq the callback to return IDialogResult. 结果 == Buttonresult.ok

c# - 如何断言两个列表包含 NUnit 中具有相同公共(public)属性的元素?

c# - 如何在 Asp.Net 网站中包含另一个项目控制台应用程序 exe?

c# - 使用 web.config 使用查询字符串重定向不存在的页面