ado| Error
Figure 1
Figure 2
The program shown in charts 1 and 2 shows how to get the two result sets returned by the data command and displays the contents of all the fields of all the data records for the two result sets in the TextBox control on the form.
This example assigns two SELECT expressions to the CommandText property of the SqlCommand object, so the two result sets are returned after the data command is executed using the ExecuteReader method. We process each result set sequentially by calling the NextResult method of the SqlDataReader object, while processing the individual result set, calling the SqlDataReader object's GetName method to get the field name (that is, Myreader.getname (i)), and the position order is passed to the SqlDataReader object to obtain the data content (i.e. Myreader[i]) of the field.
Specifically, use-expression is used to execute data commands, produce SqlDataReader objects, and return multiple data result sets, if paired with do. While loop to get the name of each field in the result set and the contents of the field, an error message "Attempt to read invalid when no data is currently available" appears.
How can this problem happen? The problem is in do ... While loop body. Do ... The while loop is characterized by at least one execution of the program code block within {}, regardless of whether the result of the specified judgment expression is True or False. In other words, if you execute the myreader.getname (i) function to get the data before the Myreader.read () function is executed, an error occurs, of course.
To solve this problem, please put the original do ... While loops are rewritten as while loops, the myreader.read () can be used as a determinant. The related program code is written in the Load event processing routine of the form, as shown below:
private void Ch6_demoform010_load (object sender, EventArgs e)
{
...
Try
{
Establish a connection.
using (SqlConnection con = new
SqlConnection (Connectstringbuilder.connectionstring))
{
Creates a data command object (i.e. SqlCommand object).
SqlCommand foxcmd = new SqlCommand ();
Foxcmd.connection = con;
Assigns two select expressions to the CommandText property.
This will allow the data command to return two result sets.
Foxcmd.commandtext = "SELECT * from Zhang Limin Laboratory; SELECT * FROM Customer ";
Open the connection.
Con. Open ();
int resultsetcounter = 1;
StringBuilder s = new StringBuilder ();
Executes the data command that returns multiple result sets.
using (SqlDataReader myreader = Foxcmd.executereader ())
{
bool Fnextresult = true;
Sequential processing of individual result sets.
Todo
{
...
Switch (resultsetcounter)
{
Case 1:
S.appendline (data record of the "Zhang Limin Laboratory");
Break
Case 2:
S.appendline (data record for "customer" datasheet);
Break
}
...
Gets the contents of the names and fields of the fields for all data records of the result set.
if (myreader.hasrows)
{
while (Myreader.read ())
{
for (int i = 0; i < Myreader.fieldcount; i++)
{
S.appendline (Myreader.getname (i) + ":" + myreader[i]. ToString ());
}
...
}
}
Moves the data reader forward to the next result set.
Fnextresult = Myreader.nextresult ();
Resultsetcounter + 1;
}
while (Fnextresult);
Assigns the obtained data to the Text property of the TextBox control.
Txtinfo.text = S.tostring ();
}
}
}
...
}
Zhang Limin, the lab.
Look forward to more exciting, please pay attention to: