C# Using 2 sqldatareader at same time?

后端 未结 5 1212
醉酒成梦
醉酒成梦 2021-01-06 08:19

Hello is there a way to use two sqldatareader at same time ?

i was coding a program

and i got error because i use 2 readers at same

Sample of code

相关标签:
5条回答
  • 2021-01-06 08:34

    It is possible when you use something called Multiple Active Result Sets, or MARS. Check out this helpful article that details all possible pitfalls.

    0 讨论(0)
  • 2021-01-06 08:42

    1.Just add MultipleActiveResultSets=True into your connection string:

    private string _ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=yourdbpath\Database.mdf;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=30";
    
    0 讨论(0)
  • 2021-01-06 08:46

    Declare those DataReader in the block public partial class Form1 : Form Example:

    namespace GoodFood_1_
    {
        public partial class Autentificare_client : Form
       {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\edi_b\Documents\visual studio 2013\Projects\GoodFood(1)\GoodFood(1)\GOOD_FOOD.mdf;
            Integrated Security=True;MultipleActiveResultSets=True");
            SqlConnection con2 = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\edi_b\documents\visual studio 2013\Projects\GoodFood(1)\GoodFood(1)\GOOD_FOOD.mdf;
            Integrated Security=True;MultipleActiveResultSets=True");
    
            SqlDataReader dr;
            SqlDataReader cc;
    
            public Autentificare_client()
            {
                InitializeComponent();
            }
    
    0 讨论(0)
  • 2021-01-06 08:52

    The error message is misleading. You must have MultipleActiveResultSets=True set in your connection string to be able to send two seperate commands with two different readers.

    0 讨论(0)
  • 2021-01-06 08:58

    You'll have to enable Multiple Active Recordsets (MARS) on your connection.

    http://msdn.microsoft.com/en-us/library/h32h3abf%28v=vs.80%29.aspx

    0 讨论(0)
提交回复
热议问题