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

C# 从数据集中的数据与用户输入匹配的数据集中选择,c#,C#,我正在尝试使用数据表根据数据集查询或匹配用户输入: 我从一个存储过程填充数据集,该存储过程只从一个表中选择一个列:例如:UserID column**我没有选择表的全部内容* public static DataSet LoadProfile() { SqlCommand cmdSQL = new SqlCommand("usp_LoadProfile", ConnectDatabase); cmdSQL.CommandType = CommandType.StoredProce

我正在尝试使用
数据表
根据
数据集
查询或匹配用户输入:

我从一个存储过程填充数据集,该存储过程只从一个表中选择一个列:例如:UserID column**我没有选择表的全部内容*

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;
}
我在form load事件中调用了以下方法:我需要在from load上填充数据集

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

    return tblExample;
}
最后,我要做的是匹配
数据表中的用户条目

我有一个按钮事件:

DataRow[] results;
results = LoadData().Select(txtExample.Text);
除此之外,我可以使用for循环,但每个人只有一条记录


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

最后一行应该是

DataRow[] results;
results = LoadData().Select("UserID = '" + txtExample.Text +"'");
假设UserID是文本类型的字段。如果不是数字类型,则删除引号

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

但是我应该指出,
LoadProfile
中的代码位于
daSQL.Fill(ds)之后调用,您可以删除它(只需返回数据集)

最后一行应该是

DataRow[] results;
results = LoadData().Select("UserID = '" + txtExample.Text +"'");
假设UserID是文本类型的字段。如果不是数字类型,则删除引号

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

但是我应该指出,
LoadProfile
中的代码位于
daSQL.Fill(ds)之后调用,您可以删除它(只需返回数据集)

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

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

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

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

我得到这个错误:对象引用未设置为对象的实例。您需要调试LoadProfile中的代码,并检查它是否有效地返回一个填充的数据集,其中有一个名为“Example”的表(提示,对数据集中的第一个表使用索引0),明白了吗!我只是在表索引中使用了零,在末尾使用了for循环。感谢您提供的有用提示。我得到了以下错误:对象引用未设置为对象的实例。您需要调试LoadProfile中的代码,并检查它是否有效地返回一个填充的数据集,其中有一个名为“Example”的表(提示,使用数据集中第一个表的索引0)明白了!我只是在表索引中使用了零,在末尾使用了for循环。谢谢你的提示。