sqldatareader

How to get table name of a column from SqlDataReader

时光总嘲笑我的痴心妄想 提交于 2019-12-03 13:09:13
I have an SQL query I get from a configuration file, this query usually contains 3-6 joins. I need to find at run time, based on the result set represented by SqlDataReader, to find the name of the table for each column. Here are some thing that don't work: SqlDataReader.GetName returns the column name but not the table name. SqlDataReader.GetSchemaTable returns a data table with column information - but all the table names are null. Querying information_schema doesn't help because I need data on the results of the current query (and the column names are not unique - there are columns with 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

c# - Fill generic list from SqlDataReader

拜拜、爱过 提交于 2019-12-03 11:25:27
How can I add values that a SqlDataReader returns to a generic List? I have a method where I use SqlDataReader to get CategoryID from a Category table. I would like to add all the CategoryID a generic List. This dose not work because it returns only one categoryID and that is the last one. I want to add all the categoryID to the list and then return them. How do I do that? SqlConnection connection = null; SqlDataReader reader = null; SqlCommand cmd = null; try { connection = new SqlConnection(connectionString); cmd = new SqlCommand("select CategoryID from Categories", connection ); connection

SqlDataReader Best way to check for null values -sqlDataReader.IsDBNull vs DBNull.Value

て烟熏妆下的殇ゞ 提交于 2019-12-03 09:19:38
问题 I want to retrieve decimal values from the database and I would like to know which is the recommended way to check for null values. I have seen on MSDN - DBNull.Value Field that this check is rarely used. Thus, is the reader.IsDBNull the best/most efficient way to check for nulls? I have created 2 sample methods: public static decimal? GetNullableDecimal(SqlDataReader reader, string fieldName) { if (reader[fieldName] == DBNull.Value) { return null; } return (decimal)reader[fieldName]; }

DataReader or DataSet when pulling multiple recordsets in ASP.NET

此生再无相见时 提交于 2019-12-03 06:11:49
问题 I've got an ASP.NET page that has a bunch of controls that need to be populated (e.g. dropdown lists). I'd like to make a single trip to the db and bring back multiple recordsets instead of making a round-trip for each control. I could bring back multiple tables in a DataSet, or I could bring back a DataReader and use '.NextResult' to put each result set into a custom business class. Will I likely see a big enough performance advantage using the DataReader approach, or should I just use the

How to make streams from BLOBs available in plain old C# objects when using SqlDataReader?

≯℡__Kan透↙ 提交于 2019-12-03 03:06:58
This is the scenario: We store files, e.g. relatively large documents (10-300MB), in blobs in our MSSQL database. We have a very small domain model so we use the clean SqlDataReader approach for our repository, instead of an ORM, to avoid unnecessary dependencies. We want to use the objects in server context on ASP.NET/ASP.NET MVC web pages. We do not want to temporarily store the blobs in byte[], to avoid high memory usage on the server So what I have been doing is to implement my own SqlBlobReader. It inherits Stream and IDisposable and during instantiation we must supply a SqlCommand

What does the buffered parameter do in Dapper dot net?

一个人想着一个人 提交于 2019-12-02 23:20:52
Dapper dot net has a buffer parameter (a bool), but as far as I can tell the only thing it does is cast the result to a list before returning it. As per the documentation : Dapper's default behavior is to execute your sql and buffer the entire reader on return. This is ideal in most cases as it minimizes shared locks in the db and cuts down on db network time. However when executing huge queries you may need to minimize memory footprint and only load objects as needed. To do so pass, buffered: false into the Query method. I'm not sure how casting the result to a list accomplishes this. Am I

How do I execute a SELECT query against a SQLServer database and iterate results using PowerShell

喜你入骨 提交于 2019-12-02 21:17:27
问题 Say I have a table with 3 columns - "Column1", "Column2", and "Column3" - datatype is varchar(100) for all 3. Using PowerShell, how do I connect to SQL Server and use SqlDataReader and ForEach operator to view the contents of "Column2"? 回答1: Here's roughly how I'm doing it: $SqlServer = 'sql.example.com'; $SqlDatabase = 'MyDB'; $SqlConnectionString = 'Data Source={0};Initial Catalog={1};Integrated Security=SSPI' -f $SqlServer, $SqlDatabase; $SqlQuery = "SELECT Name FROM dbo.Person ORDER BY

DataReader or DataSet when pulling multiple recordsets in ASP.NET

拈花ヽ惹草 提交于 2019-12-02 19:34:42
I've got an ASP.NET page that has a bunch of controls that need to be populated (e.g. dropdown lists). I'd like to make a single trip to the db and bring back multiple recordsets instead of making a round-trip for each control. I could bring back multiple tables in a DataSet, or I could bring back a DataReader and use '.NextResult' to put each result set into a custom business class. Will I likely see a big enough performance advantage using the DataReader approach, or should I just use the DataSet approach? Any examples of how you usually handle this would be appreciated. If you have more

asp.net Invalid attempt to FieldCount when reader is closed error [duplicate]

一曲冷凌霜 提交于 2019-12-02 13:55:27
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Invalid attempt to call FieldCount when reader is closed im using asp.net with c#. Im atempting to close the connection on the finally statement on aspx2.cs. In aspx1.cs: private void BindDataGrids() { try { DataGrid1.DataSource = instance.ShowDifferences(Convert.ToInt32(Request.QueryString ["CrewId"]), DateTime.Parse(Request.QueryString["StartDate"]), DateTime.Parse(Request.QueryString["EndDate"])); DataGrid1