我想检查我的随机生成的数字是否与我的数据表中的[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);
}
}
听起来您只需要查询数据库中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