idatareader

c# IDataReader SqlDataReader difference

[亡魂溺海] 提交于 2019-12-12 07:52:30
问题 Can someone tell me the difference between these two pieces of code? Why use IDataReader? using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // get data from the reader } } using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // get data from the reader } } 回答1: SqlDataReader implements the interface IDataReader . So do all other ADO.NET drivers (Oracle, MySql, etc). You can use IDataReader , so that if you plan to change database engine some

Using AutoMapper with DataTable with missing columns

て烟熏妆下的殇ゞ 提交于 2019-12-11 14:48:56
问题 I'm using AutoMapper to map a datatable to a List. In my scenario, the columns for the datatable may change depending on an outside variable. I can successfully map the datatable to the object w/ the following: AutoMapper.Mapper.CreateMap<IDataReader, Person>(); DataTableReader dtr = myDataTable.CreateDataReader(); List<Person> people = new List<Person>(); people = AutoMapper.Mapper.Map<List<Person>>(dtr); This all works fine. But there are some properties that I need to convert to integer on

Null safe way to get values from an IDataReader

旧街凉风 提交于 2019-12-10 01:25:31
问题 (LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0); this name value is coming from database.. what happening here is if this name value is null while reading it's throwing an exception? I am manually doing some if condition here. I don't want to write a manual condition to check all my variables.. I am doing something like this now.. string abc = (Idatareader)datareader.GetValue(0); if(abc = null) //assiging null else assiging abc value is there something like can we write

Supplying stream as a source of data for a binary column when SqlBulkCopy is used

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 09:36:10
If one needs to read data from SqlServer in a streamed fashion, there are some capabilities for that. Such as using SqlDataReader with CommandBehavior.SequentialAccess , and particularly when binary column data needs to be accessed there is the GetStream(int) method for that: var cmd = new SqlCommand(); cmd.Connection = connection; cmd.CommandText = @"select 0x0123456789 as Data"; using (var dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { dr.Read(); var stream = dr.GetStream(0); // access stream } But what about streaming data in the opposite direction, when one needs to feed data

Null safe way to get values from an IDataReader

你。 提交于 2019-12-05 01:32:17
(LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0); this name value is coming from database.. what happening here is if this name value is null while reading it's throwing an exception? I am manually doing some if condition here. I don't want to write a manual condition to check all my variables.. I am doing something like this now.. string abc = (Idatareader)datareader.GetValue(0); if(abc = null) //assiging null else assiging abc value is there something like can we write extension method for this? thanks Here is a couple extension methods that will nicely wrap up all of

Checking for Nulls on DB Record Mapping

送分小仙女□ 提交于 2019-12-04 15:34:12
How can I check for db null values in the attached code? Please understand I am a new C# convert... What this code does is takes a IDataReader object and converts and maps it to a strongly-typed list of objects. But what I am finding is it completely errors out when there are null columns returned in the reader. Converter internal class Converter<T> where T : new() { // Declare our _converter delegate readonly Func<IDataReader, T> _converter; // Declare our internal dataReader readonly IDataReader dataReader; // Build our mapping based on the properties in the class/type we've passed in to the

How do I convert a DataTable to an IDatareader?

放肆的年华 提交于 2019-12-03 16:39:34
问题 We all know that DataReaders are quicker than DataTables since the a DataReader is used in the construction of a DataTable. Therefore given that I already have a DataTable.... Why would I want to convert it to a DataReader? Well I am creating an internal interface called IDataProvider. This interface is intended to be implemented both locally and as a WebService. The interface will have a method "Getdata" which takes some criteria information and returns some data. Since a DataReader is the

c# IDataReader SqlDataReader difference

会有一股神秘感。 提交于 2019-12-03 12:52:52
Can someone tell me the difference between these two pieces of code? Why use IDataReader? using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // get data from the reader } } using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // get data from the reader } } SqlDataReader implements the interface IDataReader . So do all other ADO.NET drivers (Oracle, MySql, etc). You can use IDataReader , so that if you plan to change database engine some day, you don't have to rewrite all your SqlDataReader references. The same goes for IDbConnection , IDbCommand

How do I use automapper to map a dataset with multiple tables

旧巷老猫 提交于 2019-11-30 13:59:55
DISCLAIMER: this is a copy paste from an older stackoverflow post that isn't available anymore, but I have exaclty the same problem, so it seemed appropriate to repost it as it was never answered. I have a stored procedure that will return 4 result sets (contacts, addresses, email, phones) which is populated into a dataset. I would like to use AutoMapper to populate a complex object. public class Contact { public Guid ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List<Address> Addresses { get; set; } public List<Phone> Phones { get;

DataReader ordinal-based lookups vs named lookups

情到浓时终转凉″ 提交于 2019-11-29 07:30:03
Microsoft (and many developers) claim that the SqlDataReader.GetOrdinal method improves the performance of retrieving values from a DataReader versus using named lookups ie. reader["ColumnName"]. The question is what is the true performance difference if dealing with small, paged record sets? Is it worth the extra overhead of finding and referencing ordinal indexes throughout the code? Microsoft recommends not calling GetOrdinal within a loop. That would include indirect calls with the string indexer. You can use GetOrdinal at the top of your loop put the ordinals in an array and have the