sqldatareader

SqlDataReader “Enumeration yielded no results”

这一生的挚爱 提交于 2019-12-01 18:57:03
问题 Been trying to figure out, but I can't. I execute a Stored Procedure via SqlCommand and the SqlDatareader object that is returned doesn't give me the results, however, I can see them if a explore the object in the debugger. here's an image of what I'm talking about: In the Result View row it says " Enumeration yielded no results " but in the path: base -> base -> ResultView -> [0] -> Non-Public members -> _values -> [0],1,[2],[3] the results are shown. Anyone has an idea how to get them? This

ExecuteReader requires an open and available Connection. The connection's current state is closed

≯℡__Kan透↙ 提交于 2019-12-01 16:25:21
Ok, I asked about this very error earlier this week and had some very helpful answers and without doubt things have drastically improved since I started following the suggestions. However, now I am using the 'correct', best practice method to access the database I still get this error on some functions and I cannot get it to disappear for that block. Here is my code: Public Shared Function doesBasketExist(ByVal baskethash As String) As Boolean Dim _r As Boolean Using db As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("pitstopConnectionString").ConnectionString)

Make DbDataReader start reading again from the beginning of the result set

☆樱花仙子☆ 提交于 2019-12-01 13:58:45
问题 How to make dr.Read(); start reading again from the beginning if a condition is satisfied? Something like: SqlDataReader dr = command.ExecuteReader(); for(int i=0; dr.Read() ; i++){ if(condition ){ //let dr.Read() start reading from the beginning } } 回答1: You can't. The *DataReader classes are forward-only iterators. Instead, you can store the results in a List<T> (or a DataTable ) 回答2: The only way to restart it is to grab a new reader with ExecuteReader() . 回答3: You can do that by first

SqlDataReader and SqlCommand

 ̄綄美尐妖づ 提交于 2019-12-01 09:42:11
问题 I have the following code. using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { connection.Open(); SqlCommand select = new SqlCommand("SELECT RTRIM(LTRIM(PART_NO)) AS PART_NO, record FROM [RMAData].[dbo].[IMPORTING_ORDER_EDI] WHERE sessionID = '" + Session.SessionID + "'", connection); SqlDataReader reader = select.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { if (!currentPart.IsActive) { // this

How to read dynamic properties from database

不羁岁月 提交于 2019-12-01 05:16:32
I will try to explain my scenario the best way I can. I have the following tables in my database: Products{ProductId, CategoryId ...} Categories{CategoryId ...} CategoryProperties{CategoryPropertyId, CategoryId, Name ...} PropertyValues{ProductId, CategoryPropertyId, Value ...} So the goal is to have list of products which belongs to some category and each category can have 'n' number of properties with values in the 'PropertyValues' table. I have a query that returns data based on 'categoryId' so I can always get different results from the database: For the CPU category: intel i7 | CPU |

How to read dynamic properties from database

不羁岁月 提交于 2019-12-01 03:26:06
问题 I will try to explain my scenario the best way I can. I have the following tables in my database: Products{ProductId, CategoryId ...} Categories{CategoryId ...} CategoryProperties{CategoryPropertyId, CategoryId, Name ...} PropertyValues{ProductId, CategoryPropertyId, Value ...} So the goal is to have list of products which belongs to some category and each category can have 'n' number of properties with values in the 'PropertyValues' table. I have a query that returns data based on

DataReader - hardcode ordinals?

醉酒当歌 提交于 2019-12-01 03:05:35
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 becoming increasingly strongly-typed I feel more uncomfortable with this. I'm also aware that the above

Trouble loading SQL Data Reader data into DataTable

女生的网名这么多〃 提交于 2019-11-30 21:20:14
string query = "select * from cfo_daily_trans_hist"; try { using (SqlConnection connection = new SqlConnection( cnnString)) { SqlCommand command = new SqlCommand(query); command.Connection = connection; connection.Open(); var result = command.ExecuteReader(); DataTable datatable = new DataTable(); datatable.Load(result); connection.Close(); } } So the var result is created through the ExecuteReader(); and HasRows is true , and it shows the correct amount of fields. However, the DataTable that I create from it is empty. What am I doing wrong? I'm 99% sure it's getting data, but I don't know how

C# Performance gain returning a Nullable Type from a SqlDataReader

ぐ巨炮叔叔 提交于 2019-11-30 20:10:01
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; } I seem to recall that it can sometimes by faster to get the value as an object and then check if that

SQLDataReader Row Count

[亡魂溺海] 提交于 2019-11-30 17:17:10
I am trying to get the number of rows that were returned by iterating the reader. But I always get 1 when I run this code? Did I screw up something in this? int count = 0; if (reader.HasRows) { while (reader.Read()) { count++; rep.DataSource = reader; rep.DataBind(); } } resultsnolabel.Text += " " + String.Format("{0}", count) + " Results"; SQLDataReaders are forward-only. You're essentially doing this: count++; // initially 1 .DataBind(); //consuming all the records //next iteration on .Read() //we've now come to end of resultset, thanks to the DataBind() //count is still 1 You could do this