如何以一种形式处理多个sql数据读取器 [英] How do I handle multiple sql data reader in one form

查看:134
本文介绍了如何以一种形式处理多个sql数据读取器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用2个不同的组合框绑定数据,因为我两次使用sql数据读取器两次从2个不同的表中绑定数据,但是显示错误

I want to bind data with 2 different combo box for that i had use sql data reader twice time for bind data from 2 different table but it shows an error

SqlConnection conn = new SqlConnection("Data Source=HP\\SASI;Initial Catalog = test;Integrated Security=SSPI;");
        conn.Open();
        SqlCommand cmd = new SqlCommand();
        string query = "Select fn+''+ln from user_details where category='Doctor'";// position column from position table
        cmd.Connection = conn;
        cmd.CommandText = query;

        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string myItem = dr[0].ToString();
            comboBox1.Items.Add(myItem);
        }
        conn.Close();

      SqlConnection conn1 = new SqlConnection("Data Source=HP\\SASI;Initial Catalog = test;Integrated Security=SSPI;");
        conn1.Open();
        SqlCommand cmd1 = new SqlCommand("select firstname+''+lastname from patient_table",conn1);
        SqlDataReader dr1 = cmd1.ExecuteReader();
        while (dr1.Read())
        {
            string name = dr[0].ToString();
            comboBox1.Items.Add(name);
        }

推荐答案

非常简单,但我建议您阅读此内容.它将帮助您很多.对于此特定问题,请参阅此处的关闭DataReader"部分.

使用DataReader检索数据 [
Its very simple but i recommend you to read this. It will help you alot. For this particular issue, refer Closing the DataReader section there.

Retrieving Data Using a DataReader[^]


这篇关于如何以一种形式处理多个sql数据读取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆