sqldatareader

yield return vs. return IEnumerable<T>

只谈情不闲聊 提交于 2019-12-21 07:15:32
问题 I've noticed something curious about reading from an IDataReader within a using statement that I can't comprehend. Though I'm sure the answer is simple. Why is it that whilst inside the using (SqlDataReader rd) { ... } if I directly perform a yield return the reader stays open for the duration of the read. But if I perform a direct return calling a SqlDataReader extension method (outlined below) that the reader closes before the enumerable can be actualized? public static IEnumerable<T>

Invalid attempt to Read when reader is closed

孤者浪人 提交于 2019-12-20 06:18:21
问题 I'm working on C# and MySql request. I'm trying to retrieve my datas in my db but I have this error message : Invalid attempt to Read when reader is closed. Thanks for your help guys :) I have this function : public MySqlDataReader GetValueFromTable(string table, ArrayList attribut, ArrayList parameter) { string query = string.Empty; MySqlDataReader rdr = null; try { query = "SELECT * FROM `" + table + "` WHERE "; for (int i = 0; i < attribut.Count; i++) { query += attribut[i] as string;

Drop down list not binding with sqldatareader

十年热恋 提交于 2019-12-20 03:56:14
问题 i have a form with a collection of about five drop down . i have my query as follows . string sql = "SELECT a.clientID ,a.[cname],b.bid,b.[bname],c.contactID, c.[name] FROM " + " dbo.[CLIENT] AS a INNER JOIN dbo.[BRANCH] AS b " + "ON a.clientID = b.clientID JOIN dbo.[CONTACT] AS " + " c ON b.bid = c.bid ORDER BY a.clientID "; i then followed and bind my drop down individually to their respective columns as follows. SqlCommand cmd = new SqlCommand(sql, connection); cmd.CommandType =

DataReader - hardcode ordinals?

家住魔仙堡 提交于 2019-12-19 05:10:13
问题 When returning data from a DataReader I would typically use the ordinal reference on the DataReader to grab the relevant column: if (dr.HasRows) Console.WriteLine(dr[0].ToString()); or if (dr.HasRows) Console.WriteLine(dr.GetString(0)); or if (dr.HasRows) Console.WriteLine((string)dr[0]); I have always done this because I was advised at an early stage that using dr["ColumnName"] or a more elegant way of indexing causes a performance hit. However, while all references to data entities are

C# Performance gain returning a Nullable Type from a SqlDataReader

别等时光非礼了梦想. 提交于 2019-12-19 02:27:10
问题 I have a simple method that returns a Nullable Int32 from a DataReader rather than the built in GetInt32. I am calling this method many many times and have one situation where any time I can shave off of it would be beneficial. Can anyone suggest any alternative and faster ways of getting a nullable Int32 out of the DataReader? private Int32? GetNullableInt32(SqlDataReader dataReader, int fieldIndex) { return !dataReader.IsDBNull(fieldIndex) ? dataReader.GetInt32(fieldIndex) : (Int32?)null; }

C# Performance gain returning a Nullable Type from a SqlDataReader

筅森魡賤 提交于 2019-12-19 02:27:04
问题 I have a simple method that returns a Nullable Int32 from a DataReader rather than the built in GetInt32. I am calling this method many many times and have one situation where any time I can shave off of it would be beneficial. Can anyone suggest any alternative and faster ways of getting a nullable Int32 out of the DataReader? private Int32? GetNullableInt32(SqlDataReader dataReader, int fieldIndex) { return !dataReader.IsDBNull(fieldIndex) ? dataReader.GetInt32(fieldIndex) : (Int32?)null; }

Using SQLDataReader instead of recordset

落花浮王杯 提交于 2019-12-18 12:33:07
问题 I am new to this and had this question. Can I use SQLDataReader instead of a Recordset. I want to achieve the following result in an SQLDataReader. Dim dbConn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim sqlstr As String = "SELECT Name,Status FROM table1 WHERE id=" + item_id.Value.ToString rs.Open(SQL, dbConn) While Not rs.EOF txtName.Text = rs.Fields.Item("Name").Value ddlstatus.SelectedIndex = 1 rs.MoveNext() End While rs.Close() rs = Nothing dbConn.Close() dbConn = Nothing Can

DataReader ordinal-based lookups vs named lookups

☆樱花仙子☆ 提交于 2019-12-18 05:04:07
问题 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? 回答1: Microsoft recommends not calling GetOrdinal within a loop. That would include indirect calls with the

Should I call SqlDataReader.HasRows if I am calling SqlReader.Read

此生再无相见时 提交于 2019-12-17 19:13:15
问题 Trying to see if it is beneficial to add an if (dr.HasRows) before the while (dr.read()) function. I mean, technically if it doesn't have rows it isn't going to read, so would it matter if you checked this first? using (SqlDataReader dr = cmd.ExecuteReader()) { if (dr.HasRows) { while (dr.Read()) { ....do stuff here } } } or is this going to essentially do the exact same thing if you're just making sure it has values to provide... using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr

How to read multiple resultset from SqlDataReader? [duplicate]

本秂侑毒 提交于 2019-12-17 18:48:15
问题 This question already has an answer here : How to handle multiple ResultSets, each with multiple Rows? IDataReader.NextResult() ending Read() (1 answer) Closed 2 years ago . I have a SP from that I am trying to return 2 result set from, and in my .cs file i am trying something like this: dr = cmd.ExecuteReader(); while (dr.Read()) { RegistrationDetails regDetails = new RegistrationDetails() { FName = dr["FName"].ToString(), LName = dr["LName"].ToString(), MName = dr["MName"].ToString(),