C# 如何只连接一次数据库,然后执行3次查询SQL语句,然后分别把查询结果取到缓冲区中保存

27 篇文章 3 订阅

我有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();
                }
            }
        }
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值