sqldatareader

How to efficiently write to file from SQL datareader in c#?

社会主义新天地 提交于 2019-11-27 14:08:12
问题 I have a remote sql connection in C# that needs to execute a query and save its results to the users's local hard disk. There is a fairly large amount of data this thing can return, so need to think of an efficient way of storing it. I've read before that first putting the whole result into memory and then writing it is not a good idea, so if someone could help, would be great! I am currently storing the sql result data into a DataTable, although I am thinking it could be better doing

How to display database records in asp.net mvc view

[亡魂溺海] 提交于 2019-11-27 10:59:11
问题 Using ASP.NET MVC with C#, how do you pass some database records to a View and display them in table form? I need to know how I can transfer/pass some rows of records from a database that have been returned to an SqlDataReader object and pass that object to the View so I can display all the records contained by the object in the View using foreach. The following code is what I'm I'm trying to do. But its not working. The Controller: public ActionResult Students() { String connectionString = "

SqlDataReader - How to convert the current row to a dictionary

安稳与你 提交于 2019-11-27 10:50:42
问题 Is there an easy way to convert all the columns of the current row of a SqlDataReader to a dictionary? using (SqlDataReader opReader = command.ExecuteReader()) { // Convert the current row to a dictionary } Thanks 回答1: You can use LINQ: return Enumerable.Range(0, reader.FieldCount) .ToDictionary(reader.GetName, reader.GetValue); 回答2: Easier than this?: // Need to read the row in, usually in a while ( opReader.Read ) {} loop... opReader.Read(); // Convert current row into a dictionary

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 get nullable DateTime out of the database

夙愿已清 提交于 2019-11-27 09:03:30
My SQL Server database contains nullable DateTime values. How can I convert them to a nullable DateTime object in my application in C#? This is what I would think it would look like, but it doesn't: DateTime? dt = (DateTime?) sqldatareader[0]; A SQL null is not the same as a .NET null; you have to compare against System.DBNull.Value: object sqlDateTime = sqldatareader[0]; DateTime? dt = (sqlDateTime == System.DBNull.Value) ? (DateTime?)null : Convert.ToDateTime(sqlDateTime); In answer to your comment, the data type of the Item property of a DataReader is that of the underlying database type.

How to check if SQLDataReader has no rows

白昼怎懂夜的黑 提交于 2019-11-27 06:34:13
问题 I am trying to figure out how to check if my SqlDataReader is null or has no rows (meaning the reservation does not exist) and then display a messagebox. For some reason when I debug once it hits the While dr.Read()) code it steps out if it does not have a return result. I've tried putting this code in a few different locations but none seem to fire off the messagebox if no records are returned if (dr.GetValue(0) == DBNull.Value || !dr.HasRows) { MessageBox.Show("Reservation Number Does Not

Why is DataTable faster than DataReader

元气小坏坏 提交于 2019-11-27 04:15:17
So we have had a heated debate at work as to which DataAccess route to take: DataTable or DataReader. DISCLAIMER I am on the DataReader side and these results have shaken my world. We ended up writing some benchmarks to test the speed differences. It was generally agreed that a DataReader is faster, but we wanted to see how much faster. The results surprised us. The DataTable was consistently faster than the DataReader. Approaching twice as fast sometimes. So I turn to you, members of SO. Why, when most of the documentation and even Microsoft, state that a DataReader is faster are our test

using on SQLDataReader

天大地大妈咪最大 提交于 2019-11-27 02:18:07
问题 I know I asked a related question earlier. I just had another thought. using (SqlConnection conn = new SqlConnection('blah blah')) { using(SqlCommand cmd = new SqlCommand(sqlStatement, conn)) { conn.open(); // *** do I need to put this in using as well? *** SqlDataReader dr = cmd.ExecuteReader() { While(dr.Read()) { //read here } } } } The argument is that: Since the SqlDataReader dr object is NOT A NEW OBJECT LIKE THE connection or command objects, its simply a reference pointing to the cmd

How to get number of rows using SqlDataReader in C#

夙愿已清 提交于 2019-11-27 01:14:49
My question is how to get the number of rows returned by a query using SqlDataReader in C#. I've seen some answers about this but none were clearly defined except for one that states to do a while loop with Read() method and increment a counter. My problem is that I am trying to fill a multi-dimensional array with the first row being the column header names and every row after that to the be the row data. I know that I can just dump the stuff in a List control and not worry about it, but for my own personal edification and I would also like to pull the data in and out of the array as I choose

Binding sqldatareader to gridview c#

时光毁灭记忆、已成空白 提交于 2019-11-26 21:54:14
问题 I am creating an application for a asp.net class that I am taking. One of the pages in the application needs to allow a user to search for a specific student via last name or user ID. When the student is found the page should display the students data and his/her class schedule. I have gotten everything to work except for the class schedule. The approach I have taken (as we learned in class) was to get the query results via the SqlDataReader and bind it to a GridView. This is done in