Are there any benefits of reading each field async from a SqlDataReader?

After some peeking at reflector, the interesting methods here (GetFieldValueAsync<T>, IsDBNullAsync, and the internal method GetBytesAsync) only do “interesting” code for the CommandBehavior.SequentialAccess scenario. So: if you’re not using that: don’t bother – the row data is already buffered in memory, and Task<T> is pure overhead (although it will at least be an already-completed task … Read more

How to use executeReader() method to retrieve the value of just one cell

using (var conn = new SqlConnection(SomeConnectionString)) using (var cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = “SELECT * FROM learer WHERE id = @id”; cmd.Parameters.AddWithValue(“@id”, index); using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { learerLabel.Text = reader.GetString(reader.GetOrdinal(“somecolumn”)) } } }

How to write a .Net application that works with both SqlServer and Oracle (now that System.Data.OracleClient is deprecated)

EDIT: The fully managed ODP.NET is now available in production. It is very small (less than 10MB) and is not dependent on other files. You can obtain it here: http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html Original answer: One way to easily ensure that the required Oracle client side software (including ODP.NET) is always available on the deployment machine is to … Read more

Execute multiple SQL commands in one round trip

Something like this. The example is probably not very good as it doesn’t properly dispose objects but you get the idea. Here’s a cleaned up version: using (var connection = new SqlConnection(ConnectionString)) using (var command = connection.CreateCommand()) { connection.Open(); command.CommandText = “select id from test1; select id from test2”; using (var reader = command.ExecuteReader()) { … Read more

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

When dealing with varbinary(max), there are two scenarios: the length of the data is moderate the length of the data is big GetBytes() is intended for the second scenario, when you are using CommandBehaviour.SequentialAccess to ensure that you are streaming the data, not buffering it. In particular, in this usage you would usually be writing … Read more

Getting datarow values into a string?

You need to specify which column of the datarow you want to pull data from. Try the following: StringBuilder output = new StringBuilder(); foreach (DataRow rows in results.Tables[0].Rows) { foreach (DataColumn col in results.Tables[0].Columns) { output.AppendFormat(“{0} “, rows[col]); } output.AppendLine(); }

How do I translate a List into a SqlParameter for a Sql In statement? [duplicate]

You could try something like this: string sql = “SELECT dscr FROM system_settings WHERE setting IN ({0})”; string[] paramArray = settingList.Select((x, i) => “@settings” + i).ToArray(); cmd.CommandText = string.Format(sql, string.Join(“,”, paramArray)); for (int i = 0; i < settingList.Count; ++i) { cmd.Parameters.Add(new SqlParameter(“@settings” + i, settingList[i])); }