datareader

while on IDataReader.Read doesn't work with yield return but foreach on reader does

混江龙づ霸主 提交于 2019-12-04 15:04:11
This is a commonly seen ADO.NET pattern to retrieve data from database using a data reader, but strangely doesn't work. Doesn't work: public static IEnumerable<IDataRecord> SelectDataRecord<T>(string query, string connString) where T : IDbConnection, new() { using (var conn = new T()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = query; cmd.Connection.ConnectionString = connString; cmd.Connection.Open(); using (var reader = (DbDataReader)cmd.ExecuteReader()) { // the main part while (reader.Read()) { yield return (IDataRecord)reader; } } } } This does work: public static

How to add columns to DataReader

自古美人都是妖i 提交于 2019-12-04 14:37:55
My goal is to retrieve data from a data source, add some metadata to it and insert it to another target. The target has schema with four more columns then the source (calculated columns). I am using SqlBulkCopy , which requires a reader with all columns (including the 4 calculated). Is there a way to add columns to the DataReader manually? or if it's not possible what alternatives i have for the data insertion? I needed to do this, and also have the ability to make columns based on other columns and have the column's value depend on the row index of the reader. This class implements

DataReader Behaviour With SQL Server Locking

纵饮孤独 提交于 2019-12-04 10:34:01
We are having some issues with our data layer when large datasets are returned from a SQL server query via a DataReader . As we use the DataReader to populate business objects and serialize them back to the client, the fetch can take several minutes (we are showing progress to the user :-)), but we've found that there's some pretty hard-core locking going on on the affected tables which is causing other updates to be blocked. So I guess my slightly naive question is, at what point are the locks which are taken out as a result of executing the query actually relinquished? We seem to be finding

Storing results of a DataReader into an array in VB.NET

情到浓时终转凉″ 提交于 2019-12-04 10:03:09
How can I store the results of a DataReader into an array, but still be able to reference them by column name? I essentially want to be able to clone the DataReader's content so that I can close the reader and still have access. I don't want to store the items in a DataTable like everyone suggests. I've seen a lot of answers, but I couldn't really find any for what I wanted The easiest way I've found to do this is by populating the array with dictionaries with Strings as keys and Objects as values, like so: ' Read data from database Dim result As New ArrayList() Dr = myCommand.ExecuteReader()

Threads vs TPL vs Async Delegates in ASP.NET

本秂侑毒 提交于 2019-12-04 04:03:44
问题 I have an application that is working well in production, but I wonder if I could have implemented the concurrency better.... ASP.NET .NET 4, C# Basically, it generates n number of sql statements on the fly (approx 50 at the moment) and then runs them concurrently and writes the data to .csv files. EDIT: First I create a thread to do all the work on so the page request can return. Then on that thread... For each of the SQL statements I create a new Task using the TPL and execute it using a

Can I create an anonymous type collection from a DataReader?

99封情书 提交于 2019-12-03 07:12:46
I have this code: var query = "SELECT * FROM Cats"; var conn = new SqlConnection(sqlConnectionString); conn.Open(); var cmd = new SqlCommand(query); var reader = cmd.ExecuteReader(); while (reader.Read()) { var CatName = reader.GetString(0); var CatDOB = reader.GetDateTime(1); var CatStatus = reader.GetInt32(2); } I'd like to pull the rows out into an anonymous type collection, which I'd normally do using LINQ to iterate, but I an not sure if it's possible due to the way you have to call .Read() each time to get the next row. Is there a way to do this? You can create helper generic method and

I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object

眉间皱痕 提交于 2019-12-03 07:11:52
问题 Here is my sample code that I am using to fetch data from database: on DAO layer: public IEnumerable<IDataRecord> GetDATA(ICommonSearchCriteriaDto commonSearchCriteriaDto) { using(DbContext) { DbDataReader reader = DbContext.GetReader("ABC_PACKAGE.GET_DATA", oracleParams.ToArray(), CommandType.StoredProcedure); while (reader.Read()) { yield return reader; } } } On BO layer I am calling the above method like: List<IGridDataDto> GridDataDtos = MapMultiple(_costDriversGraphDao.GetGraphData

I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object

☆樱花仙子☆ 提交于 2019-12-02 20:43:03
Here is my sample code that I am using to fetch data from database: on DAO layer: public IEnumerable<IDataRecord> GetDATA(ICommonSearchCriteriaDto commonSearchCriteriaDto) { using(DbContext) { DbDataReader reader = DbContext.GetReader("ABC_PACKAGE.GET_DATA", oracleParams.ToArray(), CommandType.StoredProcedure); while (reader.Read()) { yield return reader; } } } On BO layer I am calling the above method like: List<IGridDataDto> GridDataDtos = MapMultiple(_costDriversGraphDao.GetGraphData(commonSearchCriteriaDto)).ToList(); on mapper layer MapMultiple method is defined like: public IGridDataDto

Big Performance Problems With Oracle DataReader in .Net

那年仲夏 提交于 2019-12-02 11:39:05
问题 I have a few Oracle procedures that generate/return a large amount of data that I need to write out to a file. I'm currently trying to accomplish with a data-reader. It seems to be working, I've successfully generated a 479mb file without any trouble. It took less than 4 minutes from the time I retrieved the dataReader to complete the file. But the dataReader I get for a particular procedure is crawling . It's unbelievably slow. I modified my code to try and get a better idea of what is going

I keep getting this error: “Invalid attempt to call Read when reader is closed”

我只是一个虾纸丫 提交于 2019-12-02 11:35:47
Here is my code, I close and open the reader and it's still not working. A few threads can access this function concurrently but there is a lock. It works a few times in the beginning but sooner or later I get the exception 'Invalid attempt to call Read when reader is closed' at private IList<BursaUser> GetUsers(SqlCommand cmd) { IList<User> users = new List<User>(); User user; lock (thisLock) { SqlDataReader dr = null; try { Conn.Open(); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { user = new User { UserId = Convert.ToInt32(dr["WorkerNum"]), CompanyName = dr[