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

How to (efficiently) convert (cast?) a SqlDataReader field to its corresponding c# type?

If a field allows nulls, don’t use regular primitive types. Use the C# nullable type and the as keyword. int? field_a = reader[“field_a”] as int?; string field_b = reader[“field_a”] as string; Adding a ? to any non-nullable C# type makes it “nullable”. Using the as keyword will attempt to cast an object to the specified … Read more

How to read multiple resultset from SqlDataReader? [duplicate]

Here you have a sample about how to handle multiple result sets with a data reader static void RetrieveMultipleResults(SqlConnection connection) { using (connection) { SqlCommand command = new SqlCommand( “SELECT CategoryID, CategoryName FROM dbo.Categories;” + “SELECT EmployeeID, LastName FROM dbo.Employees”, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); do { Console.WriteLine(“\t{0}\t{1}”, reader.GetName(0), reader.GetName(1)); while (reader.Read()) { Console.WriteLine(“\t{0}\t{1}”, … Read more

convert from SqlDataReader to JSON

If you want something that’ll convert to arbitrary JSON, you could convert by serializing it into a Dictionary(Of string, object) thusly: public IEnumerable<Dictionary<string, object>> Serialize(SqlDataReader reader) { var results = new List<Dictionary<string, object>>(); var cols = new List<string>(); for (var i = 0; i < reader.FieldCount; i++) cols.Add(reader.GetName(i)); while (reader.Read()) results.Add(SerializeRow(cols, reader)); return results; } … Read more

Multiples Table in DataReader

Try this because this will close connection ,data reader and command once task get over , so that this will not give datareader close exception Also do check like this if(reader.NextResult()) to check there is next result, using (SqlConnection connection = new SqlConnection(“connection string here”)) { using (SqlCommand command = new SqlCommand (“SELECT Column1 FROM … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)