我有1个数据库共有30个字段,字段名为id,字段2,字段3,... 字段30,我想只连接一次数据库,然后在数据库中依次查询以下三种符合条件的记录,查找到后取出该记录的字段2,字段3,... 字段30的数值。
1、查找 id = “xxxxxx01" 的记录,将字段2,字段3,... 字段30的数值依次取出来。
2、查找 id = “xxxxxx02" 的记录,将字段2,字段3,... 字段30的数值依次取出来。
3、查找 id = “xxxxxx04" 的记录,将字段2,字段3,... 字段30的数值依次取出来。
timing、noack_time和sleep_mode为数据库字段名
private void button1_Click(object sender, EventArgs e)
{
int[] array1 = new int[3];
int[] array2 = new int[3];
string conStr = "server=localhost;port=3306;user Id=root;password=331; database=tixx ;Allow User Variables=True"; //连接字符串
MySqlConnection conn = null;
try
{
conn = new MySqlConnection(conStr);
conn.Open();
string sql1 = "select * from table1 where id = '" + "FC-12-5A-6B-7B" + "'";
MySqlDataAdapter sda1 = new MySqlDataAdapter(sql1, conn);
DataTable dt1= new DataTable();
//使用 SqlDataAdapter 对象 sda 将查询结果填充到 DataSet 对象 dt 中
sda1.Fill(dt1);
// int count = dt1.Rows.Count;
array1[0] = Convert.ToInt32(dt1.Rows[0]["timing"].ToString());
array1[1] = Convert.ToInt32(dt1.Rows[0]["noack_time"].ToString());
array1[2] = Convert.ToInt32(dt1.Rows[0]["sleep_mode"].ToString());
string sql2 = "select * from table1 where id = '" + "FC-12-5A-6B-7C" + "'";
MySqlDataAdapter sda2 = new MySqlDataAdapter(sql2, conn);
DataTable dt2 = new DataTable();
//使用 SqlDataAdapter 对象 sda 将查询结果填充到 DataSet 对象 dt 中
sda2.Fill(dt2);
// int count2 = dt2.Rows.Count;
array2[0] = Convert.ToInt32(dt2.Rows[0]["timing"].ToString());
array2[1] = Convert.ToInt32(dt2.Rows[0]["noack_time"].ToString());
array2[2] = Convert.ToInt32(dt2.Rows[0]["sleep_mode"].ToString());
}
catch (Exception ex)
{
MessageBox.Show("查询失败!" + ex.Message);
}
finally
{
if (conn != null)
{
//关闭数据库连接
conn.Close();
}
}
}
}