I wrote code to connect to the NETEZZA database. When a response is received, the first line of the result disappears, i.e. only 9 of 10 lines are displayed. I read that there are such problems with the OLEDB driver, but I could not resolve the issue myself.
I tried to add the parameter HDR = YES
class SQL_zapros
{
private string connString = "Provider=NZOLEDB;Password=PASSWORD; User ID=LOGIN; Data Source=127.0.0.1; Initial Catalog=SYSTEM; Persist Security Info=True;";
public string sqlcomm = "";
public DataTable exeReader(string cmd, OleDbParameter[] param)
{
OleDbConnection oConn = new OleDbConnection(connString);
OleDbCommand oCmd;
OleDbDataReader oReader;
DataTable AppData = new DataTable();
oConn.Open();
oCmd = oConn.CreateCommand();
oCmd.CommandText = cmd;
try
{
oReader = oCmd.ExecuteReader();
if (oReader.Read())
{
AppData.Load(oReader);
oReader.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
oConn.Close();
return AppData;
}
}
I want to get all the rows from the database
oReader.Read()
reads the first line of the result. AppData.Load
also does that, the first line of the result is ignored. To fix it remove the if
block, just pass the reader directly.
Also use using
blocks when creating instances where the type implements IDisposable
. This ensures the resources are always released even in the event of an exception.
public DataTable exeReader(string cmd, OleDbParameter[] param)
{
using(OleDbConnection oConn = new OleDbConnection(connString))
{
DataTable AppData = new DataTable();
oConn.Open();
var oCmd = oConn.CreateCommand();
oCmd.CommandText = cmd;
try
{
using(var oReader = oCmd.ExecuteReader()) {
AppData.Load(oReader);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
return AppData;
}