sqldatareader

Multiples Table in DataReader

*爱你&永不变心* 提交于 2019-11-26 14:40:53
问题 I normally use DataSet because It is very flexible. Recently I am assigned code optimization task , To reduce hits to the database I am changing two queries in a procedure. one Query returns the count and the other returns the actual data . That is , My stored procedure returns two tables. Now, I know how to read both tables using DataSets , But I need to read both tables using DataReader . In search of that I found This. I follow the article and wrote my code like this: dr = cmd

How to get nullable DateTime out of the database

放肆的年华 提交于 2019-11-26 14:28:14
问题 My SQL Server database contains nullable DateTime values. How can I convert them to a nullable DateTime object in my application in C#? This is what I would think it would look like, but it doesn't: DateTime? dt = (DateTime?) sqldatareader[0]; 回答1: A SQL null is not the same as a .NET null; you have to compare against System.DBNull.Value: object sqlDateTime = sqldatareader[0]; DateTime? dt = (sqlDateTime == System.DBNull.Value) ? (DateTime?)null : Convert.ToDateTime(sqlDateTime); In answer to

Why is DataTable faster than DataReader

人盡茶涼 提交于 2019-11-26 11:06:47
问题 So we have had a heated debate at work as to which DataAccess route to take: DataTable or DataReader. DISCLAIMER I am on the DataReader side and these results have shaken my world. We ended up writing some benchmarks to test the speed differences. It was generally agreed that a DataReader is faster, but we wanted to see how much faster. The results surprised us. The DataTable was consistently faster than the DataReader. Approaching twice as fast sometimes. So I turn to you, members of SO. Why

How to get number of rows using SqlDataReader in C#

六月ゝ 毕业季﹏ 提交于 2019-11-26 09:35:52
问题 My question is how to get the number of rows returned by a query using SqlDataReader in C#. I\'ve seen some answers about this but none were clearly defined except for one that states to do a while loop with Read() method and increment a counter. My problem is that I am trying to fill a multi-dimensional array with the first row being the column header names and every row after that to the be the row data. I know that I can just dump the stuff in a List control and not worry about it, but for

convert from SqlDataReader to JSON

半腔热情 提交于 2019-11-26 07:36:48
问题 public string toJSON(SqlDataReader o) { StringBuilder s = new StringBuilder(); s.Append(\"[\"); if (o.HasRows) while (o.Read()) s.Append(\"{\" + \'\"\' + \"Id\" + \'\"\' + \":\" + o[\"Id\"] + \", \" + \'\"\' + \"CN\" + \'\"\' + \":\" + o[\"CatName\"] + \", \" + \'\"\' + \"Ord\" + \'\"\' + \":\" + o[\"Ord\"] + \",\" + \'\"\' + \"Icon\" + \'\"\' + \":\" + o[\"Icon\"] + \"}, \"); s.Remove(s.Length - 2, 2); s.Append(\"]\"); o.Close(); return s.ToString(); } I\'m using here my own function to do

Can you get the column names from a SqlDataReader?

不想你离开。 提交于 2019-11-26 04:06:53
问题 After connecting to the database, can I get the name of all the columns that were returned in my SqlDataReader ? 回答1: var reader = cmd.ExecuteReader(); var columns = new List<string>(); for(int i=0;i<reader.FieldCount;i++) { columns.Add(reader.GetName(i)); } or var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList(); 回答2: There is a GetName function on the SqlDataReader which accepts the column index and returns the name of the column. Conversely, there is a

Read data from SqlDataReader

不打扰是莪最后的温柔 提交于 2019-11-26 01:45:02
问题 I have a SQL Server 2008 database and I am working on it in the backend. I am working on asp.net/C# SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { //how do I read strings here???? } I know that the reader has values. My SQL command is to select just 1 column from a table. The column contains strings ONLY. I want to read the strings (rows) in the reader one by one. How do I do this? 回答1: using(SqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var myString = rdr

SQL Data Reader - handling Null column values

亡梦爱人 提交于 2019-11-26 00:16:37
问题 I\'m using a SQLdatareader to build POCOs from a database. The code works except when it encounters a null value in the database. For example, if the FirstName column in the database contains a null value, an exception is thrown. employee.FirstName = sqlreader.GetString(indexFirstName); What is the best way to handle null values in this situation? 回答1: You need to check for IsDBNull : if(!SqlReader.IsDBNull(indexFirstName)) { employee.FirstName = sqlreader.GetString(indexFirstName); } That's

Read data from SqlDataReader

蓝咒 提交于 2019-11-25 19:08:12
I have a SQL Server 2008 database and I am working on it in the backend. I am working on asp.net/C# SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { //how do I read strings here???? } I know that the reader has values. My SQL command is to select just 1 column from a table. The column contains strings ONLY. I want to read the strings (rows) in the reader one by one. How do I do this? using(SqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var myString = rdr.GetString(0); //The 0 stands for "the 0'th column", so the first column of the result. // Do somthing with this