datareader

C# MySQL second DataReader in DataReader while loop

主宰稳场 提交于 2019-12-18 05:18:10
问题 As you might have guessed from the title am I trying to do this: #region check new nations private void checknewnations() { addtolog("server","Checking for new nations"); string sql = "SELECT * FROM " + variables.tbl_nations + " WHERE nations_new=0"; MySqlCommand cmd = new MySqlCommand(sql, connection); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { addtolog("mysql",reader["nations_name"].ToString()); int nation_ID = int.Parse(reader["nations_ID"].ToString()); string

DataReader ordinal-based lookups vs named lookups

☆樱花仙子☆ 提交于 2019-12-18 05:04:07
问题 Microsoft (and many developers) claim that the SqlDataReader.GetOrdinal method improves the performance of retrieving values from a DataReader versus using named lookups ie. reader["ColumnName"]. The question is what is the true performance difference if dealing with small, paged record sets? Is it worth the extra overhead of finding and referencing ordinal indexes throughout the code? 回答1: Microsoft recommends not calling GetOrdinal within a loop. That would include indirect calls with the

Handle DBNull in C#

牧云@^-^@ 提交于 2019-12-17 07:14:51
问题 Is there a better/cleaner way to do this? int stockvalue = 0; if (!Convert.IsDBNull(reader["StockValue"])) stockvalue = (int)reader["StockValue"]; 回答1: The shortest (IMHO) is: int stockvalue = (reader["StockValue"] as int?) ?? 0; Explanation: If reader["StockValue"] is of type int , the value will be returned, and the "??" operator will return the result If reader["StockValue"] is NOT of type int (e.g. DBNull), null will be returned, and the "??" operator will return the value 0 (zero). 回答2:

C#, problems with getting double values from MySQL database

和自甴很熟 提交于 2019-12-14 02:22:38
问题 I have a MySQL database with the table " Products ". A column in " Products " is called " Price " and has the datatype " double ". I need to retrieve the values from that column, so I create a reader, etc.: MySQLCommand cmd = new MySQLCommand("SELECT Price FROM Products", connection); MySQLDataReader reader = cmd.ExecuteReaderEx(); if (reader.HasRows == true) { while (reader.Read() == true) { price = reader["Price"]).ToString(); } } Problem is that price isn't set to the expected value. If

How to iterate through a large SQL result set with multiple related tables

早过忘川 提交于 2019-12-13 17:58:51
问题 I am retrieving a very large number of records with multiple result sets from SQL Server into my .NET 3.5 project. I can't just pull them all into a DataSet and work on them, since it would take up too much memory. I don't need to pull all the records as once but just one record in the parent table and then the related child records. I could accomplish this with using a DataReader but my concern here is the process of iterating through all the records will take many hours. That means the

using datareader with dbnull values in .net4

浪尽此生 提交于 2019-12-13 15:06:32
问题 I heard there is a field extension method in framework 4 that permits one to receive null values from a datareader, without having to go through the process of first testing if not null then ... etc. There's information about the extension method here (MSDN), but i don't know how to use it in code (am relatively new to .net and never used extension methods before). Would appreciate if anyone can give an example. This is what i tried to implement, but it returns an error when a dbnull is

DbDataReader, NextResult() and filling more than one table

那年仲夏 提交于 2019-12-13 15:05:12
问题 This question is continuation of my previous one. Without going into too much details, I'm filling dataset with 2 related 1-to-many tables. So, my question now is - why this code works good public DataAgencyR_DataSet SelectOne(int id) { DataAgencyR_DataSet result = new DataAgencyR_DataSet(); using (DbCommand command = Connection.CreateCommand()) { try { command.CommandText = SqlStrings.SelectDataAgencyR_SelectOne(); var param = ParametersBuilder.CreateByKey(command, "ID_DeclAgenc", id, "ID

Help with a program that needs to read a data file and output scores

血红的双手。 提交于 2019-12-13 06:05:39
问题 So I am working on this program (obviously homework): In a diving competition, each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program that reads the provided data file formatted as depicted in the following table. For each diver output the diver's name and total score using the above scoring rules. Format each diver's total score to two decimal places. So for example, the output for Chen Ruolin below would be: Chen

how do we get the Column Size and dataType from getschemaTable?

坚强是说给别人听的谎言 提交于 2019-12-12 14:42:35
问题 I am a newbie and I am trying to retrieve the Column NAme , Size ( max legth ) and DataType from some table in my database , the following code when i execute it expecting it to display all the column types and names ( i didn't find how to refer to the Size , i used ColumnSize but it is said that DataColumn does not contain a definition for this method ) but when executing it , it only displays : IsColumnSetSystem.Boolean this is the code : private void button1_Click(object sender, EventArgs

Enforce only single row returned from DataReader

梦想与她 提交于 2019-12-12 10:29:24
问题 I seem to write this quite a lot in my code: using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { result = new User((int)reader["UserId"], reader["UserName"].ToString()); } if (reader.Read()) { throw new DataException("multiple rows returned from query"); } } Is there some built in way to do this that I don't know about? 回答1: I don't know, but this code can be delegated into an extension method: public static R Single<R>(this DataReader reader, Func<DataReader,R> selector) { R