I'm using a datareader for the first time. I'm using an example that I found online to export values to an excel spreadsheet. It seems to be skipping a row. If it is supposed to return 1 row, it is empty. If it is supposed to return 2 rows, it skips the first row and only displays the second row.
This is the call to the database:
//Get the sql string
string SqlString = "SELECT * FROM MASTER_NODE";
//Create a new command object
SqlCommand sc = new SqlCommand();
//Set the connection
sc.Connection = new SqlConnection("...");
//Open the connection
sc.Connection.Open();
//Set the command type
sc.CommandType = CommandType.Text;
//Set the SQL String
sc.CommandText = SqlString;
//Execute the SQL Statement/Procedure
SqlDataReader RS = sc.ExecuteReader();
This is where the data is output:
MemoryStream ReturnStream = new MemoryStream();
try
{
//make the update to the table changing assessors
SqlDataReader RS = ExecuteReaderCommand(CmdGetActiveCurrent);
//Create a streamwriter to write to the memory stream
StreamWriter sw = new StreamWriter(ReturnStream);
if (RS.HasRows)
{
//Create a header for the file from the names of the
//columns that we are retrieving
RS.Read();
string tempstring = "";
for (int counter = 0; counter < RS.FieldCount;counter++)
{
tempstring += RS.IsDBNull(counter)?"":RS.GetName(counter) + "\t";
}
//Write the row of data to the Memory Stream.
sw.WriteLine(tempstring);
//Loop through the data row and write the contents to the memory
//stream.
while (RS.Read())
{
//Put each columns' data value into a string separated by a tab.
tempstring = "";
for (int counter = 0; counter < RS.FieldCount;counter++)
{
tempstring += RS.IsDBNull(counter)?"":RS.GetValue(counter).ToString() + "\t";
}
//Write the row of data to the Memory Stream.
sw.WriteLine(tempstring);
}
}
//Clean up the stream writer
sw.Flush();
sw.Close();
//Clean up the data reader
RS.Close();
If anyone has any ideas, it would be appreciated.
This is the call to the database:
//Get the sql string
string SqlString = "SELECT * FROM MASTER_NODE";
//Create a new command object
SqlCommand sc = new SqlCommand();
//Set the connection
sc.Connection = new SqlConnection("...");
//Open the connection
sc.Connection.Open();
//Set the command type
sc.CommandType = CommandType.Text;
//Set the SQL String
sc.CommandText = SqlString;
//Execute the SQL Statement/Procedure
SqlDataReader RS = sc.ExecuteReader();
This is where the data is output:
MemoryStream ReturnStream = new MemoryStream();
try
{
//make the update to the table changing assessors
SqlDataReader RS = ExecuteReaderCommand(CmdGetActiveCurrent);
//Create a streamwriter to write to the memory stream
StreamWriter sw = new StreamWriter(ReturnStream);
if (RS.HasRows)
{
//Create a header for the file from the names of the
//columns that we are retrieving
RS.Read();
string tempstring = "";
for (int counter = 0; counter < RS.FieldCount;counter++)
{
tempstring += RS.IsDBNull(counter)?"":RS.GetName(counter) + "\t";
}
//Write the row of data to the Memory Stream.
sw.WriteLine(tempstring);
//Loop through the data row and write the contents to the memory
//stream.
while (RS.Read())
{
//Put each columns' data value into a string separated by a tab.
tempstring = "";
for (int counter = 0; counter < RS.FieldCount;counter++)
{
tempstring += RS.IsDBNull(counter)?"":RS.GetValue(counter).ToString() + "\t";
}
//Write the row of data to the Memory Stream.
sw.WriteLine(tempstring);
}
}
//Clean up the stream writer
sw.Flush();
sw.Close();
//Clean up the data reader
RS.Close();
If anyone has any ideas, it would be appreciated.