从c#.net中的sql数据库中搜索查询 [英] search query from sql database in c#.net

查看:180
本文介绍了从c#.net中的sql数据库中搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在制作一个项目,并且在从数据库中搜索时遇到了问题。我在名称的基础上搜索,这是一个'组合框',有两个'文本框'用于颜色和价格,一个'数字向下'用于从数据库中检索的数量。

所以请告诉我如何在try()中编写代码。



问候

sakshi


i m making a project and is having problem in searching from the database. I m searching on the basis of "name" which is a 'combobox' and there are two 'textboxes' for color and price and one 'numericupdown' for quantity which is to be retrieved from database.
So please tell me how to write code in try().

regards
sakshi

推荐答案

我们不能明确,因为我们不知道你的系统是如何工作的,但这是一般的想法:

We can't be explicit, because we have no idea how your system works, but this is the general idea:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT iD, description FROM myTable WHERE Name LIKE '%' + @NM + '%'", con))
        {
        cmd.Parameters.AddWithValue("@NM", myNameTextBox.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);
                }
            }
        }
    }

显然,您需要修改WHERE子句以适应您的情况。

Clearly, you will need to modify the WHERE clause to fit your circumstances at the minimum.


这篇关于从c#.net中的sql数据库中搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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