Skip to content

Instantly share code, notes, and snippets.

@mrtank
Created February 9, 2018 14:51
Show Gist options
  • Save mrtank/194f76426d50d0c0646ccb12b9d5b4b7 to your computer and use it in GitHub Desktop.
Save mrtank/194f76426d50d0c0646ccb12b9d5b4b7 to your computer and use it in GitHub Desktop.
namespace SqlStuff
{
using System.Data.SqlClient;
using System;
using System.Collections.Generic;
public static class Class1
{
public static IEnumerable<IDictionary<String, Object>> ReadStuff(this SqlConnection connection)
{
string query = "SELECT * FROM dbo.test";
using (var command = new SqlCommand(query, connection))
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return ReadAsDictionary(reader);
}
}
}
private static IDictionary<String, Object> ReadAsDictionary(SqlDataReader reader)
{
IDictionary<String, Object> dataRecord = new Dictionary<String, Object>();
for (var i = 0; i < reader.FieldCount; i++)
{
dataRecord.Add(reader.GetName(i), reader.GetValue(i));
}
return dataRecord;
}
}
}
namespace SqlStuff
{
using System.Data.SqlClient;
using FluentAssertions;
using NUnit.Framework;
[TestFixture]
class TestIt
{
[Test]
public void do_it()
{
SqlConnection connection = new SqlConnection("Data Source=garai;Initial Catalog=testChina;Integrated Security=True");
connection.Open();
connection.ReadStuff().Should().HaveCountGreaterThan(1);
connection.ReadStuff().Should().HaveCountGreaterThan(1);
connection.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment