sqldatareader

How to read from a data with no column name using SqlDataReader

左心房为你撑大大i 提交于 2019-12-10 11:55:08
问题 I have Visual Studio 2013 Ultimate and creating application of WPF. I have to read the data from a SQL Server stored procedure by using SqlDataReader . Sometimes, if data exists, I can read data using sdr(sqldatareader).Read() But in the case when no data exists, I just select 'false' in stored procedure but I don't know how to read that string with having no column name by SqlDataReader . 回答1: You can do this. reader.GetString(0); //0 stands for "the 0'th column" 来源: https://stackoverflow

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

拜拜、爱过 提交于 2019-12-09 18:01:01
问题 A fairly large web application written in C# keeps throwing up 2 errors: 'ExecuteReader requires an open and available Connection. The connection's current state is open.' and 'Invalid attempt to call Read when reader is closed.' These errors were sporadic -- the pages used to load fine about 95% of the time, but recently they've become endemic, they're occurring all the time and basically crippling the application's functionality. The web app is highly reliant on an MS SQL database, and the

How to get table name of a column from SqlDataReader

五迷三道 提交于 2019-12-09 11:38:05
问题 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

What does the buffered parameter do in Dapper dot net?

血红的双手。 提交于 2019-12-09 04:17:34
问题 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:

Timeout exception causes SqlDataReader to close?

百般思念 提交于 2019-12-08 23:45:43
问题 I'm trying to pull some binary data from a database and write them to pdf files. For the most part, this is going along swimmingly, but the occasional row of data seems to throw a particular error - Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Keep in mind, this only happens on a handful of rows, and is never random. The same rows always throw the exception. I'm not really sure why the exception is being thrown, but I'm ok

How to safely cast nullable result from sqlreader to int?

不想你离开。 提交于 2019-12-08 19:42:14
问题 I have a table which contains null values and I need to get data from the table using SqlDataReader. I can't figure out how I can safely cast DBNull to int. I'm doing it in this way at the moment: ... reader = command.ExecuteReader(); while (reader.Read()) { int y = (reader["PublicationYear"] != null) ? Convert.ToInt32(reader["PublicationYear"]) : 0; ... } ... but getting a Object cannot be cast from DBNull to other types. when PublicationYear is null. How can I get the value safely? Thanks.

Cannot convert method group 'Read' to non-delegate type 'bool'

雨燕双飞 提交于 2019-12-08 17:19:08
问题 I am trying to use SqlDataReader to check if a entry exists. If it exists, it will return the ID, else it will return false. When I try to compile, I'm getting the error "Cannot convert method group 'Read' to non-delegate type 'bool'. I have been following an example I found in VB, but it seems the translation may not be correct. private string checkProfileExists() { string strReturn = "False"; string strSql = ("SELECT ID FROM tblInformation WHERE txtUsername=@UserName " + "AND TrackingID=

Reuse of SqlConnection and SqlDataReader

亡梦爱人 提交于 2019-12-08 15:26:39
问题 If I want to run multiple SELECT queries on different tables, can I use the same SqlDataReader and SqlConnection for all of them?? Would the following be wise?? (I typed this up fast, so it lacks try/catch): MySqlCommand myCommand = new MySqlCommand("SELECT * FROM table1", myConnection); myConnection.Open(); SqlDataReader myDataReader = myCommand.ExecuteReader(); while(myReader.Read()) { //Perform work. } myCommand.commandText = "SELECT * FROM table2"; myReader = myCommand.ExecuteReader();

How to best clean up a SQLDataReader when using linq

让人想犯罪 __ 提交于 2019-12-08 07:29:12
问题 I have a method that returns a data reader. Normally I would wrap a using around the data reader so that it gets disposed nicely after I iterate through it. THe problem is that I've chosen to consume the data reader using Linq, which means that the defered execution causes the reader to get disposed early. Is there a proper pattern to consume and dispose of the data reader using Linq without having to built a complete collection of it's contents? using (System.Data.SqlClient.SqlDataReader

Can I reset and loop again through MySqlDataReader?

故事扮演 提交于 2019-12-08 05:50:41
问题 I have a MySqlDataReader object, with the result of this query : SELECT warehouse, leasing, transportation, maintenance, manpower FROM retail WHERE zone = 'Central' GROUP BY warehouse And then I loop through the DataReader once, while (r2.Read()) { strXml.AppendFormat("<set label = '{0}'></set>",r2["warehouse"].ToString()); } And now I want to loop through it again...!! I know that DataReader is only a 'forward-only' object.. But is there any other solution for me here ? I'm asking, is there