从数据集中选择数据集中的数据与用户输入匹配

时间:2013-09-14 22:12:47

标签: c#

我正在尝试使用dataset查询或匹配DataTable的用户输入:

我正在从存储过程填充数据集,该存储过程仅从单个表中选择单个列:示例:UserID列。 **我没有选择表格的全部内容。*

public static DataSet LoadProfile()
{
    SqlCommand cmdSQL = new SqlCommand("usp_LoadProfile", ConnectDatabase);
    cmdSQL.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter daSQL = new SqlDataAdapter(cmdSQL);
    DataSet ds = new DataSet();
    daSQL.Fill(ds);

    try
    {
        ConnectDatabase.Open();
        cmdSQL.ExecuteNonQuery();
    }
    catch(Exception)
    {
        StatusMsg = ex.Message;
    }
    finally
    {
        ConnectDatabase.Close();
        cmdSQL.Parameters.Clear();
        cmdSQL.Dispose();
    }
    return ds;
}

我在表单加载事件中调用了以下方法:我需要在加载时填充数据集。

public static DataTable LoadData()
{
    DataSet dsView = new DataSet();
    dsView = LoadProfile();
    DataTable tblExample = dsView.Tables["Example"];

    return tblExample;
}

最后我要做的是匹配DataTable的用户条目。

我在按钮事件中有这个:

DataRow[] results;
results = LoadData().Select(txtExample.Text);

除此之外,我可以使用for循环,但每个人只有一条记录。

我试图通过数据表将用户条目与数据集匹配。

2 个答案:

答案 0 :(得分:3)

最后一行应该是

DataRow[] results;
results = LoadData().Select("UserID = '" + txtExample.Text +"'");

假设UserID是文本类型的字段。如果是数字类型,则删除引号

results = LoadData().Select("UserID = " + txtExample.Text);

但是我应该指出LoadProfile调用之后的daSQL.Fill(ds);中的代码是不需要的,你可以删除它(只是返回DataSet)

答案 1 :(得分:0)

对数据集使用以下简单查询:

DataRow[] dRow = dataSetName.Tables[0].Select("fieldToMatch = '" + userInput.ToString() + "' ");