在数据库上使用Windows窗体进行搜索 [英] Searching using a windows form on the database

查看:85
本文介绍了在数据库上使用Windows窗体进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一段Windows窗体上的代码.我需要使用从MySQL数据库在2个不同的文本框中输入的2个值进行搜索(我正在使用Name和User-id进行搜索,如果它们存在于数据库中,并且未显示正确的错误消息).有人可以举例说明如何做到这一点.

谢谢,
Tushar.

Hi,

I have a piece of code which is on a Windows Form. I need to Search using 2 values entered in 2 different Textboxes from a MySQL database (i am searching using Name and User-id if they exist in the database and if not display proper error messages). Can someone please explain how this can be done with an example.

Thanks,
Tushar.

推荐答案

尝试:
SELECT * FROM myTable WHERE name='Name' AND userId='UserId'

只需使用参数化查询即可:

Just do it with a parametrized query:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT iD, description FROM myTable WHERE userName=@UN AND userId=@UI", con))
        {
        cmd.Parameters.AddWithValue("@UN", myUserNameTextBox.Text);
        cmd.Parameters.AddWithValue("@UI", myUserIdTextBox.Text);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }


这篇关于在数据库上使用Windows窗体进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆