我如何检查我的电话号码在数据表中是否是有价值的ID?

问题描述 投票:-2回答:1

我想检查我的随机生成的数字是否与我的数据表中的[id]相匹配,这是必要的,以便我可以通过id从表中获取随机值。我想到的是:

            Random rnd = new Random();
            int card = rnd.Next(1,21);
            label8.Text = Convert.ToString(card);

            try
            {
                while (await sqlReader.ReadAsync())
                {
                    string find = "item_manuf_id = 'some value'";
                    DataRow[] foundRows = table.Select(find);
                }
            }
c# winforms datatables mdf
1个回答
0
投票

听起来您只需要查询数据库中field matches the值的某个表中的任何记录:

bool isValidId = false;
string sql = "SELECT [item_manuf_id] from [tableName] where [item_manuf_id] = @id";

using (SqlConnection connection = new SqlConnection(/* connection info */))
using (SqlCommand command = new SqlCommand(sql, connection))
{
    var idParam = new SqlParameter("id", SqlDbType.Int);
    idParam.Value = card;
    command.Parameters.Add(idParam);
    var results = command.ExecuteReader();
    if (results.HasRows) isValidId = true;
}

// isValidId  will be 'true' if any records for that id were found
© www.soinside.com 2019 - 2024. All rights reserved.