Created
February 9, 2018 14:51
-
-
Save mrtank/194f76426d50d0c0646ccb12b9d5b4b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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