Hi,
I execute a query that calls a SP, that SP returns data... but when I call it with a DataAdapter I get no result data, if use a DataReader instead... then I get data. The database is SQL Server and the code is using OleDb for reasons I cannot change.
These two calls returns diferent things:
String commandText = "Declare @return_value int; exec dbo.copyTemplate ? , ? , ? , ? , ? , ? , ?, Null , 0 , @return_value;";
Console.WriteLine("Data Adapter");
using (OleDbConnection con = new OleDbConnection(connectionString))
{
using (OleDbDataAdapter da = new OleDbDataAdapter(commandText, con))
using (DataTable table = new DataTable("table"))
{
da.SelectCommand.Parameters.AddWithValue("?",9).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", "AAAAB").DbType = DbType.String;
da.SelectCommand.Parameters.AddWithValue("?", 1).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", 1).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", 2).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", true).DbType = DbType.Boolean;
da.SelectCommand.Parameters.AddWithValue("?", DateTime.Now.Date).DbType = DbType.DateTime;
da.Fill(table);
foreach (DataRow dr in table.Rows)
{
foreach (DataColumn dc in table.Columns)
{
Console.Write(dr[dc].ToString());
Console.Write(" ");
}
Console.WriteLine();
}
}
}
Console.WriteLine("Data Reader");
using (OleDbConnection con = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand(commandText, con))
{
cmd.Parameters.AddWithValue("?", 9).DbType = DbType.Int32;
cmd.Parameters.AddWithValue("?", "AAAAC").DbType = DbType.String;
cmd.Parameters.AddWithValue("?", 1).DbType = DbType.Int32;
cmd.Parameters.AddWithValue("?", 1).DbType = DbType.Int32;
cmd.Parameters.AddWithValue("?", 2).DbType = DbType.Int32;
cmd.Parameters.AddWithValue("?", true).DbType = DbType.Boolean;
cmd.Parameters.AddWithValue("?", DateTime.Now.Date).DbType = DbType.DateTime;
con.Open();
using (OleDbDataReader reader = cmd.ExecuteReader())
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write(reader.GetValue(i) ?? "null");
Console.Write(" ");
}
Console.WriteLine();
}
}
}
Console.ReadKey(true);
This code returns:
Data Adapter
Data Reader
1057
I can call this code hundreds of times and I always get a value in Data Reader a nothing in Data Adapter, and I can have whatever in the second parameter, it doesn't change anything in the result of the SP. I could swap the parameter values between the two calls, or alter the order... and the result would be still the same :(
I don't understand why is this happening.
Has anybody any idea about what could be the problem?
Cheers.
UPDATE: If I fill a DataSet instead a DataTable I get the result:
Console.WriteLine("Data Adapter with DataSet");
using (OleDbConnection con = new OleDbConnection(connectionString))
{
using (OleDbDataAdapter da = new OleDbDataAdapter(commandText, con))
using (DataSet ds = new DataSet("table"))
{
da.SelectCommand.Parameters.AddWithValue("?",9).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", "AAAAB").DbType = DbType.String;
da.SelectCommand.Parameters.AddWithValue("?", 1).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", 1).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", 2).DbType = DbType.Int32;
da.SelectCommand.Parameters.AddWithValue("?", true).DbType = DbType.Boolean;
da.SelectCommand.Parameters.AddWithValue("?", DateTime.Now.Date).DbType = DbType.DateTime;
da.Fill(ds);
foreach (DataTable table in ds.Tables)
foreach (DataRow dr in table.Rows)
{
foreach (DataColumn dc in table.Columns)
{
Console.Write(dr[dc].ToString());
Console.Write(" ");
}
Console.WriteLine();
}
}
}
But the DataSet contains only one table, so I still don't understand why DataAdapter.Fill(DataTable) is not working.