datareader

DataReader.GetFieldType returned null

好久不见. 提交于 2019-11-30 11:45:00
In my db table Layout, there's one column whose type is hierarchyid (column index=4). When trying to set-up new environment (a virtual web-server, created from XEN server), then running the site, I've met with this issue: Exception message: DataReader.GetFieldType(4) returned null. Exception data: System.Collections.ListDictionaryInternal I've made some search and found out there are already some topic on it (such as on MSDN ). But even when I added the C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Types.dll library, it seems like db type in structure

Reading a date using DataReader

强颜欢笑 提交于 2019-11-30 07:03:51
问题 I read a string using this format with a data reader. How can I read in a date using similar format? while (MyReader.Read()) { TextBox1.Text = (string)MyReader["Note"]; } 回答1: Try as given below: while (MyReader.Read()) { TextBox1.Text = Convert.ToDateTime(MyReader["DateField"]).ToString("dd/MM/yyyy"); } in ToString() method you can change data format as per your requirement. 回答2: If the query's column has an appropriate type then var dateString = MyReader.GetDateTime(MyReader.GetOrdinal(

SQL Server and SqlDataReader - Trillion Records - Memory

爱⌒轻易说出口 提交于 2019-11-30 05:07:36
问题 I've never tried this - so I don't know if I'd run into memory issues. But can a SqlDataReader read a trillion records? It's all streamed correct? I'm a little green to what the SQL/TDS protocol is doing under the covers. UPDATE Translate Trillion to mean very large number. I probably should have said something like 1 billion or 100 million. 回答1: Yes, that will stream... but I don't think you should actually try to do it. If you could read a million records per second (which sounds unlikely

Output parameters not readable when used with a DataReader

我们两清 提交于 2019-11-30 02:58:56
问题 When using a DataReader object to access data from a database (such as SQL Server) through stored procedures, any output parameter added to the Command object before executing are not being filled after reading. I can read row data just fine, as well as all input parameters, but not output ones. 回答1: This is due to the "by design" nature of DataReaders. Any parameters marked as ParameterDirection.Output won't be "filled" until the DataReader has been closed. While still open, all Output

WinRT No mapping for the Unicode character exists in the target multi-byte code page

青春壹個敷衍的年華 提交于 2019-11-30 01:53:25
问题 I am trying to read a file in my Windows 8 Store App. Here is a fragment of code I use to achieve this: if(file != null) { var stream = await file.OpenAsync(FileAccessMode.Read); var size = stream.Size; using(var inputStream = stream.GetInputStreamAt(0)) { DataReader dataReader = new DataReader(inputStream); uint numbytes = await dataReader.LoadAsync((uint)size); string text = dataReader.ReadString(numbytes); } } However, an exeption is thrown at line: string text = dataReader.ReadString

How to handle multiple ResultSets, each with multiple Rows? IDataReader.NextResult() ending Read()

天大地大妈咪最大 提交于 2019-11-29 13:12:56
How to handle multiple ResultSets, each with multiple Rows? The call to NextResult() breaks the while loop. Some of my SPs return multiple ResultSets. I'm handling these with NextResult() but when I do and my SP only has a single ResultSet, I see the while loop with Read() finishes leaving me with only the first Row. Without the call to NextResult() I get all the rows for the first ResultSet but of course the second and subsequent ResultSets don't get processed? using (IDataReader reader = storedProcedure.ExecuteReader( CommandBehavior.CloseConnection, parameterNames as string[], arguments)) {

Invalid attempt to call FieldCount when reader is closed

余生长醉 提交于 2019-11-29 12:34:15
The error above occurs when I try to do a dataReader.Read on the data recieved from the database. I know there are two rows in there so it isnt because no data actually exists. Could it be the CommandBehavior.CloseConnection, causing the problem? I was told you had to do this right after a ExecuteReader? Is this correct? try { _connection.Open(); using (_connection) { SqlCommand command = new SqlCommand("SELECT * FROM Structure", _connection); SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection); if (dataReader == null) return null; var newData = new List<Structure

DataReader ordinal-based lookups vs named lookups

情到浓时终转凉″ 提交于 2019-11-29 07:30:03
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? Microsoft recommends not calling GetOrdinal within a loop. That would include indirect calls with the string indexer. You can use GetOrdinal at the top of your loop put the ordinals in an array and have the

Reading a date using DataReader

房东的猫 提交于 2019-11-29 01:06:07
I read a string using this format with a data reader. How can I read in a date using similar format? while (MyReader.Read()) { TextBox1.Text = (string)MyReader["Note"]; } Sukhjeevan Try as given below: while (MyReader.Read()) { TextBox1.Text = Convert.ToDateTime(MyReader["DateField"]).ToString("dd/MM/yyyy"); } in ToString() method you can change data format as per your requirement. If the query's column has an appropriate type then var dateString = MyReader.GetDateTime(MyReader.GetOrdinal("column")).ToString(myDateFormat) If the query's column is actually a string then see other answers.

DataReader best-practices

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:38:22
Similar to this question , but the answers never really got around to what I want to know. Is there any standards around getting values from a DataReader? I.e., is this dataReader.GetString(dataReader.GetOrdinal("ColumnName")); considered better/worse/the same as this? (string) dataReader["ColumnName"]; Here is the way that I do it: Int32 ordinal = dataReader.GetOrdinal("ColumnName"); if (!dataReader.IsDBNull(ordinal)) yourString = dataReader.GetString(ordinal); It is important to check for DBNull like I have shown above because if the field is null in the DataReader it will throw an exception