如何在文本框中显示搜索结果?

时间:2014-07-04 07:21:04

标签: c# oledb

我创建了一个表格代码,想要在TextBox IMEI中搜索和检索设备IMEI的答案,当我点击搜索时,我想在我要显示的文本框代码中显示结果code_mck结果:

id  Imei             code_mck
1  356885021519453   830782136
2  356885021519156   948790617
3  356885021518893   715398945
4  356885021518935   567456626
5  359654022104377   557960750 

最初我有一个典型的三层架构,搜索方法在数据层中。我的SQL查询遇到了一些问题:

public DataSet recherche(string code)
{
    DataSet ds = null;
    using (OleDbConnection cnn = new OleDbConnection(strConn))
    {
        cnn.Open();
        string Oledb = "SELECT * FROM Code WHERE Imei=@IMEI";
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(Oledb, cnn))
        {
            adapter.SelectCommand.Parameters.AddWithValue("@IMEI", code);
            ds = new DataSet();
            adapter.Fill(ds, "Code");
        }
    }
    return ds;
}

业务逻辑层:

namespace unlock2_buisness
{
    public  class code_imei

    public DataSet rechercheduCode(string imei)
    {
        unlockDAL objetDataLayer = new unlockDAL();

        if (imei == "")
            throw new Exception("merci d'indique l'imei de recherche");
        DataSet dt = null;
        dt = objetDataLayer.recherche(imei);
        return dt;
}

在我的用户层中,专门用于搜索以在文本框代码中显示结果的TextBox没有响应,并且没有通过提供设备IMEI来获取相应的代码,因为它应该来自表格。

private void btnrechercheimei_Click(object sender, EventArgs e)
{
    imeiLogic.rechercheduCode(txtimei.Text);
    imeiLogic.rechercheduCode(txtcode.Text);
}

我将非常感谢您的支持。

1 个答案:

答案 0 :(得分:0)

问题在于您如何使用BusinesLogic类。

public DataSet rechercheduCode(string imei)接受一个字符串作为输入参数并返回一个DataSet。

当你调用这样的方法时:

imeiLogic.rechercheduCode(txtimei.Text);

您确实提供了输入参数(文本框中的值),但您没有将返回的数据集存储在变量中。

数据集将有一个名为 Code 的表,它将保存查询中的结果行,如果没有找到,则保存0行。

尽可能接近当前的实施情况我建议:

private void btnrechercheimei_Click(object sender, EventArgs e)
{
    // call the BLL to get a dataset
    var dataSet = imeiLogic.rechercheduCode(txtimei.Text);
    // get our table in the dataset
    var codeTable = dataSet.Tables["Code"]; // table
    // check if there was a row returned
    if (codeTable.Rows.Count > 0)
    {
        var row = codeTable.Rows[0];  // pick the first one
        txtcode.Text = row["code_mck"].ToString();  // update our textbox 
    }    
    // Add handling for 0 rows and/or more than 1 rows returned
}