sqldatareader

MAX(id) using SqlDataReader C#

那年仲夏 提交于 2019-11-29 08:04:28
How do I change this: using (SqlCommand myCommand = myConnection.CreateCommand()) { myConnection.Open(); myCommand.CommandText = "SELECT FormID FROM tbl_Form"; using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) { int FormID = reader.GetInt32(reader.GetOrdinal("FormID")); MessageBox.Show(FormID.ToString()); } } } to get MAX(FormID) ? My natural tendency is to throw a MAX around FormID but I'm getting an IndexOutOfRange exception. BrokenGlass When you select the maximum id you shouldn't use a SqlDataReader - the query returns just one item, which by default is

How do I fill a DataTable using DataReader

你。 提交于 2019-11-29 07:40:31
I want to fill DataTable using DataReader. I have created object like this SqlDataReader dr = cmd.ExecuteReader(); if(dr.HasRows) { } If all you want is a ReadOnly DataTable for reporting or web, try this: conn = new SqlConnection(connString); string query = "SELECT * FROM Customers"; SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); DataTable dt = new DataTable(); dt.Load(dr); Credit where it's due: http://www.dotnetcurry.com/showarticle.aspx?ID=143 DataTable.load() can be used for a generic approach. do { var

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

Slow performance of SqlDataReader

不想你离开。 提交于 2019-11-29 04:13:32
I've query executing ~2 secs in MSSMS (returning 25K of rows) Same query used in .NET (sqlReader) exetuting few minutes! I've also tried to execute only reader (commented all code in while loop just leaving reader.Read() ) - still same! Any idea what's up? I'm not DBA and not priviledged to play with Profiler - will ask my DBA and let all know. In the meantime I'm noticed essential performance boost after adding " WITH RECOMPILE " param to SP I'm talking So, from my perspective it seems to be the case with execution plan... What do you think? [EDIT] Also what I've checked was performing below

Read boolean values from DB?

这一生的挚爱 提交于 2019-11-29 03:29:02
In C#, using SqlDataReader, is there a way to read a boolean value from the DB? while (reader.Read()) { destPath = reader["destination_path"].ToString(); destFile = reader["destination_file"].ToString(); createDir = reader["create_directory"].ToString(); deleteExisting = Convert.ToBoolean(reader["delete_existing"]); skipIFolderDate = reader["skipifolderdate"].ToString(); useTempFile = reader["useTempFile"].ToString(); password = reader["password"].ToString(); } In the code above, delete_existing is always a 1 or 0 in the DB. I read on MSDN that Convert.ToBoolean() does not accept a 1 or 0 as

Why use the GetOrdinal() Method of the SqlDataReader

限于喜欢 提交于 2019-11-29 03:13:01
What's the difference between reading a value from an SqlDataReader using this syntax: Dim reader As SqlClient.SqlDataReader reader("value").ToString() OR Dim reader As SqlClient.SqlDataReader reader.GetString(reader.GetOrdinal("value")) I think that the reason to use GetOrdinal() is so that you can cache the result and re-use it multiple times for performance. E.g. Dim reader As SqlClient.SqlDataReader int valueOrdinal = reader.GetOrdinal("value"); while ( ... ) { var value = reader.GetString(valueOrdinal); } joe GetOrdinal performs a case-sensitive lookup first. If it fails, a second case

What 'length' parameter should I pass to SqlDataReader.GetBytes()

家住魔仙堡 提交于 2019-11-28 23:12:26
I have a SqlDataReader and need to read a varbinary(max) column from it using the SqlDataReader.GetBytes() method. This method populates a byte array and therefore needs to know what length of data to read. This is where I get confused.. Clearly I want to read all the data that has been returned from the database in this row/column so what 'length' parameter should I pass? As far as I can see, the SqlDataReader doesn't provide any methods to discover what length of data is available, therefore this method seems fairly awkward to me. I'm tempted to just pass int.MaxValue here and forget about

SqlDataReader vs SqlDataAdapter: which one has the better performance for returning a DataTable?

跟風遠走 提交于 2019-11-28 19:53:30
问题 I want to know which one has the better performance for returning a DataTable . Here for SqlDataReader I use DataTable.Load(dr) Using SqlDataReader : public static DataTable populateUsingDataReader(string myQuery) { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(constring)) { SqlCommand cmd = new SqlCommand(myQuery, con); con.Open(); SqlDataReader dr = null; dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr.HasRows) { dt.Load(dr); } return dt; } }

how to check if a datareader is null or empty

 ̄綄美尐妖づ 提交于 2019-11-28 18:22:02
问题 I have a datareader that return a lsit of records from a sql server database. I have a field in the database called "Additional". This field is 50% of the time empty or null. I am trying to write code that checks if this field isnull. The logic behind this is: If the field "Additional" contains text then display the info otherwise hide the field. I have tried: if (myReader["Additional"] != null) { ltlAdditional.Text = "contains data"; } else { ltlAdditional.Text = "is null"; } The above code

How to display database records in asp.net mvc view

心已入冬 提交于 2019-11-28 17:59:26
Using ASP.NET MVC with C#, how do you pass some database records to a View and display them in table form? I need to know how I can transfer/pass some rows of records from a database that have been returned to an SqlDataReader object and pass that object to the View so I can display all the records contained by the object in the View using foreach. The following code is what I'm I'm trying to do. But its not working. The Controller: public ActionResult Students() { String connectionString = "<THE CONNECTION STRING HERE>"; String sql = "SELECT * FROM students"; SqlCommand cmd = new SqlCommand