datareader

How to handle multiple ResultSets, each with multiple Rows? IDataReader.NextResult() ending Read()

只愿长相守 提交于 2019-11-28 07:08:39
问题 How to handle multiple ResultSets, each with multiple Rows? The call to NextResult() breaks the while loop. Some of my SPs return multiple ResultSets. I'm handling these with NextResult() but when I do and my SP only has a single ResultSet, I see the while loop with Read() finishes leaving me with only the first Row. Without the call to NextResult() I get all the rows for the first ResultSet but of course the second and subsequent ResultSets don't get processed? using (IDataReader reader =

Invalid attempt to call FieldCount when reader is closed

纵然是瞬间 提交于 2019-11-28 06:10:24
问题 The error above occurs when I try to do a dataReader.Read on the data recieved from the database. I know there are two rows in there so it isnt because no data actually exists. Could it be the CommandBehavior.CloseConnection, causing the problem? I was told you had to do this right after a ExecuteReader? Is this correct? try { _connection.Open(); using (_connection) { SqlCommand command = new SqlCommand("SELECT * FROM Structure", _connection); SqlDataReader dataReader = command.ExecuteReader

.NET DataTable skips rows on Load(DataReader)

不羁的心 提交于 2019-11-27 14:38:22
I'm trying to populate a DataTable, to build a LocalReport, using the following: MySqlCommand cmd = new MySqlCommand(); cmd.Connection = new MySqlConnection(Properties.Settings.Default.dbConnectionString); cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT ... LEFT JOIN ... WHERE ..."; /* query snipped */ // prepare data dataTable.Clear(); cn.Open(); // fill datatable dt.Load(cmd.ExecuteReader()); // fill report rds = new ReportDataSource("InvoicesDataSet_InvoiceTable",dt); reportViewerLocal.LocalReport.DataSources.Clear(); reportViewerLocal.LocalReport.DataSources.Add(rds); At one

Multiples Table in DataReader

爷,独闯天下 提交于 2019-11-27 09:23:45
I normally used DataSet because It is very flexible. Recently I am assigned code optimization task , To reduce hits to the database I am changing two queries in one procedure. one Query returns the count and the other returns the actual data . That is , My stored procedure returns two tables. Now, I know how to read both tables using DataSets , But I need to read both tables using DataReader . In search of that I found This . I follow the article and wrote my code like this: dr = cmd.ExecuteReader(); while (dr.Read()) { } if (dr.NextResult()) // this line throws exception { while (dr.Read()) {

How to fill Dataset with multiple tables?

☆樱花仙子☆ 提交于 2019-11-27 08:21:18
I'm trying to fill DataSet which contains 2 tables with one to many relationship. I'm using DataReader to achieve this : public DataSet SelectOne(int id) { DataSet result = new DataSet(); using (DbCommand command = Connection.CreateCommand()) { command.CommandText = "select * from table1"; var param = ParametersBuilder.CreateByKey(command, "ID", id, null); command.Parameters.Add(param); Connection.Open(); using (DbDataReader reader = command.ExecuteReader()) { result.MainTable.Load(reader); } Connection.Close(); } return result; } But I've got only one table filled up. How do I achieve my goal

Handle NULL values when reading through OracleDataReader?

为君一笑 提交于 2019-11-27 06:44:02
问题 I'm working on my first ASP.Net application, and seem to be hitting a lot of hurdles (background primarily in WinForms with a recent MVC5 project under my belt). I am successfully making my DB connection using OracleCommand and executing my query, but when I try reading through the rows I am getting a Column contains NULL value on the second row for odr.GetDecimal(1) . Anyone know how to handle null values when reading through an OracleDataReader? Below is my code: List<YearsOfService>

Handle DBNull in C#

有些话、适合烂在心里 提交于 2019-11-27 03:38:51
Is there a better/cleaner way to do this? int stockvalue = 0; if (!Convert.IsDBNull(reader["StockValue"])) stockvalue = (int)reader["StockValue"]; The shortest (IMHO) is: int stockvalue = (reader["StockValue"] as int?) ?? 0; Explanation: If reader["StockValue"] is of type int , the value will be returned, and the "??" operator will return the result If reader["StockValue"] is NOT of type int (e.g. DBNull), null will be returned, and the "??" operator will return the value 0 (zero). Chris Marisic The way I handle this is int? stockvalue = reader["StockValue"] as int?; Very simple, clean and one

Convert rows from a data reader into typed results

一个人想着一个人 提交于 2019-11-27 02:58:38
I'm using a third party library which returns a data reader. I would like a simple way and as generic as possible to convert it into a List of objects. For example, say I have a class 'Employee' with 2 properties EmployeeId and Name, I would like the data reader (which contains a list of employees) to be converted into List< Employee>. I guess I have no choice but to iterate though the rows of the data reader and for each of them convert them into an Employee object that I will add to the List. Any better solution? I'm using C# 3.5 and ideally I would like it to be as generic as possible so

DataReader best-practices

让人想犯罪 __ 提交于 2019-11-27 02:21:30
问题 Similar to this question, but the answers never really got around to what I want to know. Is there any standards around getting values from a DataReader? I.e., is this dataReader.GetString(dataReader.GetOrdinal("ColumnName")); considered better/worse/the same as this? (string) dataReader["ColumnName"]; 回答1: Here is the way that I do it: Int32 ordinal = dataReader.GetOrdinal("ColumnName"); if (!dataReader.IsDBNull(ordinal)) yourString = dataReader.GetString(ordinal); It is important to check

.NET DataTable skips rows on Load(DataReader)

拥有回忆 提交于 2019-11-26 16:50:02
问题 I'm trying to populate a DataTable, to build a LocalReport, using the following: MySqlCommand cmd = new MySqlCommand(); cmd.Connection = new MySqlConnection(Properties.Settings.Default.dbConnectionString); cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT ... LEFT JOIN ... WHERE ..."; /* query snipped */ // prepare data dataTable.Clear(); cn.Open(); // fill datatable dt.Load(cmd.ExecuteReader()); // fill report rds = new ReportDataSource("InvoicesDataSet_InvoiceTable",dt);