SqlConnection conn = new SqlConnection("Data Source=myServer;Integrated Security=True;"); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT Name FROM Customers", conn); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { TdsParserStateObject stateObj = rdr.GetStateObject(); string result; if (stateObj.TryReadString(out result)) { Console.WriteLine(result); } }
SqlConnection conn = new SqlConnection("Data Source=myServer;Integrated Security=True;"); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { TdsParserStateObject stateObj = rdr.GetStateObject(); if (stateObj.IsEndOfMessage()) { // No more data to read break; } string customerName; if (stateObj.TryReadString(out customerName)) { Console.WriteLine(customerName); } }In this example, we execute a SQL query that retrieves all columns from the Customers table. We loop through the data returned by the query, retrieve the TdsParserStateObject for each row, and check if there is more data to read using the IsEndOfMessage method. If there is no more data to read, we break out of the loop. Otherwise, we use the TryReadString method to read the customer's name from the TDS parser's buffer and write it to the console. Package library: System.Data.SqlClient is part of the .NET Framework Class Library and is included in the System.Data.SqlClient.dll assembly.