sqldatareader

SqlDataReader - How to convert the current row to a dictionary

有些话、适合烂在心里 提交于 2019-11-28 17:52:12
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 You can use LINQ: return Enumerable.Range(0, reader.FieldCount) .ToDictionary(reader.GetName, reader.GetValue); Easier than this?: // Need to read the row in, usually in a while ( opReader.Read ) {} loop... opReader.Read(); // Convert current row into a dictionary Dictionary<string, object> dict = new Dictionary<string, object>(); for( int lp = 0 ; lp < opReader.FieldCount ; lp++ ) {

How to (efficiently) convert (cast?) a SqlDataReader field to its corresponding c# type?

陌路散爱 提交于 2019-11-28 17:08:49
问题 First, let me explain the current situation: I'm reading records from a database and putting them in an object for later use; today a question about the database type to C# type conversion (casting?) arose. Let's see an example: namespace Test { using System; using System.Data; using System.Data.SqlClient; public enum MyEnum { FirstValue = 1, SecondValue = 2 } public class MyObject { private String field_a; private Byte field_b; private MyEnum field_c; public MyObject(Int32 object_id) { using

How to check if SQLDataReader has no rows

为君一笑 提交于 2019-11-28 11:53:16
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 Exist","Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { (read records) } My code... try

How do I get values from a SQL database into textboxes using C#?

穿精又带淫゛_ 提交于 2019-11-28 10:37:07
I'm creating a booking management system and I am having problems trying to get data from a SQL database and insert into a group of textboxes of my application. I want to show the customer details, when a button is clicked, in a DataGridView, but when I click the button, the application throws an exception with the following error message; Invalid attempt to read when no data is present. I have attached a screenshot of the screen where I want to view customer details, and the code for the button, which will eventually show customer details in the respective textboxes. Any help would be greatly

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

痴心易碎 提交于 2019-11-28 09:42:18
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.Read()) { ....do stuff here } } No..It is not mandatory to check (dr.HasRows) if the DataReader contains

Unable to cast object of type 'System.Int32' to type 'System.String' in DataReader.GetString()

大憨熊 提交于 2019-11-28 03:24:45
问题 I was trying to add data from a database to acombobox. try { SqlCeCommand com = new SqlCeCommand("select * from Category_Master", con); SqlCeDataReader dr = com.ExecuteReader(); while(dr.Read()){ string name = dr.GetString(1); cmbProductCategory.Items.Add(name); } } catch(Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } I get the following exception: Unable to cast object of type

Binding sqldatareader to gridview c#

老子叫甜甜 提交于 2019-11-28 01:26:21
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 showStudentSchedule(). The query in this function returns the correct results when I test it against the DB I

How do I fill a DataTable using DataReader

与世无争的帅哥 提交于 2019-11-28 01:15:18
问题 I want to fill DataTable using DataReader. I have created object like this SqlDataReader dr = cmd.ExecuteReader(); if(dr.HasRows) { } 回答1: If all you want is a ReadOnly DataTable for reporting or web, try this: conn = new SqlConnection(connString); string query = "SELECT * FROM Customers"; SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); DataTable dt = new DataTable(); dt.Load(dr); Credit where it's due: http:/

Read boolean values from DB?

不问归期 提交于 2019-11-27 17:36:10
问题 In C#, using SqlDataReader, is there a way to read a boolean value from the DB? while (reader.Read()) { destPath = reader["destination_path"].ToString(); destFile = reader["destination_file"].ToString(); createDir = reader["create_directory"].ToString(); deleteExisting = Convert.ToBoolean(reader["delete_existing"]); skipIFolderDate = reader["skipifolderdate"].ToString(); useTempFile = reader["useTempFile"].ToString(); password = reader["password"].ToString(); } In the code above, delete

What 'length' parameter should I pass to SqlDataReader.GetBytes()

こ雲淡風輕ζ 提交于 2019-11-27 14:42:59
问题 I have a SqlDataReader and need to read a varbinary(max) column from it using the SqlDataReader.GetBytes() method. This method populates a byte array and therefore needs to know what length of data to read. This is where I get confused.. Clearly I want to read all the data that has been returned from the database in this row/column so what 'length' parameter should I pass? As far as I can see, the SqlDataReader doesn't provide any methods to discover what length of data is available,