├── Src ├── BuildNugetPackage.cmd ├── packages │ ├── NUnit.2.6.3 │ │ ├── license.txt │ │ └── lib │ │ │ └── nunit.framework.dll │ ├── Moq.4.2.1402.2112 │ │ └── lib │ │ │ ├── net35 │ │ │ └── Moq.dll │ │ │ ├── net40 │ │ │ └── Moq.dll │ │ │ └── sl4 │ │ │ └── Moq.Silverlight.dll │ └── repositories.config ├── DBHelpers.Tests │ ├── packages.config │ ├── InternalDBHelper.cs │ ├── DataReaderConverterTests.cs │ ├── TypeExtensions.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── MockProviderFactory.cs │ ├── DBHelpers.Tests.csproj │ ├── BaseOverloadTests.cs │ ├── DBConvertOverloadTests.cs │ ├── DBHelperTests.cs │ ├── DBConvertTests.cs │ └── DbHelperOverloadTests.cs ├── DBHelpers │ ├── DBHelpers.nuspec │ ├── RawValue.cs │ ├── DataReaderExtensions.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── DataReaderConverter.cs │ ├── DBHelpers.csproj │ ├── DBConvert.cs │ ├── DBHelper.StringOverloads.cs │ └── DBHelper.Core.cs └── DBHelpers.sln ├── .gitignore └── README.md /Src/BuildNugetPackage.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | nuget pack DBHelpers\DBHelpers.csproj -Prop Configuration=Release 3 | pause -------------------------------------------------------------------------------- /Src/packages/NUnit.2.6.3/license.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvivo/dbhelpers/HEAD/Src/packages/NUnit.2.6.3/license.txt -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.user 2 | *.suo 3 | *.bak 4 | *.~* 5 | *.lnk 6 | *.design 7 | *.log 8 | *.licenseheader 9 | bin 10 | obj 11 | Build 12 | *.nupkg -------------------------------------------------------------------------------- /Src/packages/Moq.4.2.1402.2112/lib/net35/Moq.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvivo/dbhelpers/HEAD/Src/packages/Moq.4.2.1402.2112/lib/net35/Moq.dll -------------------------------------------------------------------------------- /Src/packages/Moq.4.2.1402.2112/lib/net40/Moq.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvivo/dbhelpers/HEAD/Src/packages/Moq.4.2.1402.2112/lib/net40/Moq.dll -------------------------------------------------------------------------------- /Src/packages/NUnit.2.6.3/lib/nunit.framework.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvivo/dbhelpers/HEAD/Src/packages/NUnit.2.6.3/lib/nunit.framework.dll -------------------------------------------------------------------------------- /Src/packages/Moq.4.2.1402.2112/lib/sl4/Moq.Silverlight.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvivo/dbhelpers/HEAD/Src/packages/Moq.4.2.1402.2112/lib/sl4/Moq.Silverlight.dll -------------------------------------------------------------------------------- /Src/packages/repositories.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Src/DBHelpers/DBHelpers.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | DBHelpers 5 | $version$ 6 | DBHelpers 7 | Natan Vivo 8 | Natan Vivo 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | https://github.com/nvivo/dbhelpers 11 | false 12 | DBHelpers is a simple but powerful helper library for working with plain ADO.NET. 13 | 14 | Copyright 2010-2014 15 | dbhelpers dbhelper ado.net database helper 16 | 17 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/InternalDBHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | 4 | namespace DBHelpers.Tests 5 | { 6 | public class InternalDBHelper : DBHelper 7 | { 8 | public InternalDBHelper() 9 | : base(new MockProviderFactory(), "fake connection string") 10 | { } 11 | 12 | public string CreateParameterName_(int index) 13 | { 14 | return base.CreateParameterName(index); 15 | } 16 | 17 | public void FillFromReader_(DbDataReader reader, int startRecord, int maxRecords, Action action) 18 | { 19 | FillFromReader(reader, startRecord, maxRecords, action); 20 | } 21 | 22 | protected override void OnExecuteCommand(DbCommand command) 23 | { 24 | OnExecuteCommand_(command); 25 | } 26 | 27 | public void OnExecuteCommand_(DbCommand command) 28 | { 29 | base.OnExecuteCommand(command); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Src/DBHelpers/RawValue.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // Copyright 2010-2015 Natan Vivo - http://github.com/nvivo/dbhelpers 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | #endregion 16 | 17 | namespace DBHelpers 18 | { 19 | public class RawValue 20 | { 21 | public RawValue(string value) 22 | { 23 | this.Value = value; 24 | } 25 | 26 | public string Value { get; private set; } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/DataReaderConverterTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System.Data.Common; 3 | 4 | namespace DBHelpers.Tests 5 | { 6 | [TestFixture] 7 | public class DataReaderConverterTests 8 | { 9 | [Test] 10 | public void ConvertByName() 11 | { 12 | var reader = CreateReader(); 13 | reader.Read(); 14 | 15 | var converter = new DataReaderConverter(); 16 | 17 | var model = converter.Convert(reader); 18 | 19 | Assert.IsNotNull(model); 20 | Assert.AreEqual(reader["IntCol"], model.IntCol); 21 | Assert.AreEqual(reader["StringCol"], model.StringCol); 22 | Assert.AreEqual(reader["DateCol"], model.DateCol); 23 | Assert.IsNull(model.UnmappedCol); 24 | } 25 | 26 | private DbDataReader CreateReader() 27 | { 28 | var factory = new MockProviderFactory(); 29 | var reader = factory.CreateReader(); 30 | return reader; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/TypeExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace DBHelpers.Tests 6 | { 7 | public static class TypeExtensions 8 | { 9 | public static string[] GetSignatures(this Type type, string methodName, BindingFlags flags) 10 | { 11 | var methods = type.GetMethods(flags).Where(m => m.Name == methodName); 12 | 13 | var signatures = methods.Select(m => 14 | { 15 | var returnType = GetTypeName(m.ReturnType); 16 | var argumentTypes = m.GetParameters().Select(p => GetTypeName(p.ParameterType)); 17 | 18 | return String.Format("{0} {1}({2})", returnType, methodName, String.Join(", ", argumentTypes)); 19 | }); 20 | 21 | return signatures.ToArray(); 22 | } 23 | 24 | public static string GetTypeName(Type type) 25 | { 26 | if (type.IsGenericType) 27 | { 28 | var name = type.Name.Split('`')[0]; 29 | var genericArguments = type.GetGenericArguments().Select(t => GetTypeName(t)); 30 | 31 | return String.Format("{0}<{1}>", name, String.Join(", ", genericArguments)); 32 | } 33 | 34 | return type.Name; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Src/DBHelpers.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBHelpers", "DBHelpers\DBHelpers.csproj", "{DECB23E4-A87D-433F-9066-0428C2C6466E}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBHelpers.Tests", "DBHelpers.Tests\DBHelpers.Tests.csproj", "{C3A369D0-68CD-4DDF-8607-8AAAB62EB39D}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {DECB23E4-A87D-433F-9066-0428C2C6466E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {DECB23E4-A87D-433F-9066-0428C2C6466E}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {DECB23E4-A87D-433F-9066-0428C2C6466E}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {DECB23E4-A87D-433F-9066-0428C2C6466E}.Release|Any CPU.Build.0 = Release|Any CPU 18 | {C3A369D0-68CD-4DDF-8607-8AAAB62EB39D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {C3A369D0-68CD-4DDF-8607-8AAAB62EB39D}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {C3A369D0-68CD-4DDF-8607-8AAAB62EB39D}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {C3A369D0-68CD-4DDF-8607-8AAAB62EB39D}.Release|Any CPU.Build.0 = Release|Any CPU 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("DBHelper.Tests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("DBHelper.Tests")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("9939d625-e194-459b-beef-ddd0d6e76350")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Src/DBHelpers/DataReaderExtensions.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // Copyright 2010-2015 Natan Vivo - http://github.com/nvivo/dbhelpers 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | #endregion 16 | 17 | using System; 18 | using System.Data.Common; 19 | using System.Globalization; 20 | 21 | namespace DBHelpers 22 | { 23 | public static class DataReaderExtensions 24 | { 25 | public static T Get(this DbDataReader reader, int ordinal, IFormatProvider provider) 26 | { 27 | return DBConvert.To(reader[ordinal], provider); 28 | } 29 | 30 | public static T Get(this DbDataReader reader, int ordinal) 31 | { 32 | return DBConvert.To(reader[ordinal], CultureInfo.CurrentCulture); 33 | } 34 | 35 | public static T Get(this DbDataReader reader, string name, IFormatProvider provider) 36 | { 37 | return DBConvert.To(reader[name], provider); 38 | } 39 | 40 | public static T Get(this DbDataReader reader, string name) 41 | { 42 | return DBConvert.To(reader[name], CultureInfo.CurrentCulture); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Src/DBHelpers/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // Copyright 2010-2015 Natan Vivo - http://github.com/nvivo/dbhelpers 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | #endregion 16 | 17 | using System.Reflection; 18 | using System.Runtime.CompilerServices; 19 | using System.Runtime.InteropServices; 20 | 21 | // General Information about an assembly is controlled through the following 22 | // set of attributes. Change these attribute values to modify the information 23 | // associated with an assembly. 24 | [assembly: AssemblyTitle("DBHelpers")] 25 | [assembly: AssemblyDescription("DBHelpers")] 26 | [assembly: AssemblyProduct("DBHelpers")] 27 | [assembly: AssemblyCopyright("Copyright © Natan Vivo 2010-2015")] 28 | 29 | // Setting ComVisible to false makes the types in this assembly not visible 30 | // to COM components. If you need to access a type in this assembly from 31 | // COM, set the ComVisible attribute to true on that type. 32 | [assembly: ComVisible(false)] 33 | 34 | // The following GUID is for the ID of the typelib if this project is exposed to COM 35 | [assembly: Guid("8874ddf4-cd68-49fe-83c1-753d7f3f99c6")] 36 | 37 | // Version information for an assembly consists of the following four values: 38 | // 39 | // Major Version 40 | // Minor Version 41 | // Build Number 42 | // Revision 43 | // 44 | // You can specify all the values or you can default the Build and Revision Numbers 45 | // by using the '*' as shown below: 46 | // [assembly: AssemblyVersion("1.0.*")] 47 | [assembly: AssemblyVersion("1.1.0")] 48 | [assembly: AssemblyFileVersion("1.1.0")] 49 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/MockProviderFactory.cs: -------------------------------------------------------------------------------- 1 | using Moq; 2 | using Moq.Protected; 3 | using System; 4 | using System.Data; 5 | using System.Data.Common; 6 | 7 | namespace DBHelpers.Tests 8 | { 9 | public class MockProviderFactory : DbProviderFactory 10 | { 11 | public class DataReaderModel 12 | { 13 | public DateTime DateCol { get; set; } 14 | public int IntCol { get; set; } 15 | public string StringCol { get; set; } 16 | public string UnmappedCol { get; set; } 17 | } 18 | 19 | public Func NewConnection = () => new Mock().Object; 20 | public Func NewParameter = () => new Mock().Object; 21 | public Func NewDataAdapter = () => new Mock().Object; 22 | 23 | public override DbCommand CreateCommand() 24 | { 25 | var command = new Mock(); 26 | 27 | command 28 | .Protected() 29 | .Setup("ExecuteDbDataReader", ItExpr.IsAny()) 30 | .Returns(() => CreateReader()); 31 | 32 | return command.Object; 33 | } 34 | 35 | public override DbCommandBuilder CreateCommandBuilder() 36 | { 37 | var mock = new Mock(); 38 | 39 | mock.Protected() 40 | .Setup("GetParameterPlaceholder", ItExpr.IsAny()) 41 | .Returns((int i) => "$p" + i); 42 | 43 | return mock.Object; 44 | } 45 | 46 | public override DbConnection CreateConnection() 47 | { 48 | return NewConnection(); 49 | } 50 | 51 | public override DbDataAdapter CreateDataAdapter() 52 | { 53 | return NewDataAdapter(); 54 | } 55 | 56 | public override DbParameter CreateParameter() 57 | { 58 | return NewParameter(); 59 | } 60 | 61 | public DbDataReader CreateReader() 62 | { 63 | var dt = new DataTable(); 64 | dt.Columns.Add("IntCol", typeof(int)); 65 | dt.Columns.Add("StringCol", typeof(string)); 66 | dt.Columns.Add("DateCol", typeof(DateTime)); 67 | 68 | for (int i = 0; i < 50; i++) 69 | dt.Rows.Add(i, "row " + i, DateTime.Today.AddDays(i)); 70 | 71 | return dt.CreateDataReader(); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Src/DBHelpers/DataReaderConverter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // Copyright 2010-2015 Natan Vivo - http://github.com/nvivo/dbhelpers 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | #endregion 16 | 17 | using System.Collections.Generic; 18 | using System.Data.Common; 19 | using System.Reflection; 20 | 21 | namespace DBHelpers 22 | { 23 | public class DataReaderConverter where T : new() 24 | { 25 | private class Mapping 26 | { 27 | public int Index; 28 | public PropertyInfo Property; 29 | } 30 | 31 | private Mapping[] _mappings; 32 | private DbDataReader _lastReader; 33 | 34 | public T Convert(DbDataReader reader) 35 | { 36 | if (_mappings == null || reader != _lastReader) 37 | _mappings = MapProperties(reader); 38 | 39 | var o = new T(); 40 | 41 | foreach (var mapping in _mappings) 42 | { 43 | var prop = mapping.Property; 44 | var rawValue = reader.GetValue(mapping.Index); 45 | var value = DBConvert.To(prop.PropertyType, rawValue); 46 | prop.SetValue(o, value, null); 47 | } 48 | 49 | _lastReader = reader; 50 | 51 | return o; 52 | } 53 | 54 | private Mapping[] MapProperties(DbDataReader reader) 55 | { 56 | var fieldCount = reader.FieldCount; 57 | 58 | var fields = new Dictionary(fieldCount); 59 | 60 | for (var i = 0; i < fieldCount; i++) 61 | fields.Add(reader.GetName(i).ToLowerInvariant(), i); 62 | 63 | var type = typeof(T); 64 | 65 | var mapping = new List(fieldCount); 66 | 67 | foreach (var prop in type.GetProperties()) 68 | { 69 | var name = prop.Name.ToLowerInvariant(); 70 | 71 | int index; 72 | 73 | if (fields.TryGetValue(name, out index)) 74 | mapping.Add(new Mapping() { Index = index, Property = prop }); 75 | } 76 | 77 | return mapping.ToArray(); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Src/DBHelpers/DBHelpers.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {DECB23E4-A87D-433F-9066-0428C2C6466E} 8 | Library 9 | Properties 10 | DBHelpers 11 | DBHelpers 12 | v4.5 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | false 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | false 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 60 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/DBHelpers.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {C3A369D0-68CD-4DDF-8607-8AAAB62EB39D} 8 | Library 9 | Properties 10 | DBHelpers.Tests 11 | DBHelpers.Tests 12 | v4.5 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | false 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | false 34 | 35 | 36 | 37 | False 38 | ..\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll 39 | 40 | 41 | ..\packages\NUnit.2.6.3\lib\nunit.framework.dll 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | {decb23e4-a87d-433f-9066-0428c2c6466e} 69 | DBHelpers 70 | 71 | 72 | 73 | 74 | 75 | 76 | 83 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/BaseOverloadTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace DBHelpers.Tests 8 | { 9 | public abstract class BaseOverloadTests 10 | { 11 | protected virtual IEnumerable GetRequiredInstanceSignatures() 12 | { 13 | return null; 14 | } 15 | 16 | protected virtual IEnumerable GetRequiredStaticSignatures() 17 | { 18 | return null; 19 | } 20 | 21 | protected virtual IEnumerable GetExistingInstanceSignatures() 22 | { 23 | return null; 24 | } 25 | 26 | protected virtual IEnumerable GetExistingStaticSignatures() 27 | { 28 | return null; 29 | } 30 | 31 | [TestCaseSource("RequiredInstanceSignaturesTestSource")] 32 | public void RequiredInstanceSignaturesExist(bool pass) 33 | { 34 | Assert.That(pass, "The signature does not exists when it should."); 35 | } 36 | 37 | [TestCaseSource("RequiredStaticSignaturesTestSource")] 38 | public void RequiredStaticSignaturesExist(bool pass) 39 | { 40 | Assert.That(pass, "The signature does not exists when it should."); 41 | } 42 | 43 | [TestCaseSource("TestedInstanceSignaturesTestSource")] 44 | public void TestedInstanceSignatures(bool pass) 45 | { 46 | Assert.That(pass, "Public instance signature exists but is not required."); 47 | } 48 | 49 | [TestCaseSource("TestedStaticSignaturesTestSource")] 50 | public void TestedStaticSignatures(bool pass) 51 | { 52 | Assert.That(pass, "Public static signature exists but is not required."); 53 | } 54 | 55 | public IEnumerable RequiredInstanceSignaturesTestSource() 56 | { 57 | var existing = GetExistingInstanceSignatures() ?? new string[0]; 58 | var required = GetRequiredInstanceSignatures() ?? new string[0]; 59 | 60 | if (required.Count() == 0) 61 | yield return new TestCaseData(true).SetName("No required signatures to test."); 62 | 63 | foreach (var sig in required) 64 | { 65 | bool pass = existing.Contains(sig); 66 | yield return new TestCaseData(pass).SetName(sig); 67 | } 68 | } 69 | 70 | protected IEnumerable TestedInstanceSignaturesTestSource() 71 | { 72 | var existing = GetExistingInstanceSignatures() ?? new string[0]; 73 | var required = GetRequiredInstanceSignatures() ?? new string[0]; 74 | 75 | if (required.Count() == 0) 76 | yield return new TestCaseData(true).SetName("No required signatures to test."); 77 | 78 | foreach (var sig in existing) 79 | { 80 | bool pass = required.Contains(sig); 81 | yield return new TestCaseData(pass).SetName(sig); 82 | } 83 | } 84 | 85 | protected IEnumerable RequiredStaticSignaturesTestSource() 86 | { 87 | var existing = GetExistingStaticSignatures() ?? new string[0]; 88 | var required = GetRequiredStaticSignatures() ?? new string[0]; 89 | 90 | if (required.Count() == 0) 91 | yield return new TestCaseData(true).SetName("No required signatures to test."); 92 | 93 | foreach (var sig in required) 94 | { 95 | bool pass = existing.Contains(sig); 96 | yield return new TestCaseData(pass).SetName(sig); 97 | } 98 | } 99 | 100 | protected IEnumerable TestedStaticSignaturesTestSource() 101 | { 102 | var existing = GetExistingStaticSignatures() ?? new string[0]; 103 | var required = GetRequiredStaticSignatures() ?? new string[0]; 104 | 105 | if (required.Count() == 0) 106 | yield return new TestCaseData(true).SetName("No required signatures to test."); 107 | 108 | foreach (var sig in existing) 109 | { 110 | bool pass = required.Contains(sig); 111 | yield return new TestCaseData(pass).SetName(sig); 112 | } 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/DBConvertOverloadTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | 7 | namespace DBHelpers.Tests 8 | { 9 | public class DBConvertOverloadTests : BaseOverloadTests 10 | { 11 | protected override IEnumerable GetRequiredStaticSignatures() 12 | { 13 | return new string[] { 14 | "Boolean ToBoolean(Object)", 15 | "Boolean ToBoolean(Object, IFormatProvider)", 16 | 17 | "SByte ToSByte(Object)", 18 | "SByte ToSByte(Object, IFormatProvider)", 19 | 20 | "Int16 ToInt16(Object)", 21 | "Int16 ToInt16(Object, IFormatProvider)", 22 | 23 | "Int32 ToInt32(Object)", 24 | "Int32 ToInt32(Object, IFormatProvider)", 25 | 26 | "Int64 ToInt64(Object)", 27 | "Int64 ToInt64(Object, IFormatProvider)", 28 | 29 | "Byte ToByte(Object)", 30 | "Byte ToByte(Object, IFormatProvider)", 31 | 32 | "UInt16 ToUInt16(Object)", 33 | "UInt16 ToUInt16(Object, IFormatProvider)", 34 | 35 | "UInt32 ToUInt32(Object)", 36 | "UInt32 ToUInt32(Object, IFormatProvider)", 37 | 38 | "UInt64 ToUInt64(Object)", 39 | "UInt64 ToUInt64(Object, IFormatProvider)", 40 | 41 | "Decimal ToDecimal(Object)", 42 | "Decimal ToDecimal(Object, IFormatProvider)", 43 | 44 | "Single ToSingle(Object)", 45 | "Single ToSingle(Object, IFormatProvider)", 46 | 47 | "Double ToDouble(Object)", 48 | "Double ToDouble(Object, IFormatProvider)", 49 | 50 | "Char ToChar(Object)", 51 | "Char ToChar(Object, IFormatProvider)", 52 | 53 | "DateTime ToDateTime(Object)", 54 | "DateTime ToDateTime(Object, IFormatProvider)", 55 | 56 | "Guid ToGuid(Object)", 57 | 58 | "Nullable ToNullableBoolean(Object)", 59 | "Nullable ToNullableBoolean(Object, IFormatProvider)", 60 | 61 | "Nullable ToNullableSByte(Object)", 62 | "Nullable ToNullableSByte(Object, IFormatProvider)", 63 | 64 | "Nullable ToNullableInt16(Object)", 65 | "Nullable ToNullableInt16(Object, IFormatProvider)", 66 | 67 | "Nullable ToNullableInt32(Object)", 68 | "Nullable ToNullableInt32(Object, IFormatProvider)", 69 | 70 | "Nullable ToNullableInt64(Object)", 71 | "Nullable ToNullableInt64(Object, IFormatProvider)", 72 | 73 | "Nullable ToNullableByte(Object)", 74 | "Nullable ToNullableByte(Object, IFormatProvider)", 75 | 76 | "Nullable ToNullableUInt16(Object)", 77 | "Nullable ToNullableUInt16(Object, IFormatProvider)", 78 | 79 | "Nullable ToNullableUInt32(Object)", 80 | "Nullable ToNullableUInt32(Object, IFormatProvider)", 81 | 82 | "Nullable ToNullableUInt64(Object)", 83 | "Nullable ToNullableUInt64(Object, IFormatProvider)", 84 | 85 | "Nullable ToNullableDecimal(Object)", 86 | "Nullable ToNullableDecimal(Object, IFormatProvider)", 87 | 88 | "Nullable ToNullableSingle(Object)", 89 | "Nullable ToNullableSingle(Object, IFormatProvider)", 90 | 91 | "Nullable ToNullableDouble(Object)", 92 | "Nullable ToNullableDouble(Object, IFormatProvider)", 93 | 94 | "Nullable ToNullableChar(Object)", 95 | "Nullable ToNullableChar(Object, IFormatProvider)", 96 | 97 | "Nullable ToNullableDateTime(Object)", 98 | "Nullable ToNullableDateTime(Object, IFormatProvider)", 99 | 100 | "Nullable ToNullableGuid(Object)", 101 | 102 | "String ToString(Object)", 103 | "String ToString(Object, IFormatProvider)", 104 | 105 | "Object To(Type, Object)", 106 | "Object To(Type, Object, IFormatProvider)", 107 | 108 | "T To(Object)", 109 | "T To(Object, IFormatProvider)", 110 | 111 | "Object ToDBValue(Object)" 112 | }; 113 | } 114 | 115 | protected override IEnumerable GetExistingStaticSignatures() 116 | { 117 | string[] methods = { 118 | "ToBoolean", 119 | "ToSByte", 120 | "ToInt16", 121 | "ToInt32", 122 | "ToInt64", 123 | "ToByte", 124 | "ToUInt16", 125 | "ToUInt32", 126 | "ToUInt64", 127 | "ToDecimal", 128 | "ToSingle", 129 | "ToDouble", 130 | "ToChar", 131 | "ToDateTime", 132 | "ToGuid", 133 | "ToNullableBoolean", 134 | "ToNullableSByte", 135 | "ToNullableInt16", 136 | "ToNullableInt32", 137 | "ToNullableInt64", 138 | "ToNullableByte", 139 | "ToNullableUInt16", 140 | "ToNullableUInt32", 141 | "ToNullableUInt64", 142 | "ToNullableDecimal", 143 | "ToNullableSingle", 144 | "ToNullableDouble", 145 | "ToNullableChar", 146 | "ToNullableDateTime", 147 | "ToNullableGuid", 148 | "ToString", 149 | "To", 150 | "ToDBValue", 151 | }; 152 | 153 | var type = typeof(DBConvert); 154 | 155 | foreach (var method in methods) 156 | foreach (var signature in type.GetSignatures(method, BindingFlags.Public | BindingFlags.Static)) 157 | yield return signature; 158 | } 159 | 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/DBHelperTests.cs: -------------------------------------------------------------------------------- 1 | using Moq; 2 | using Moq.Protected; 3 | using NUnit.Framework; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Data; 7 | using System.Data.Common; 8 | using System.Data.SqlClient; 9 | 10 | namespace DBHelpers.Tests 11 | { 12 | [TestFixture] 13 | public class DBHelperTests 14 | { 15 | private InternalDBHelper DB = new InternalDBHelper(); 16 | 17 | [Test] 18 | public void CreateCommand_NoParameters() 19 | { 20 | var db = NewSqlHelper(); 21 | 22 | var commandText = "select * from table"; 23 | var command = db.CreateCommand(commandText); 24 | 25 | Assert.AreEqual(commandText, command.CommandText); 26 | Assert.AreEqual(CommandType.Text, command.CommandType); 27 | Assert.AreEqual(0, command.Parameters.Count); 28 | } 29 | 30 | [Test] 31 | public void CreateCommand_WithParameters() 32 | { 33 | var db = NewSqlHelper(); 34 | 35 | var commandText = "text {0}, {1}, {2}, {3}"; 36 | var p0 = 1; 37 | var p1 = "foo"; 38 | var p2 = DateTime.Now; 39 | var p3 = new RawValue("(raw text)"); 40 | var command = db.CreateCommand(commandText, p0, p1, p2, p3); 41 | 42 | Assert.AreEqual("text @p0, @p1, @p2, (raw text)", command.CommandText); 43 | Assert.AreEqual(CommandType.Text, command.CommandType); 44 | Assert.AreEqual(3, command.Parameters.Count); 45 | Assert.AreEqual(p0, command.Parameters[0].Value); 46 | Assert.AreEqual(p1, command.Parameters[1].Value); 47 | Assert.AreEqual(p2, command.Parameters[2].Value); 48 | } 49 | 50 | [Test] 51 | public void CreateCommand_NullValuesSentAsDBNull() 52 | { 53 | var db = NewSqlHelper(); 54 | 55 | var command = db.CreateCommand("text {0}", new string[] { null }); 56 | 57 | Assert.AreEqual(DBNull.Value, command.Parameters[0].Value); 58 | } 59 | 60 | [Test] 61 | public void CreateParameterName() 62 | { 63 | var param = DB.CreateParameterName_(123); 64 | Assert.AreEqual("$p123", param); 65 | } 66 | 67 | [Test] 68 | public void ExecuteArray() 69 | { 70 | var command = DB.Factory.CreateCommand(); 71 | var arr = DB.ExecuteArray(command); 72 | 73 | Assert.AreEqual(50, arr.Length); 74 | } 75 | 76 | [Test] 77 | public void ExecuteDataSet() 78 | { 79 | var adapter = new Mock(); 80 | var factory = new MockProviderFactory() { NewDataAdapter = () => adapter.Object }; 81 | 82 | adapter.Setup(a => a.Fill(It.IsAny())); 83 | var db = new DBHelper(factory, "cn"); 84 | 85 | var command = db.Factory.CreateCommand(); 86 | db.ExecuteDataSet(command); 87 | 88 | adapter.VerifyAll(); 89 | } 90 | 91 | [Test] 92 | public void ExecuteDataTable() 93 | { 94 | Assert.Inconclusive("No simple way to check for DbDataAdapter.Fill(DataTable)."); 95 | } 96 | 97 | [Test] 98 | public void ExecuteDictionary() 99 | { 100 | var command = DB.Factory.CreateCommand(); 101 | var dict = DB.ExecuteDictionary(command); 102 | 103 | Assert.AreEqual(50, dict.Count); 104 | } 105 | 106 | [Test] 107 | public void ExecuteList() 108 | { 109 | var command = DB.Factory.CreateCommand(); 110 | var list = DB.ExecuteList(command); 111 | 112 | Assert.IsNotNull(list); 113 | Assert.AreEqual(50, list.Count); 114 | } 115 | 116 | [Test] 117 | public void ExecuteNonQuery() 118 | { 119 | var mockCommand = new Mock(); 120 | 121 | mockCommand.SetupSet(c => c.Connection = It.IsAny()); 122 | mockCommand.Setup(c => c.ExecuteNonQuery()); 123 | 124 | DB.ExecuteNonQuery(mockCommand.Object); 125 | 126 | mockCommand.VerifyAll(); 127 | } 128 | 129 | [Test] 130 | public void ExecuteObject() 131 | { 132 | var command = DB.Factory.CreateCommand(); 133 | var obj = DB.ExecuteObject(command); 134 | 135 | Assert.IsNotNull(obj); 136 | Assert.AreEqual(0, obj.IntCol); 137 | Assert.AreEqual("row 0", obj.StringCol); 138 | Assert.AreEqual(DateTime.Today, obj.DateCol); 139 | } 140 | 141 | [Test] 142 | public void ExecuteReader_WithoutConnectionParameter_ShouldCloseConnection() 143 | { 144 | var mockCommand = new Mock(); 145 | 146 | mockCommand.SetupSet(c => c.Connection = It.IsAny()); 147 | mockCommand.Protected().Setup("ExecuteDbDataReader", CommandBehavior.CloseConnection); 148 | 149 | var reader = DB.ExecuteReader(mockCommand.Object); 150 | 151 | mockCommand.VerifyAll(); 152 | } 153 | 154 | [Test] 155 | public void ExecuteReader_WithConnectionParameter_ShouldNotCloseConnection() 156 | { 157 | var mockCommand = new Mock(); 158 | var mockConnection = new Mock(); 159 | 160 | mockCommand.Protected().Setup("ExecuteDbDataReader", ItExpr.Is(v => v != CommandBehavior.CloseConnection)); 161 | DB.ExecuteReader(mockCommand.Object, mockConnection.Object); 162 | 163 | mockCommand.VerifyAll(); 164 | } 165 | 166 | [Test] 167 | public void ExecuteScalar() 168 | { 169 | var mockCommand = new Mock(); 170 | var expectedValue = 10; 171 | 172 | mockCommand.SetupSet(c => c.Connection = It.IsAny()); 173 | mockCommand.Setup(c => c.ExecuteScalar()).Returns(expectedValue); 174 | 175 | var value = DB.ExecuteScalar(mockCommand.Object); 176 | 177 | mockCommand.VerifyAll(); 178 | Assert.AreEqual(expectedValue, value); 179 | } 180 | 181 | [TestCaseSource("FillFromReaderSource")] 182 | public void FillFromReader(int startRecord, int maxRecords, int expectedCount, int firstValue) 183 | { 184 | var factory = (MockProviderFactory)DB.Factory; 185 | var reader = factory.CreateReader(); 186 | 187 | var list = new List(); 188 | 189 | DB.FillFromReader_(reader, startRecord, maxRecords, r => list.Add(r.GetInt32(0))); 190 | 191 | Assert.AreEqual(expectedCount, list.Count, "Invalid row count."); 192 | 193 | if (list.Count > 0) 194 | Assert.AreEqual(firstValue, list[0], "Invalid first value."); 195 | } 196 | 197 | [ExpectedException(typeof(ArgumentOutOfRangeException))] 198 | public void FillFromReader(int startRecord, int maxRecords) 199 | { 200 | var factory = (MockProviderFactory)DB.Factory; 201 | var reader = factory.CreateReader(); 202 | 203 | DB.FillFromReader_(reader, startRecord, maxRecords, r => { }); 204 | } 205 | 206 | public IEnumerable FillFromReaderSource() 207 | { 208 | var dataSets = new[] 209 | { 210 | // startRecord, maxRecords, expectedCount, firstValue 211 | new [] {0, 0, 50, 0}, 212 | new [] {0, -1, 50, 0}, 213 | new [] {51, 10, 0, 0}, 214 | new [] {0, 100, 50, 0}, 215 | new [] {10, 20, 20, 10}, 216 | new [] {45, 10, 5, 45} 217 | }; 218 | 219 | foreach (var arr in dataSets) 220 | { 221 | string name = String.Format("Start {0}, maxRecords {1} should return {2}", arr[0], arr[1], arr[2]); 222 | 223 | int expectedCount = arr[2]; 224 | 225 | if (expectedCount > 0) 226 | name += " with first index " + arr[3]; 227 | 228 | yield return new TestCaseData(arr[0], arr[1], arr[2], arr[3]).SetName(name); 229 | } 230 | } 231 | 232 | private InternalDBHelper NewMockHelper() 233 | { 234 | return new InternalDBHelper(); 235 | } 236 | 237 | private DBHelper NewSqlHelper() 238 | { 239 | return new DBHelper(SqlClientFactory.Instance, "cn"); 240 | } 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/DBConvertTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | 8 | namespace DBHelpers.Tests 9 | { 10 | [TestFixture] 11 | public class DBConvertTests 12 | { 13 | [TestCaseSource("NumericConversionCases")] 14 | public void ConvertTo_NumericsFromNumericsInRange_ReturnsCastValue(Type targetType, object sourceValue) 15 | { 16 | var value = DBConvert.To(targetType, sourceValue); 17 | Assert.IsInstanceOf(targetType, value); 18 | } 19 | 20 | [TestCaseSource("NumericTypes")] 21 | [TestCaseSource("NonNumericTypes")] 22 | public void ConvertTo_FromNull_ReturnsDefaultValue(Type targetType) 23 | { 24 | object reference = targetType.IsValueType ? Activator.CreateInstance(targetType) : null; 25 | object value = DBConvert.To(targetType, (object)null); 26 | Assert.AreEqual(reference, value); 27 | } 28 | 29 | [TestCaseSource("NumericTypes")] 30 | [TestCaseSource("NonNumericTypes")] 31 | public void ConvertTo_FromDBNull_ReturnsDefaultValue(Type targetType) 32 | { 33 | object reference = targetType.IsValueType ? Activator.CreateInstance(targetType) : null; 34 | object value = DBConvert.To(targetType, DBNull.Value); 35 | Assert.AreEqual(reference, value); 36 | } 37 | 38 | [TestCaseSource("NumericValues")] 39 | [TestCaseSource("NonNumericValues")] 40 | public void ConvertTo_String_ReturnsNonEmptyValue(object value) 41 | { 42 | string s = DBConvert.To(value); 43 | Assert.IsNotEmpty(s); 44 | } 45 | 46 | [TestCase('A')] 47 | [TestCase("A")] 48 | [TestCase("ABC")] 49 | public void ConvertTo_Char_ReturnsCharOrFirstChar(object value) 50 | { 51 | var c = DBConvert.To(value); 52 | Assert.AreEqual('A', c); 53 | } 54 | 55 | [TestCase("2000-12-31T23:59:59")] 56 | [TestCase("2000-12-31 23:59:59")] 57 | public void ConvertTo_DateTime_ReturnsDateTime(object value) 58 | { 59 | var reference = new DateTime(2000, 12, 31, 23, 59, 59); 60 | var dt = DBConvert.To(value); 61 | Assert.AreEqual(reference, dt); 62 | } 63 | 64 | [TestCase("2000-12-31T23:59:59+03:00")] 65 | [TestCase("2000-12-31 23:59:59+03:00")] 66 | public void ConvertTo_DateTimeOffset_ReturnsDateTimeOffset(object value) 67 | { 68 | var reference = new DateTimeOffset(2000, 12, 31, 23, 59, 59, TimeSpan.FromHours(3)); 69 | var dt = DBConvert.To(value); 70 | Assert.AreEqual(reference, dt); 71 | } 72 | 73 | [TestCase("2000-12-31")] 74 | public void ConvertTo_DateTime_ReturnsDate(object value) 75 | { 76 | var reference = new DateTime(2000, 12, 31); 77 | var dt = DBConvert.To(value); 78 | Assert.AreEqual(reference, dt); 79 | } 80 | 81 | [TestCase("5033b416-9f63-4370-a435-1c0c7c102e67")] 82 | [TestCase("5033b4169f634370a4351c0c7c102e67")] 83 | public void ConvertTo_Guid_ReturnsGuid(object value) 84 | { 85 | var reference = Guid.Parse("5033b416-9f63-4370-a435-1c0c7c102e67"); 86 | var guid = DBConvert.To(value); 87 | 88 | Assert.AreEqual(reference, guid); 89 | } 90 | 91 | [TestCase(new byte[] { 0, 1, 2 })] 92 | public void ConvertTo_ByteArray_ReturnsByteArray(object value) 93 | { 94 | var arr = DBConvert.To(value); 95 | 96 | Assert.NotNull(arr); 97 | Assert.AreEqual(arr.Length, 3); 98 | } 99 | 100 | [Test] 101 | public void Convert_DBNullToNullable_ReturnsNull() 102 | { 103 | var value = DBNull.Value; 104 | 105 | Assert.IsNull(DBConvert.ToNullableSByte(value), "ToNullableSByte"); 106 | Assert.IsNull(DBConvert.ToNullableInt16(value), "ToNullableInt16"); 107 | Assert.IsNull(DBConvert.ToNullableInt32(value), "ToNullableInt32"); 108 | Assert.IsNull(DBConvert.ToNullableInt64(value), "ToNullableInt64"); 109 | 110 | Assert.IsNull(DBConvert.ToNullableByte(value), "ToNullableByte"); 111 | Assert.IsNull(DBConvert.ToNullableUInt16(value), "ToNullableUInt16"); 112 | Assert.IsNull(DBConvert.ToNullableUInt32(value), "ToNullableUInt32"); 113 | Assert.IsNull(DBConvert.ToNullableUInt64(value), "ToNullableUInt64"); 114 | 115 | Assert.IsNull(DBConvert.ToNullableSingle(value), "ToNullableSingle"); 116 | Assert.IsNull(DBConvert.ToNullableDouble(value), "ToNullableDouble"); 117 | Assert.IsNull(DBConvert.ToNullableDecimal(value), "ToNullableDecimal"); 118 | Assert.IsNull(DBConvert.ToNullableGuid(value), "ToNullableGuid"); 119 | 120 | Assert.IsNull(DBConvert.ToNullableBoolean(value), "ToNullableBoolean"); 121 | Assert.IsNull(DBConvert.ToNullableChar(value), "ToNullableChar"); 122 | } 123 | 124 | [Test] 125 | public void Convert_NullToNullable_ReturnsNull() 126 | { 127 | var value = (object) null; 128 | 129 | Assert.IsNull(DBConvert.ToNullableSByte(value), "ToNullableSByte"); 130 | Assert.IsNull(DBConvert.ToNullableInt16(value), "ToNullableInt16"); 131 | Assert.IsNull(DBConvert.ToNullableInt32(value), "ToNullableInt32"); 132 | Assert.IsNull(DBConvert.ToNullableInt64(value), "ToNullableInt64"); 133 | 134 | Assert.IsNull(DBConvert.ToNullableByte(value), "ToNullableByte"); 135 | Assert.IsNull(DBConvert.ToNullableUInt16(value), "ToNullableUInt16"); 136 | Assert.IsNull(DBConvert.ToNullableUInt32(value), "ToNullableUInt32"); 137 | Assert.IsNull(DBConvert.ToNullableUInt64(value), "ToNullableUInt64"); 138 | 139 | Assert.IsNull(DBConvert.ToNullableSingle(value), "ToNullableSingle"); 140 | Assert.IsNull(DBConvert.ToNullableDouble(value), "ToNullableDouble"); 141 | Assert.IsNull(DBConvert.ToNullableDecimal(value), "ToNullableDecimal"); 142 | Assert.IsNull(DBConvert.ToNullableGuid(value), "ToNullableGuid"); 143 | 144 | Assert.IsNull(DBConvert.ToNullableBoolean(value), "ToNullableBoolean"); 145 | Assert.IsNull(DBConvert.ToNullableChar(value), "ToNullableChar"); 146 | } 147 | 148 | [TestCase(true)] 149 | [TestCase("true")] 150 | [TestCase("TRUE")] 151 | [TestCase(1)] 152 | [TestCase('1')] 153 | [TestCase("1")] 154 | public void ConvertTo_Bool_ReturnsTrue(object value) 155 | { 156 | var b = DBConvert.To(value); 157 | Assert.AreEqual(true, b); 158 | } 159 | 160 | [TestCase(false)] 161 | [TestCase("false")] 162 | [TestCase("FALSE")] 163 | [TestCase(0)] 164 | [TestCase('0')] 165 | [TestCase("0")] 166 | public void ConvertTo_Bool_ReturnsFalse(object value) 167 | { 168 | var b = DBConvert.To(value); 169 | Assert.AreEqual(false, b); 170 | } 171 | 172 | private Type[] NumericTypes = { 173 | typeof(SByte), 174 | typeof(Int16), 175 | typeof(Int32), 176 | typeof(Int64), 177 | typeof(Byte), 178 | typeof(UInt16), 179 | typeof(UInt32), 180 | typeof(UInt64), 181 | typeof(Decimal), 182 | typeof(Single), 183 | typeof(Double) 184 | }; 185 | 186 | private Type[] NonNumericTypes = { 187 | typeof(char), 188 | typeof(string), 189 | typeof(DateTime), 190 | typeof(Guid), 191 | typeof(bool) 192 | }; 193 | 194 | // numeric types used by data providers 195 | private object[] NumericValues = { 196 | (SByte) 127, 197 | (Int16) 127, 198 | (Int32) 127, 199 | (Int64) 127, 200 | (Byte) 127, 201 | (UInt16) 127, 202 | (UInt32) 127, 203 | (UInt64) 127, 204 | (Decimal) 127, 205 | (Single) 127, 206 | (Double) 127 207 | }; 208 | 209 | // core non-numeric types used by data providers 210 | private object[] NonNumericValues = { 211 | "string", 212 | 'c', 213 | DateTime.Today, 214 | Guid.Parse("5033b416-9f63-4370-a435-1c0c7c102e67"), 215 | true 216 | }; 217 | 218 | private IEnumerable NumericConversionCases() 219 | { 220 | foreach (var type in NumericTypes) 221 | { 222 | foreach (var value in NumericValues) 223 | { 224 | var name = String.Format("{0} to {1}", value.GetType().Name, type.Name); 225 | yield return new TestCaseData(type, value).SetName(name); 226 | } 227 | } 228 | } 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DBHelpers 2 | 3 | DBHelpers is a simple but powerful library for working with plain ADO.NET. 4 | 5 | There are countless frameworks for data access, but most of them get in the way when you need to run direct SQL. 6 | If you want to optimize complex operations, or just want to run some sql without mapping classes, this library is for you. 7 | 8 | This library is not intended to replace any big framework, but is a nice and lightweight addition to any project of any size and it will make you rethink how much you can do using plain ADO.NET. 9 | 10 | ## System Requirements 11 | 12 | DBHelpers is coded for .NET 4.5 and should work with any .NET Data Provider. 13 | 14 | ## Installation 15 | 16 | 17 | You can install DBHelpers from [Nuget](https://www.nuget.org/packages/DBHelpers/): 18 | 19 | ``` 20 | Install-Package DBHelpers 21 | ``` 22 | 23 | ## Quick Start 24 | 25 | This is not actually required, but makes it easier to work with DBHelper. If possible, add the "providerName" to your connection string: 26 | 27 | ```xml 28 | 29 | 30 | 31 | ``` 32 | Then, instantiate the DBHelper: 33 | 34 | ```cs 35 | using DBHelpers; 36 | ... 37 | // if you have provided a providerName in the connectionString, you can just use the connection string name 38 | var db = new DBHelper("MyCN"); 39 | 40 | // or instantiate it manually 41 | var db = new DBHelper(System.Data.SqlClient.SqlClientFactory.Instance, "Server=localhost; ..."); 42 | ``` 43 | And start calling the ExecuteXyz methods: 44 | ```cs 45 | // returning a Scalar value 46 | var count = db.ExecuteScalar("select count(*) from table"); 47 | 48 | // returning a DataTable 49 | var dt = db.ExecuteDataTable("select * from table"); 50 | 51 | // returning an object (properties are mapped if names match the columns) 52 | var client = db.ExecuteObject("select * from client"); 53 | ``` 54 | 55 | ## More Details 56 | 57 | ADO.NET is not hard to use, but as any low level component it requires a lot of plumbing. It requires you to explicitly open connections and remember to close them. It requires you to convert values and handle DBNulls. As you work with it, it becomes clear that many things could be automated. This library is basically a lot of overloads that do most of this plumbing and let you concentrate on what you need to do. 58 | 59 | DBHelpers is composed of 2 main helper classes: 60 | 61 | * **DBHelper**: handles query execution 62 | * **DBConvert**: handles common cases of type conversion when using by ADO.NET (similar to System.Convert) 63 | 64 | Once instantiated, DBHelper supports executing queries directly to the database and returning useful types in a single command. For example: 65 | 66 | ```cs 67 | var lastDate = db.ExecuteScalar("select max(change_date) from table"); 68 | var arr = db.ExecuteArray("select id from table"); 69 | var dict = db.ExecuteDictionary("select id, name from users"); 70 | var dt = db.ExecuteDataTable("select * from table"); 71 | ``` 72 | 73 | The connection is automatically opened, closed and returned to the pool as quickly as possible. 74 | 75 | You can execute any type of query and return any type of object. It support the regular old methods: 76 | 77 | * `ExecuteNonQuery` 78 | * `ExecuteReader` 79 | * `ExecuteScalar` 80 | 81 | The exception is ExecuteScalar which is typed and automatically converts values to the appropriate type. 82 | Additionally we have some more useful methods: 83 | 84 | * `ExecuteDataTable` 85 | * `ExecuteDataSet` 86 | * `ExecuteArray` 87 | * `ExecuteDictionary` 88 | * `ExecuteObject` 89 | * `ExecuteList` 90 | 91 | All methods are optimized for speed and will do as little as possible and return the data in the desired format. 92 | 93 | ### Automatic Type Conversion 94 | --- 95 | When loading data from the database, values can be null/DBNull or can be of a slightly different type. DBHelpers adds some extension methods to DbDataReader, so you can safely expect certain types. 96 | 97 | This is how you can read data from a table to a list of anonymous objects for quick use: 98 | 99 | ```cs 100 | var list = DB.ExecuteList("select id, name, age from people", r => new { 101 | ID = r.Get("id"), 102 | Name = r.Get("name"), 103 | Age = r.Get("age") 104 | }); 105 | ``` 106 | 107 | DBHelper uses **DBConvert** under the hood for type conversion when it needs to. This class was based on System.Convert, and can be used directly in your code. It provides methods to convert all the basc types and some others between .NET and data providers. It is supposed to be used to get values out of DbDataReader and DataRows, and here is how you would use it: 108 | 109 | ```cs 110 | string value = DBConvert.ToString(reader["someField"]); 111 | int value = DBConvert.ToInt32(reader["someField"]); 112 | int? value = DBConvert.ToNullableDateTime(reader["someField"]); 113 | long? value = DBConvert.To(reader["someField"]); 114 | Guid value = (Guid) DBConvert.To(typeof(Guid), reader["someField"]); 115 | 116 | ``` 117 | The core functionality of DBConvert for all "ToXyz" overloads is quite simple: 118 | 119 | * If the value is a DBNull or null, return the default value for the type 120 | * If the value is of the desired type or can be safely cast to that type, cast it and return 121 | * If the value is something else, delegate to System.Convert (if possible) 122 | 123 | This provides a quite safe experience for most cases and allow you to not care about DBNulls or conversions. 124 | 125 | Here is what is expected: 126 | ```cs 127 | var value = db.ExecuteScalar("select cast(null as int)"); 128 | // int is a struct, so value = default(int) = 0 129 | 130 | var value = db.ExecuteScalar("select cast(null as varchar)"); 131 | // string is a reference type, so value = default(string) = null 132 | 133 | var value = db.ExecuteScalar("select cast(null as int)"); 134 | // int? is a nullable, so value = default(int?) = null 135 | 136 | var value = db.ExecuteScalar("select count(*) from t"); 137 | // value comes as int or long depending on the provider, but is converted to byte using System.Convert 138 | ``` 139 | 140 | Note that the idea is not to hide errors, but to avoid having to write obvious conversions. If you retrieve a byte[] and try to cast to a DateTime, you will get an exception. You are expected to know what you are doing. :-) Check [how System.Convert works](https://msdn.microsoft.com/en-us/library/system.convert(v=vs.110).aspx) for what you need, most of the actual conversion is done there when needed. 141 | 142 | There is also a simple `DBConvert.ToDBValue(object)` that does one simple thing: 143 | 144 | * If the value is null return DBNull.Value, else return the original value 145 | 146 | This is used to when creating commands and passing values to DbParameters. 147 | 148 | ### Some other useful features 149 | --- 150 | 151 | #### Async 152 | All methods have `Async` versions, so you can use tasks. Async support is provided by the .NET data provider directly whenever possible. 153 | 154 | ```cs 155 | await db.ExecuteNonQueryAsync("create table... "); 156 | var ids = await db.ExecuteScalarAsync("select id from table"); 157 | using (var reader = await db.ExecuteReaderAsync(query)) { ... } 158 | ``` 159 | 160 | #### Batch execution 161 | 162 | All methods have overloads to support using a specific connections, so you can can also run multiple operations in batch: 163 | 164 | ```cs 165 | using (var connection = db.CreateConnection()) { 166 | connection.Open(); 167 | db.ExecuteNonQuery(..., connection); 168 | db.ExecuteNonQuery(..., connection); 169 | db.ExecuteNonQuery(..., connection); 170 | return db.ExecuteScalar(..., connection); 171 | } 172 | ``` 173 | 174 | #### Custom converters 175 | 176 | All methods that return values accept either `Converter` or` Converter`, so you if you know what to expect, you can optimize with some manual conversion while still leveraging DBHelper: 177 | 178 | ```cs 179 | // using reader directly with ordinal lookup 180 | var list = db.ExecuteList("select top 10 id, name, age from user", reader => new User { 181 | ID = reader.GetInt32(0), 182 | Name = reader.GetString(1), 183 | Age = reader.IsDBNull(2) ? null : reader.GetInt32(2) 184 | }); 185 | 186 | // if you know what is coming, just cast the value and avoid any checks 187 | var count = db.ExecuteArray("select id from users", o => (int) o); 188 | 189 | // you can also leverage existing methods for simple conversions 190 | var dict = db.ExecuteDictionary("select id, name from users", Convert.ToInt32, Convert.ToString); 191 | ``` 192 | 193 | #### Multi table retrieval 194 | You can retrieve multiple datatables at once: 195 | 196 | ```cs 197 | var queries = @" 198 | select * from table1; 199 | select * from table2; 200 | select * from table3; 201 | select * from table4; 202 | " 203 | var dataset = db.ExecuteDataSet(queries); 204 | ``` 205 | 206 | #### Easy parameterized command generation 207 | 208 | You can generate safe parameterized DbCommand objects for any database: 209 | 210 | ```cs 211 | var command = db.CreateCommand("select * from users where name like {0}", userName + "%"); 212 | // generates "select * from users where name like @p0" in SQL Server and MySql 213 | // "select * from users where name like ?" in OleDb 214 | ``` 215 | 216 | All methods support passing string and DbCommand for maximum flexibility. 217 | 218 | #### Automatic mapping of objects 219 | 220 | Although this is not the focus of the library, it is sometimes useful to just load a table to an object. DBHelper provides a very simple mapper that matches column names to the properties. Mapping is case-insensitive. 221 | 222 | ```cs 223 | var user = db.ExecuteObject("select * from user where id = 1"); 224 | var users = db.ExecuteList("select * from user"); 225 | ``` 226 | 227 | This is mostly provided as a utility method, for most cases it is better to provide a converter method. 228 | 229 | #### Built in paging support 230 | 231 | There is also a built-in paging mechanism that works with any database for all methods that return collections. If you are using this library, you can probably optimize using SQL directly. But some databases either don't support pagination or have very complicated syntax. This method uses the data reader to load only the amount of records you need, and is a good compromise for most situations. 232 | 233 | ```cs 234 | var topUsers = db.ExecuteList("select * from users", 0, 10); 235 | var page = db.ExecuteDataTable("select * from users", 20, 10); 236 | var arr = db.ExecuteArray("select id from top_visitors", 0, 100); 237 | ``` 238 | 239 | ## Getting Help / Contributing 240 | 241 | If you have any question, suggestion, feature request or found a bug, please use the [issue tracker](https://github.com/nvivo/dbhelpers/issues). 242 | -------------------------------------------------------------------------------- /Src/DBHelpers.Tests/DbHelperOverloadTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text.RegularExpressions; 7 | 8 | namespace DBHelpers.Tests 9 | { 10 | [TestFixture] 11 | public class DbHelperOverloadTests : BaseOverloadTests 12 | { 13 | protected override IEnumerable GetRequiredInstanceSignatures() 14 | { 15 | var sigs = new string[] { 16 | "Int32 ExecuteNonQuery(DbCommand)", 17 | "Int32 ExecuteNonQuery(DbCommand, DbConnection)", 18 | "Int32 ExecuteNonQuery(DbCommand, DbTransaction)", 19 | 20 | "T ExecuteScalar(DbCommand)", 21 | "T ExecuteScalar(DbCommand, DbConnection)", 22 | "T ExecuteScalar(DbCommand, DbTransaction)", 23 | 24 | "T ExecuteScalar(DbCommand, Converter)", 25 | "T ExecuteScalar(DbCommand, Converter, DbConnection)", 26 | "T ExecuteScalar(DbCommand, Converter, DbTransaction)", 27 | 28 | "DbDataReader ExecuteReader(DbCommand)", 29 | "DbDataReader ExecuteReader(DbCommand, DbConnection)", 30 | "DbDataReader ExecuteReader(DbCommand, DbTransaction)", 31 | 32 | "DataTable ExecuteDataTable(DbCommand)", 33 | "DataTable ExecuteDataTable(DbCommand, DbConnection)", 34 | "DataTable ExecuteDataTable(DbCommand, DbTransaction)", 35 | "DataTable ExecuteDataTable(DbCommand, Int32, Int32)", 36 | "DataTable ExecuteDataTable(DbCommand, Int32, Int32, DbConnection)", 37 | "DataTable ExecuteDataTable(DbCommand, Int32, Int32, DbTransaction)", 38 | 39 | "DataSet ExecuteDataSet(DbCommand)", 40 | "DataSet ExecuteDataSet(DbCommand, DbConnection)", 41 | "DataSet ExecuteDataSet(DbCommand, DbTransaction)", 42 | 43 | "T[] ExecuteArray(DbCommand)", 44 | "T[] ExecuteArray(DbCommand, DbConnection)", 45 | "T[] ExecuteArray(DbCommand, DbTransaction)", 46 | "T[] ExecuteArray(DbCommand, Int32, Int32)", 47 | "T[] ExecuteArray(DbCommand, Int32, Int32, DbConnection)", 48 | "T[] ExecuteArray(DbCommand, Int32, Int32, DbTransaction)", 49 | 50 | "T[] ExecuteArray(DbCommand, Converter)", 51 | "T[] ExecuteArray(DbCommand, Converter, DbConnection)", 52 | "T[] ExecuteArray(DbCommand, Converter, DbTransaction)", 53 | "T[] ExecuteArray(DbCommand, Converter, Int32, Int32)", 54 | "T[] ExecuteArray(DbCommand, Converter, Int32, Int32, DbConnection)", 55 | "T[] ExecuteArray(DbCommand, Converter, Int32, Int32, DbTransaction)", 56 | 57 | "Dictionary ExecuteDictionary(DbCommand)", 58 | "Dictionary ExecuteDictionary(DbCommand, DbConnection)", 59 | "Dictionary ExecuteDictionary(DbCommand, DbTransaction)", 60 | "Dictionary ExecuteDictionary(DbCommand, Int32, Int32)", 61 | "Dictionary ExecuteDictionary(DbCommand, Int32, Int32, DbConnection)", 62 | "Dictionary ExecuteDictionary(DbCommand, Int32, Int32, DbTransaction)", 63 | 64 | "Dictionary ExecuteDictionary(DbCommand, Converter, Converter)", 65 | "Dictionary ExecuteDictionary(DbCommand, Converter, Converter, DbConnection)", 66 | "Dictionary ExecuteDictionary(DbCommand, Converter, Converter, DbTransaction)", 67 | "Dictionary ExecuteDictionary(DbCommand, Converter, Converter, Int32, Int32)", 68 | "Dictionary ExecuteDictionary(DbCommand, Converter, Converter, Int32, Int32, DbConnection)", 69 | "Dictionary ExecuteDictionary(DbCommand, Converter, Converter, Int32, Int32, DbTransaction)", 70 | 71 | "T ExecuteObject(DbCommand)", 72 | "T ExecuteObject(DbCommand, DbConnection)", 73 | "T ExecuteObject(DbCommand, DbTransaction)", 74 | 75 | "T ExecuteObject(DbCommand, Converter)", 76 | "T ExecuteObject(DbCommand, Converter, DbConnection)", 77 | "T ExecuteObject(DbCommand, Converter, DbTransaction)", 78 | 79 | "List ExecuteList(DbCommand)", 80 | "List ExecuteList(DbCommand, DbConnection)", 81 | "List ExecuteList(DbCommand, DbTransaction)", 82 | "List ExecuteList(DbCommand, Int32, Int32)", 83 | "List ExecuteList(DbCommand, Int32, Int32, DbConnection)", 84 | "List ExecuteList(DbCommand, Int32, Int32, DbTransaction)", 85 | 86 | "List ExecuteList(DbCommand, Converter)", 87 | "List ExecuteList(DbCommand, Converter, DbConnection)", 88 | "List ExecuteList(DbCommand, Converter, DbTransaction)", 89 | "List ExecuteList(DbCommand, Converter, Int32, Int32)", 90 | "List ExecuteList(DbCommand, Converter, Int32, Int32, DbConnection)", 91 | "List ExecuteList(DbCommand, Converter, Int32, Int32, DbTransaction)", 92 | 93 | "Int32 ExecuteNonQuery(String)", 94 | "Int32 ExecuteNonQuery(String, DbConnection)", 95 | "Int32 ExecuteNonQuery(String, DbTransaction)", 96 | 97 | "T ExecuteScalar(String)", 98 | "T ExecuteScalar(String, DbConnection)", 99 | "T ExecuteScalar(String, DbTransaction)", 100 | "T ExecuteScalar(String, Converter)", 101 | "T ExecuteScalar(String, Converter, DbConnection)", 102 | "T ExecuteScalar(String, Converter, DbTransaction)", 103 | 104 | "DbDataReader ExecuteReader(String)", 105 | "DbDataReader ExecuteReader(String, DbConnection)", 106 | "DbDataReader ExecuteReader(String, DbTransaction)", 107 | 108 | "DataTable ExecuteDataTable(String)", 109 | "DataTable ExecuteDataTable(String, DbConnection)", 110 | "DataTable ExecuteDataTable(String, DbTransaction)", 111 | "DataTable ExecuteDataTable(String, Int32, Int32)", 112 | "DataTable ExecuteDataTable(String, Int32, Int32, DbConnection)", 113 | "DataTable ExecuteDataTable(String, Int32, Int32, DbTransaction)", 114 | 115 | "DataSet ExecuteDataSet(String)", 116 | "DataSet ExecuteDataSet(String, DbConnection)", 117 | "DataSet ExecuteDataSet(String, DbTransaction)", 118 | 119 | "T[] ExecuteArray(String)", 120 | "T[] ExecuteArray(String, DbConnection)", 121 | "T[] ExecuteArray(String, DbTransaction)", 122 | "T[] ExecuteArray(String, Int32, Int32)", 123 | "T[] ExecuteArray(String, Int32, Int32, DbConnection)", 124 | "T[] ExecuteArray(String, Int32, Int32, DbTransaction)", 125 | 126 | "T[] ExecuteArray(String, Converter)", 127 | "T[] ExecuteArray(String, Converter, DbConnection)", 128 | "T[] ExecuteArray(String, Converter, DbTransaction)", 129 | "T[] ExecuteArray(String, Converter, Int32, Int32)", 130 | "T[] ExecuteArray(String, Converter, Int32, Int32, DbConnection)", 131 | "T[] ExecuteArray(String, Converter, Int32, Int32, DbTransaction)", 132 | 133 | "Dictionary ExecuteDictionary(String)", 134 | "Dictionary ExecuteDictionary(String, DbConnection)", 135 | "Dictionary ExecuteDictionary(String, DbTransaction)", 136 | "Dictionary ExecuteDictionary(String, Int32, Int32)", 137 | "Dictionary ExecuteDictionary(String, Int32, Int32, DbConnection)", 138 | "Dictionary ExecuteDictionary(String, Int32, Int32, DbTransaction)", 139 | 140 | "Dictionary ExecuteDictionary(String, Converter, Converter)", 141 | "Dictionary ExecuteDictionary(String, Converter, Converter, DbConnection)", 142 | "Dictionary ExecuteDictionary(String, Converter, Converter, DbTransaction)", 143 | "Dictionary ExecuteDictionary(String, Converter, Converter, Int32, Int32)", 144 | "Dictionary ExecuteDictionary(String, Converter, Converter, Int32, Int32, DbConnection)", 145 | "Dictionary ExecuteDictionary(String, Converter, Converter, Int32, Int32, DbTransaction)", 146 | 147 | "T ExecuteObject(String)", 148 | "T ExecuteObject(String, DbConnection)", 149 | "T ExecuteObject(String, DbTransaction)", 150 | "T ExecuteObject(String, Converter)", 151 | "T ExecuteObject(String, Converter, DbConnection)", 152 | "T ExecuteObject(String, Converter, DbTransaction)", 153 | 154 | "List ExecuteList(String)", 155 | "List ExecuteList(String, DbConnection)", 156 | "List ExecuteList(String, DbTransaction)", 157 | "List ExecuteList(String, Int32, Int32)", 158 | "List ExecuteList(String, Int32, Int32, DbConnection)", 159 | "List ExecuteList(String, Int32, Int32, DbTransaction)", 160 | 161 | "List ExecuteList(String, Converter)", 162 | "List ExecuteList(String, Converter, DbConnection)", 163 | "List ExecuteList(String, Converter, DbTransaction)", 164 | "List ExecuteList(String, Converter, Int32, Int32)", 165 | "List ExecuteList(String, Converter, Int32, Int32, DbConnection)", 166 | "List ExecuteList(String, Converter, Int32, Int32, DbTransaction)", 167 | }; 168 | 169 | // hack to create an async version of all overloads above 170 | 171 | var asyncSigs = new List(); 172 | var matcher = new Regex(@"(.+) (.+)\((.+)\)"); 173 | 174 | foreach (var sig in sigs) 175 | { 176 | var asyncSig = matcher.Replace(sig, m => String.Format("Task<{0}> {1}Async({2})", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value)); 177 | asyncSigs.Add(asyncSig); 178 | } 179 | 180 | return sigs.Concat(asyncSigs); 181 | } 182 | 183 | protected override IEnumerable GetExistingInstanceSignatures() 184 | { 185 | string[] methods = { 186 | "ExecuteNonQuery", 187 | "ExecuteScalar", 188 | "ExecuteReader", 189 | "ExecuteDataTable", 190 | "ExecuteDataSet", 191 | "ExecuteArray", 192 | "ExecuteDictionary", 193 | "ExecuteObject", 194 | "ExecuteList" 195 | }; 196 | 197 | var type = typeof(DBHelper); 198 | 199 | foreach (var method in methods) 200 | { 201 | foreach (var signature in type.GetSignatures(method, BindingFlags.Public | BindingFlags.Instance)) 202 | yield return signature; 203 | 204 | foreach (var signature in type.GetSignatures(method + "Async", BindingFlags.Public | BindingFlags.Instance)) 205 | yield return signature; 206 | } 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /Src/DBHelpers/DBConvert.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // Copyright 2010-2015 Natan Vivo - http://github.com/nvivo/dbhelpers 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | #endregion 16 | 17 | using System; 18 | using System.Globalization; 19 | 20 | namespace DBHelpers 21 | { 22 | public class DBConvert 23 | { 24 | #region Boolean 25 | 26 | private static bool ToBooleanInternal(object value, IFormatProvider provider) 27 | { 28 | string s = value as string; 29 | bool b; 30 | 31 | if (s != null && Boolean.TryParse(s, out b)) 32 | return b; 33 | 34 | if ((s = value.ToString()) != null) 35 | { 36 | int i; 37 | 38 | if (Int32.TryParse(s, out i)) 39 | return i != 0; 40 | } 41 | 42 | return Convert.ToBoolean(value, provider); 43 | } 44 | 45 | public static bool? ToNullableBoolean(object value, IFormatProvider provider) 46 | { 47 | return ToNullable(value, provider, ToBooleanInternal); 48 | } 49 | 50 | public static bool? ToNullableBoolean(object value) 51 | { 52 | return ToNullable(value, CultureInfo.CurrentCulture, ToBooleanInternal); 53 | } 54 | 55 | public static bool ToBoolean(object value, IFormatProvider provider) 56 | { 57 | return To(value, provider, ToBooleanInternal); 58 | } 59 | 60 | public static bool ToBoolean(object value) 61 | { 62 | return To(value, CultureInfo.CurrentCulture, ToBooleanInternal); 63 | } 64 | 65 | #endregion 66 | 67 | #region SByte 68 | 69 | private static sbyte ToSByteInternal(object value, IFormatProvider provider) 70 | { 71 | return Convert.ToSByte(value, provider); 72 | } 73 | 74 | public static sbyte? ToNullableSByte(object value, IFormatProvider provider) 75 | { 76 | return ToNullable(value, provider, ToSByteInternal); 77 | } 78 | 79 | public static sbyte? ToNullableSByte(object value) 80 | { 81 | return ToNullable(value, CultureInfo.CurrentCulture, ToSByteInternal); 82 | } 83 | 84 | public static sbyte ToSByte(object value, IFormatProvider provider) 85 | { 86 | return To(value, provider, ToSByteInternal); 87 | } 88 | 89 | public static sbyte ToSByte(object value) 90 | { 91 | return To(value, CultureInfo.CurrentCulture, ToSByteInternal); 92 | } 93 | 94 | #endregion 95 | 96 | #region Int16 97 | 98 | private static short ToInt16Internal(object value, IFormatProvider provider) 99 | { 100 | return Convert.ToInt16(value, provider); 101 | } 102 | 103 | public static short? ToNullableInt16(object value, IFormatProvider provider) 104 | { 105 | return ToNullable(value, provider, ToInt16Internal); 106 | } 107 | 108 | public static short? ToNullableInt16(object value) 109 | { 110 | return ToNullable(value, CultureInfo.CurrentCulture, ToInt16Internal); 111 | } 112 | 113 | public static short ToInt16(object value, IFormatProvider provider) 114 | { 115 | return To(value, provider, ToInt16Internal); 116 | } 117 | 118 | public static short ToInt16(object value) 119 | { 120 | return To(value, CultureInfo.CurrentCulture, ToInt16Internal); 121 | } 122 | 123 | #endregion 124 | 125 | #region Int32 126 | 127 | private static int ToInt32Internal(object value, IFormatProvider provider) 128 | { 129 | return Convert.ToInt32(value, provider); 130 | } 131 | 132 | public static int? ToNullableInt32(object value, IFormatProvider provider) 133 | { 134 | return ToNullable(value, provider, ToInt32Internal); 135 | } 136 | 137 | public static int? ToNullableInt32(object value) 138 | { 139 | return ToNullable(value, CultureInfo.CurrentCulture, ToInt32Internal); 140 | } 141 | 142 | public static int ToInt32(object value, IFormatProvider provider) 143 | { 144 | return To(value, provider, ToInt32Internal); 145 | } 146 | 147 | public static int ToInt32(object value) 148 | { 149 | return To(value, CultureInfo.CurrentCulture, ToInt32Internal); 150 | } 151 | 152 | #endregion 153 | 154 | #region Int64 155 | 156 | private static long ToInt64Internal(object value, IFormatProvider provider) 157 | { 158 | return Convert.ToInt64(value, provider); 159 | } 160 | 161 | public static long? ToNullableInt64(object value, IFormatProvider provider) 162 | { 163 | return ToNullable(value, provider, ToInt64Internal); 164 | } 165 | 166 | public static long? ToNullableInt64(object value) 167 | { 168 | return ToNullable(value, CultureInfo.CurrentCulture, ToInt64Internal); 169 | } 170 | 171 | public static long ToInt64(object value, IFormatProvider provider) 172 | { 173 | return To(value, provider, ToInt64Internal); 174 | } 175 | 176 | public static long ToInt64(object value) 177 | { 178 | return To(value, CultureInfo.CurrentCulture, ToInt64Internal); 179 | } 180 | 181 | #endregion 182 | 183 | #region Byte 184 | 185 | private static byte ToByteInternal(object value, IFormatProvider provider) 186 | { 187 | return Convert.ToByte(value, provider); 188 | } 189 | 190 | public static byte? ToNullableByte(object value, IFormatProvider provider) 191 | { 192 | return ToNullable(value, provider, ToByteInternal); 193 | } 194 | 195 | public static byte? ToNullableByte(object value) 196 | { 197 | return ToNullable(value, CultureInfo.CurrentCulture, ToByteInternal); 198 | } 199 | 200 | public static byte ToByte(object value, IFormatProvider provider) 201 | { 202 | return To(value, provider, ToByteInternal); 203 | } 204 | 205 | public static byte ToByte(object value) 206 | { 207 | return To(value, CultureInfo.CurrentCulture, ToByteInternal); 208 | } 209 | 210 | #endregion 211 | 212 | #region UInt16 213 | 214 | private static ushort ToUInt16Internal(object value, IFormatProvider provider) 215 | { 216 | return Convert.ToUInt16(value, provider); 217 | } 218 | 219 | public static ushort? ToNullableUInt16(object value, IFormatProvider provider) 220 | { 221 | return ToNullable(value, provider, ToUInt16Internal); 222 | } 223 | 224 | public static ushort? ToNullableUInt16(object value) 225 | { 226 | return ToNullable(value, CultureInfo.CurrentCulture, ToUInt16Internal); 227 | } 228 | 229 | public static ushort ToUInt16(object value, IFormatProvider provider) 230 | { 231 | return To(value, provider, ToUInt16Internal); 232 | } 233 | 234 | public static ushort ToUInt16(object value) 235 | { 236 | return To(value, CultureInfo.CurrentCulture, ToUInt16Internal); 237 | } 238 | 239 | #endregion 240 | 241 | #region UInt32 242 | 243 | private static uint ToUInt32Internal(object value, IFormatProvider provider) 244 | { 245 | return Convert.ToUInt32(value, provider); 246 | } 247 | 248 | public static uint? ToNullableUInt32(object value, IFormatProvider provider) 249 | { 250 | return ToNullable(value, provider, ToUInt32Internal); 251 | } 252 | 253 | public static uint? ToNullableUInt32(object value) 254 | { 255 | return ToNullable(value, CultureInfo.CurrentCulture, ToUInt32Internal); 256 | } 257 | 258 | public static uint ToUInt32(object value, IFormatProvider provider) 259 | { 260 | return To(value, provider, ToUInt32Internal); 261 | } 262 | 263 | public static uint ToUInt32(object value) 264 | { 265 | return To(value, CultureInfo.CurrentCulture, ToUInt32Internal); 266 | } 267 | 268 | #endregion 269 | 270 | #region UInt64 271 | 272 | private static ulong ToUInt64Internal(object value, IFormatProvider provider) 273 | { 274 | return Convert.ToUInt64(value, provider); 275 | } 276 | 277 | public static ulong? ToNullableUInt64(object value, IFormatProvider provider) 278 | { 279 | return ToNullable(value, provider, ToUInt64Internal); 280 | } 281 | 282 | public static ulong? ToNullableUInt64(object value) 283 | { 284 | return ToNullable(value, CultureInfo.CurrentCulture, ToUInt64Internal); 285 | } 286 | 287 | public static ulong ToUInt64(object value, IFormatProvider provider) 288 | { 289 | return To(value, provider, ToUInt64Internal); 290 | } 291 | 292 | public static ulong ToUInt64(object value) 293 | { 294 | return To(value, CultureInfo.CurrentCulture, ToUInt64Internal); 295 | } 296 | 297 | #endregion 298 | 299 | #region Decimal 300 | 301 | private static decimal ToDecimalInternal(object value, IFormatProvider provider) 302 | { 303 | return Convert.ToDecimal(value, provider); 304 | } 305 | 306 | public static decimal? ToNullableDecimal(object value, IFormatProvider provider) 307 | { 308 | return ToNullable(value, provider, ToDecimalInternal); 309 | } 310 | 311 | public static decimal? ToNullableDecimal(object value) 312 | { 313 | return ToNullable(value, CultureInfo.CurrentCulture, ToDecimalInternal); 314 | } 315 | 316 | public static decimal ToDecimal(object value, IFormatProvider provider) 317 | { 318 | return To(value, provider, ToDecimalInternal); 319 | } 320 | 321 | public static decimal ToDecimal(object value) 322 | { 323 | return To(value, CultureInfo.CurrentCulture, ToDecimalInternal); 324 | } 325 | 326 | #endregion 327 | 328 | #region Single 329 | 330 | private static float ToSingleInternal(object value, IFormatProvider provider) 331 | { 332 | return Convert.ToSingle(value, provider); 333 | } 334 | 335 | public static float? ToNullableSingle(object value, IFormatProvider provider) 336 | { 337 | return ToNullable(value, provider, ToSingleInternal); 338 | } 339 | 340 | public static float? ToNullableSingle(object value) 341 | { 342 | return ToNullable(value, CultureInfo.CurrentCulture, ToSingleInternal); 343 | } 344 | 345 | public static float ToSingle(object value, IFormatProvider provider) 346 | { 347 | return To(value, provider, ToSingleInternal); 348 | } 349 | 350 | public static float ToSingle(object value) 351 | { 352 | return To(value, CultureInfo.CurrentCulture, ToSingleInternal); 353 | } 354 | 355 | #endregion 356 | 357 | #region Double 358 | 359 | private static double ToDoubleInternal(object value, IFormatProvider provider) 360 | { 361 | return Convert.ToDouble(value, provider); 362 | } 363 | 364 | public static double? ToNullableDouble(object value, IFormatProvider provider) 365 | { 366 | return ToNullable(value, provider, ToDoubleInternal); 367 | } 368 | 369 | public static double? ToNullableDouble(object value) 370 | { 371 | return ToNullable(value, CultureInfo.CurrentCulture, ToDoubleInternal); 372 | } 373 | 374 | public static double ToDouble(object value, IFormatProvider provider) 375 | { 376 | return To(value, provider, ToDoubleInternal); 377 | } 378 | 379 | public static double ToDouble(object value) 380 | { 381 | return To(value, CultureInfo.CurrentCulture, ToDoubleInternal); 382 | } 383 | 384 | #endregion 385 | 386 | #region Char 387 | 388 | private static char ToCharInternal(object value, IFormatProvider provider) 389 | { 390 | var s = value as string; 391 | 392 | if (!String.IsNullOrEmpty(s)) 393 | return s[0]; 394 | 395 | return Convert.ToChar(value, provider); 396 | } 397 | 398 | public static char? ToNullableChar(object value, IFormatProvider provider) 399 | { 400 | return ToNullable(value, provider, ToCharInternal); 401 | } 402 | 403 | public static char? ToNullableChar(object value) 404 | { 405 | return ToNullable(value, CultureInfo.CurrentCulture, ToCharInternal); 406 | } 407 | 408 | public static char ToChar(object value, IFormatProvider provider) 409 | { 410 | return To(value, provider, ToCharInternal); 411 | } 412 | 413 | public static char ToChar(object value) 414 | { 415 | return To(value, CultureInfo.CurrentCulture, ToCharInternal); 416 | } 417 | 418 | #endregion 419 | 420 | #region DateTime 421 | 422 | private static DateTime ToDateTimeInternal(object value, IFormatProvider provider) 423 | { 424 | return Convert.ToDateTime(value, provider); 425 | } 426 | 427 | public static DateTime? ToNullableDateTime(object value, IFormatProvider provider) 428 | { 429 | return ToNullable(value, provider, ToDateTimeInternal); 430 | } 431 | 432 | public static DateTime? ToNullableDateTime(object value) 433 | { 434 | return ToNullable(value, CultureInfo.CurrentCulture, ToDateTimeInternal); 435 | } 436 | 437 | public static DateTime ToDateTime(object value, IFormatProvider provider) 438 | { 439 | return To(value, provider, ToDateTimeInternal); 440 | } 441 | 442 | public static DateTime ToDateTime(object value) 443 | { 444 | return To(value, CultureInfo.CurrentCulture, ToDateTimeInternal); 445 | } 446 | 447 | #endregion 448 | 449 | #region DateTimeOffset 450 | 451 | private static DateTimeOffset ToDateTimeOffsetInternal(object value, IFormatProvider provider, DateTimeStyles styles) 452 | { 453 | if (value is DateTime) 454 | return (DateTimeOffset) value; 455 | 456 | return DateTimeOffset.Parse(value.ToString(), provider, styles); 457 | } 458 | 459 | private static DateTimeOffset ToDateTimeOffsetInternal(object value, IFormatProvider provider) 460 | { 461 | return ToDateTimeOffsetInternal(value, provider, DateTimeStyles.None); 462 | } 463 | 464 | public static DateTimeOffset? ToNullableDateTimeOffset(object value, IFormatProvider provider) 465 | { 466 | return ToNullable(value, provider, ToDateTimeOffsetInternal); 467 | } 468 | 469 | public static DateTimeOffset? ToNullableDateTimeOffset(object value) 470 | { 471 | return ToNullable(value, CultureInfo.CurrentCulture, ToDateTimeOffsetInternal); 472 | } 473 | 474 | public static DateTimeOffset ToDateTimeOffset(object value, IFormatProvider provider) 475 | { 476 | return To(value, provider, ToDateTimeOffsetInternal); 477 | } 478 | 479 | public static DateTimeOffset ToDateTimeOffset(object value) 480 | { 481 | return To(value, CultureInfo.CurrentCulture, ToDateTimeOffsetInternal); 482 | } 483 | 484 | #endregion 485 | 486 | #region String 487 | 488 | private static string ToStringInternal(object value, IFormatProvider provider) 489 | { 490 | return Convert.ToString(value, provider); 491 | } 492 | 493 | public static string ToString(object value, IFormatProvider provider) 494 | { 495 | return To(value, provider, ToStringInternal); 496 | } 497 | 498 | public static string ToString(object value) 499 | { 500 | return To(value, CultureInfo.CurrentCulture, ToStringInternal); 501 | } 502 | 503 | #endregion 504 | 505 | #region Guid 506 | 507 | // This method accepts IFormatProvider just to match the signature required by 508 | // To and ToNullable, but it is not used 509 | private static Guid ToGuidInternal(object value, IFormatProvider provider) 510 | { 511 | byte[] bytes = value as byte[]; 512 | 513 | if (bytes != null) 514 | return new Guid(bytes); 515 | 516 | return new Guid(value.ToString()); 517 | } 518 | 519 | public static Guid? ToNullableGuid(object value) 520 | { 521 | return ToNullable(value, null, ToGuidInternal); 522 | } 523 | 524 | public static Guid ToGuid(object value) 525 | { 526 | return To(value, null, ToGuidInternal); 527 | } 528 | 529 | #endregion 530 | 531 | #region ByteArray 532 | 533 | public static byte[] ToByteArray(object value) 534 | { 535 | if (value is byte[]) 536 | return (byte[])value; 537 | 538 | if (value == null || value == DBNull.Value) 539 | return null; 540 | 541 | throw new FormatException("Cannot cast value to byte[]."); 542 | } 543 | 544 | #endregion 545 | 546 | #region To 547 | 548 | public static T To(object value) 549 | { 550 | return To(value, CultureInfo.CurrentCulture); 551 | } 552 | 553 | public static T To(object value, IFormatProvider provider) 554 | { 555 | if (value is T) 556 | return (T)value; 557 | 558 | if (value == null || value == DBNull.Value) 559 | return default(T); 560 | 561 | return (T)To(typeof(T), value, provider); 562 | } 563 | 564 | public static object To(Type type, object value) 565 | { 566 | return To(type, value, CultureInfo.CurrentCulture); 567 | } 568 | 569 | public static object To(Type type, object value, IFormatProvider provider) 570 | { 571 | if (type == null) 572 | throw new ArgumentNullException("type"); 573 | 574 | if (value != null && value.GetType() == type) 575 | return value; 576 | 577 | var isNullable = IsNullable(type); 578 | 579 | if (isNullable) 580 | type = Nullable.GetUnderlyingType(type); 581 | 582 | if (value == null || value == DBNull.Value) 583 | { 584 | if (isNullable || !type.IsValueType) 585 | return null; 586 | else 587 | return Activator.CreateInstance(type); 588 | } 589 | 590 | var typeCode = Type.GetTypeCode(type); 591 | 592 | switch (typeCode) 593 | { 594 | case TypeCode.Boolean: 595 | return ToBooleanInternal(value, provider); 596 | 597 | case TypeCode.SByte: 598 | return ToSByteInternal(value, provider); 599 | 600 | case TypeCode.Int16: 601 | return ToInt16Internal(value, provider); 602 | 603 | case TypeCode.Int32: 604 | return ToInt32Internal(value, provider); 605 | 606 | case TypeCode.Int64: 607 | return ToInt64Internal(value, provider); 608 | 609 | case TypeCode.Byte: 610 | return ToByteInternal(value, provider); 611 | 612 | case TypeCode.UInt16: 613 | return ToUInt16Internal(value, provider); 614 | 615 | case TypeCode.UInt32: 616 | return ToUInt32Internal(value, provider); 617 | 618 | case TypeCode.UInt64: 619 | return ToUInt64Internal(value, provider); 620 | 621 | case TypeCode.Decimal: 622 | return ToDecimalInternal(value, provider); 623 | 624 | case TypeCode.Single: 625 | return ToSingleInternal(value, provider); 626 | 627 | case TypeCode.Double: 628 | return ToDoubleInternal(value, provider); 629 | 630 | case TypeCode.Char: 631 | return ToCharInternal(value, provider); 632 | 633 | case TypeCode.DateTime: 634 | return ToDateTimeInternal(value, provider); 635 | 636 | case TypeCode.String: 637 | return ToStringInternal(value, provider); 638 | 639 | case TypeCode.Object: 640 | if (type == typeof(Guid)) 641 | return ToGuidInternal(value, null); 642 | 643 | if (type == typeof(byte[])) 644 | return ToByteArray(value); 645 | 646 | if (type == typeof(DateTimeOffset)) 647 | return ToDateTimeOffsetInternal(value, null); 648 | 649 | break; 650 | } 651 | 652 | // fallback to System.Convert for IConvertible types 653 | return Convert.ChangeType(value, typeCode, provider); 654 | } 655 | 656 | #endregion 657 | 658 | #region ToDBValue 659 | 660 | public static object ToDBValue(object value) 661 | { 662 | if (value == null) 663 | return DBNull.Value; 664 | 665 | return value; 666 | } 667 | 668 | #endregion 669 | 670 | #region Internal Private Helpers 671 | 672 | private static T? ToNullable(object value, IFormatProvider provider, Func converter) 673 | where T : struct 674 | { 675 | if (value is T) 676 | return (T)value; 677 | 678 | if (value == null || value == DBNull.Value) 679 | return null; 680 | 681 | return converter(value, provider); 682 | } 683 | 684 | private static T To(object value, IFormatProvider provider, Func converter) 685 | { 686 | if (value is T) 687 | return (T)value; 688 | 689 | if (value == null || value == DBNull.Value) 690 | return default(T); 691 | 692 | return converter(value, provider); 693 | } 694 | 695 | private static bool IsNullable(Type type) 696 | { 697 | return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); 698 | } 699 | 700 | #endregion 701 | } 702 | } 703 | -------------------------------------------------------------------------------- /Src/DBHelpers/DBHelper.StringOverloads.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // Copyright 2010-2015 Natan Vivo - http://github.com/nvivo/dbhelpers 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | #endregion 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Data; 20 | using System.Data.Common; 21 | using System.Threading.Tasks; 22 | 23 | namespace DBHelpers 24 | { 25 | public partial class DBHelper 26 | { 27 | #region ExecuteNonQuery 28 | 29 | public int ExecuteNonQuery(string commandText, DbConnection connection) 30 | { 31 | var command = CreateCommand(commandText); 32 | return ExecuteNonQuery(command, connection); 33 | } 34 | 35 | public int ExecuteNonQuery(string commandText, DbTransaction transaction) 36 | { 37 | var command = CreateCommand(commandText); 38 | return ExecuteNonQuery(command, transaction); 39 | } 40 | 41 | public int ExecuteNonQuery(string commandText) 42 | { 43 | var command = CreateCommand(commandText); 44 | return ExecuteNonQuery(command); 45 | } 46 | 47 | #endregion 48 | 49 | #region ExecuteNonQueryAsync 50 | 51 | public Task ExecuteNonQueryAsync(string commandText, DbConnection connection) 52 | { 53 | var command = CreateCommand(commandText); 54 | return ExecuteNonQueryAsync(command, connection); 55 | } 56 | 57 | public Task ExecuteNonQueryAsync(string commandText, DbTransaction transaction) 58 | { 59 | var command = CreateCommand(commandText); 60 | return ExecuteNonQueryAsync(command, transaction); 61 | } 62 | 63 | public Task ExecuteNonQueryAsync(string commandText) 64 | { 65 | var command = CreateCommand(commandText); 66 | return ExecuteNonQueryAsync(command); 67 | } 68 | 69 | #endregion 70 | 71 | #region ExecuteScalar 72 | 73 | public T ExecuteScalar(string commandText, DbConnection connection) 74 | { 75 | var command = CreateCommand(commandText); 76 | return ExecuteScalar(command, connection); 77 | } 78 | 79 | public T ExecuteScalar(string commandText, DbTransaction transaction) 80 | { 81 | var command = CreateCommand(commandText); 82 | return ExecuteScalar(command, transaction); 83 | } 84 | 85 | public T ExecuteScalar(string commandText, Converter converter, DbConnection connection) 86 | { 87 | var command = CreateCommand(commandText); 88 | return ExecuteScalar(command, converter, connection); 89 | } 90 | 91 | public T ExecuteScalar(string commandText, Converter converter, DbTransaction transaction) 92 | { 93 | var command = CreateCommand(commandText); 94 | return ExecuteScalar(command, converter, transaction); 95 | } 96 | 97 | public T ExecuteScalar(string commandText) 98 | { 99 | var command = CreateCommand(commandText); 100 | return ExecuteScalar(command); 101 | } 102 | 103 | public T ExecuteScalar(string commandText, Converter converter) 104 | { 105 | var command = CreateCommand(commandText); 106 | return ExecuteScalar(command, converter); 107 | } 108 | 109 | #endregion 110 | 111 | #region ExecuteScalarAsync 112 | 113 | public Task ExecuteScalarAsync(string commandText, DbConnection connection) 114 | { 115 | var command = CreateCommand(commandText); 116 | return ExecuteScalarAsync(command, connection); 117 | } 118 | 119 | public Task ExecuteScalarAsync(string commandText, DbTransaction transaction) 120 | { 121 | var command = CreateCommand(commandText); 122 | return ExecuteScalarAsync(command, transaction); 123 | } 124 | 125 | public Task ExecuteScalarAsync(string commandText, Converter converter, DbConnection connection) 126 | { 127 | var command = CreateCommand(commandText); 128 | return ExecuteScalarAsync(command, converter, connection); 129 | } 130 | 131 | public Task ExecuteScalarAsync(string commandText, Converter converter, DbTransaction transaction) 132 | { 133 | var command = CreateCommand(commandText); 134 | return ExecuteScalarAsync(command, converter, transaction); 135 | } 136 | 137 | public Task ExecuteScalarAsync(string commandText) 138 | { 139 | var command = CreateCommand(commandText); 140 | return ExecuteScalarAsync(command); 141 | } 142 | 143 | public Task ExecuteScalarAsync(string commandText, Converter converter) 144 | { 145 | var command = CreateCommand(commandText); 146 | return ExecuteScalarAsync(command, converter); 147 | } 148 | 149 | #endregion 150 | 151 | #region ExecuteReader 152 | 153 | public DbDataReader ExecuteReader(string commandText, DbConnection connection) 154 | { 155 | var command = CreateCommand(commandText); 156 | return ExecuteReader(command, connection); 157 | } 158 | 159 | public DbDataReader ExecuteReader(string commandText, DbTransaction transaction) 160 | { 161 | var command = CreateCommand(commandText); 162 | return ExecuteReader(command, transaction); 163 | } 164 | 165 | public DbDataReader ExecuteReader(string commandText) 166 | { 167 | var command = CreateCommand(commandText); 168 | return ExecuteReader(command); 169 | } 170 | 171 | #endregion 172 | 173 | #region ExecuteReaderAsync 174 | 175 | public Task ExecuteReaderAsync(string commandText, DbConnection connection) 176 | { 177 | var command = CreateCommand(commandText); 178 | return ExecuteReaderAsync(command, connection); 179 | } 180 | 181 | public Task ExecuteReaderAsync(string commandText, DbTransaction transaction) 182 | { 183 | var command = CreateCommand(commandText); 184 | return ExecuteReaderAsync(command, transaction); 185 | } 186 | 187 | public Task ExecuteReaderAsync(string commandText) 188 | { 189 | var command = CreateCommand(commandText); 190 | return ExecuteReaderAsync(command); 191 | } 192 | 193 | #endregion 194 | 195 | #region ExecuteDataTable 196 | 197 | public DataTable ExecuteDataTable(string commandText, int startRecord, int maxRecords, DbConnection connection) 198 | { 199 | var command = CreateCommand(commandText); 200 | return ExecuteDataTable(command, startRecord, maxRecords, connection); 201 | } 202 | 203 | public DataTable ExecuteDataTable(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 204 | { 205 | var command = CreateCommand(commandText); 206 | return ExecuteDataTable(command, startRecord, maxRecords, transaction); 207 | } 208 | 209 | public DataTable ExecuteDataTable(string commandText, DbConnection connection) 210 | { 211 | var command = CreateCommand(commandText); 212 | return ExecuteDataTable(command, connection); 213 | } 214 | 215 | public DataTable ExecuteDataTable(string commandText, DbTransaction transaction) 216 | { 217 | var command = CreateCommand(commandText); 218 | return ExecuteDataTable(command, transaction); 219 | } 220 | 221 | public DataTable ExecuteDataTable(string commandText, int startRecord, int maxRecords) 222 | { 223 | var command = CreateCommand(commandText); 224 | return ExecuteDataTable(command, startRecord, maxRecords); 225 | } 226 | 227 | public DataTable ExecuteDataTable(string commandText) 228 | { 229 | var command = CreateCommand(commandText); 230 | return ExecuteDataTable(command); 231 | } 232 | 233 | #endregion 234 | 235 | #region ExecuteDataTableAsync 236 | 237 | public Task ExecuteDataTableAsync(string commandText, int startRecord, int maxRecords, DbConnection connection) 238 | { 239 | var command = CreateCommand(commandText); 240 | return ExecuteDataTableAsync(command, startRecord, maxRecords, connection); 241 | } 242 | 243 | public Task ExecuteDataTableAsync(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 244 | { 245 | var command = CreateCommand(commandText); 246 | return ExecuteDataTableAsync(command, startRecord, maxRecords, transaction); 247 | } 248 | 249 | public Task ExecuteDataTableAsync(string commandText, DbConnection connection) 250 | { 251 | var command = CreateCommand(commandText); 252 | return ExecuteDataTableAsync(command, connection); 253 | } 254 | 255 | public Task ExecuteDataTableAsync(string commandText, DbTransaction transaction) 256 | { 257 | var command = CreateCommand(commandText); 258 | return ExecuteDataTableAsync(command, transaction); 259 | } 260 | 261 | public Task ExecuteDataTableAsync(string commandText, int startRecord, int maxRecords) 262 | { 263 | var command = CreateCommand(commandText); 264 | return ExecuteDataTableAsync(command, startRecord, maxRecords); 265 | } 266 | 267 | public Task ExecuteDataTableAsync(string commandText) 268 | { 269 | var command = CreateCommand(commandText); 270 | return ExecuteDataTableAsync(command); 271 | } 272 | 273 | #endregion 274 | 275 | #region ExecuteDataSet 276 | 277 | public DataSet ExecuteDataSet(string commandText, DbConnection connection) 278 | { 279 | var command = CreateCommand(commandText); 280 | return ExecuteDataSet(command, connection); 281 | } 282 | 283 | public DataSet ExecuteDataSet(string commandText, DbTransaction transaction) 284 | { 285 | var command = CreateCommand(commandText); 286 | return ExecuteDataSet(command, transaction); 287 | } 288 | 289 | public Task ExecuteDataSetAsync(string commandText, DbConnection connection) 290 | { 291 | var command = CreateCommand(commandText); 292 | return ExecuteDataSetAsync(command, connection); 293 | } 294 | 295 | public Task ExecuteDataSetAsync(string commandText, DbTransaction transaction) 296 | { 297 | var command = CreateCommand(commandText); 298 | return ExecuteDataSetAsync(command, transaction); 299 | } 300 | 301 | public DataSet ExecuteDataSet(string commandText) 302 | { 303 | var command = CreateCommand(commandText); 304 | return ExecuteDataSet(command); 305 | } 306 | 307 | public Task ExecuteDataSetAsync(string commandText) 308 | { 309 | var command = CreateCommand(commandText); 310 | return ExecuteDataSetAsync(command); 311 | } 312 | 313 | #endregion 314 | 315 | #region ExecuteArray 316 | 317 | public T[] ExecuteArray(string commandText, int startRecord, int maxRecords, DbConnection connection) 318 | { 319 | var command = CreateCommand(commandText); 320 | return ExecuteArray(command, startRecord, maxRecords, connection); 321 | } 322 | 323 | public T[] ExecuteArray(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 324 | { 325 | var command = CreateCommand(commandText); 326 | return ExecuteArray(command, startRecord, maxRecords, transaction); 327 | } 328 | 329 | public T[] ExecuteArray(string commandText, DbConnection connection) 330 | { 331 | var command = CreateCommand(commandText); 332 | return ExecuteArray(command, connection); 333 | } 334 | 335 | public T[] ExecuteArray(string commandText, DbTransaction transaction) 336 | { 337 | var command = CreateCommand(commandText); 338 | return ExecuteArray(command, transaction); 339 | } 340 | 341 | public T[] ExecuteArray(string commandText, Converter converter, int startRecord, int maxRecords, DbConnection connection) 342 | { 343 | var command = CreateCommand(commandText); 344 | return ExecuteArray(command, converter, startRecord, maxRecords, connection); 345 | } 346 | 347 | public T[] ExecuteArray(string commandText, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 348 | { 349 | var command = CreateCommand(commandText); 350 | return ExecuteArray(command, converter, startRecord, maxRecords, transaction); 351 | } 352 | 353 | public T[] ExecuteArray(string commandText, Converter converter, DbConnection connection) 354 | { 355 | var command = CreateCommand(commandText); 356 | return ExecuteArray(command, converter, connection); 357 | } 358 | 359 | public T[] ExecuteArray(string commandText, Converter converter, DbTransaction transaction) 360 | { 361 | var command = CreateCommand(commandText); 362 | return ExecuteArray(command, converter, transaction); 363 | } 364 | 365 | public T[] ExecuteArray(string commandText, int startRecord, int maxRecords) 366 | { 367 | var command = CreateCommand(commandText); 368 | return ExecuteArray(command, startRecord, maxRecords); 369 | } 370 | 371 | public T[] ExecuteArray(string commandText) 372 | { 373 | var command = CreateCommand(commandText); 374 | return ExecuteArray(command); 375 | } 376 | 377 | public T[] ExecuteArray(string commandText, Converter converter, int startRecord, int maxRecords) 378 | { 379 | var command = CreateCommand(commandText); 380 | return ExecuteArray(command, converter, startRecord, maxRecords); 381 | } 382 | 383 | public T[] ExecuteArray(string commandText, Converter converter) 384 | { 385 | var command = CreateCommand(commandText); 386 | return ExecuteArray(command, converter); 387 | } 388 | 389 | #endregion 390 | 391 | #region ExecuteArrayAsync 392 | 393 | public Task ExecuteArrayAsync(string commandText, int startRecord, int maxRecords, DbConnection connection) 394 | { 395 | var command = CreateCommand(commandText); 396 | return ExecuteArrayAsync(command, startRecord, maxRecords, connection); 397 | } 398 | 399 | public Task ExecuteArrayAsync(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 400 | { 401 | var command = CreateCommand(commandText); 402 | return ExecuteArrayAsync(command, startRecord, maxRecords, transaction); 403 | } 404 | 405 | public Task ExecuteArrayAsync(string commandText, DbConnection connection) 406 | { 407 | var command = CreateCommand(commandText); 408 | return ExecuteArrayAsync(command, connection); 409 | } 410 | 411 | public Task ExecuteArrayAsync(string commandText, DbTransaction transaction) 412 | { 413 | var command = CreateCommand(commandText); 414 | return ExecuteArrayAsync(command, transaction); 415 | } 416 | 417 | public Task ExecuteArrayAsync(string commandText, Converter converter, int startRecord, int maxRecords, DbConnection connection) 418 | { 419 | var command = CreateCommand(commandText); 420 | return ExecuteArrayAsync(command, converter, startRecord, maxRecords, connection); 421 | } 422 | 423 | public Task ExecuteArrayAsync(string commandText, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 424 | { 425 | var command = CreateCommand(commandText); 426 | return ExecuteArrayAsync(command, converter, startRecord, maxRecords, transaction); 427 | } 428 | 429 | public Task ExecuteArrayAsync(string commandText, Converter converter, DbConnection connection) 430 | { 431 | var command = CreateCommand(commandText); 432 | return ExecuteArrayAsync(command, converter, connection); 433 | } 434 | 435 | public Task ExecuteArrayAsync(string commandText, Converter converter, DbTransaction transaction) 436 | { 437 | var command = CreateCommand(commandText); 438 | return ExecuteArrayAsync(command, converter, transaction); 439 | } 440 | 441 | public Task ExecuteArrayAsync(string commandText, int startRecord, int maxRecords) 442 | { 443 | var command = CreateCommand(commandText); 444 | return ExecuteArrayAsync(command, startRecord, maxRecords); 445 | } 446 | 447 | public Task ExecuteArrayAsync(string commandText) 448 | { 449 | var command = CreateCommand(commandText); 450 | return ExecuteArrayAsync(command); 451 | } 452 | 453 | public Task ExecuteArrayAsync(string commandText, Converter converter, int startRecord, int maxRecords) 454 | { 455 | var command = CreateCommand(commandText); 456 | return ExecuteArrayAsync(command, converter, startRecord, maxRecords); 457 | } 458 | 459 | public Task ExecuteArrayAsync(string commandText, Converter converter) 460 | { 461 | var command = CreateCommand(commandText); 462 | return ExecuteArrayAsync(command, converter); 463 | } 464 | 465 | #endregion 466 | 467 | #region ExecuteDictionary 468 | 469 | public Dictionary ExecuteDictionary(string commandText, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbConnection connection) 470 | { 471 | var command = CreateCommand(commandText); 472 | return ExecuteDictionary(command, keyConverter, valueConverter, startRecord, maxRecords, connection); 473 | } 474 | 475 | public Dictionary ExecuteDictionary(string commandText, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbTransaction transaction) 476 | { 477 | var command = CreateCommand(commandText); 478 | return ExecuteDictionary(command, keyConverter, valueConverter, startRecord, maxRecords, transaction); 479 | } 480 | 481 | public Dictionary ExecuteDictionary(string commandText, Converter keyConverter, Converter valueConverter, DbConnection connection) 482 | { 483 | var command = CreateCommand(commandText); 484 | return ExecuteDictionary(command, keyConverter, valueConverter, connection); 485 | } 486 | 487 | public Dictionary ExecuteDictionary(string commandText, Converter keyConverter, Converter valueConverter, DbTransaction transaction) 488 | { 489 | var command = CreateCommand(commandText); 490 | return ExecuteDictionary(command, keyConverter, valueConverter, transaction); 491 | } 492 | 493 | public Dictionary ExecuteDictionary(string commandText, int startRecord, int maxRecords, DbConnection connection) 494 | { 495 | var command = CreateCommand(commandText); 496 | return ExecuteDictionary(command, startRecord, maxRecords, connection); 497 | } 498 | 499 | public Dictionary ExecuteDictionary(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 500 | { 501 | var command = CreateCommand(commandText); 502 | return ExecuteDictionary(command, startRecord, maxRecords, transaction); 503 | } 504 | 505 | public Dictionary ExecuteDictionary(string commandText, DbConnection connection) 506 | { 507 | var command = CreateCommand(commandText); 508 | return ExecuteDictionary(command, connection); 509 | } 510 | 511 | public Dictionary ExecuteDictionary(string commandText, DbTransaction transaction) 512 | { 513 | var command = CreateCommand(commandText); 514 | return ExecuteDictionary(command, transaction); 515 | } 516 | 517 | public Dictionary ExecuteDictionary(string commandText, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords) 518 | { 519 | var command = CreateCommand(commandText); 520 | return ExecuteDictionary(command, keyConverter, valueConverter, startRecord, maxRecords); 521 | } 522 | 523 | public Dictionary ExecuteDictionary(string commandText, Converter keyConverter, Converter valueConverter) 524 | { 525 | var command = CreateCommand(commandText); 526 | return ExecuteDictionary(command, keyConverter, valueConverter); 527 | } 528 | 529 | public Dictionary ExecuteDictionary(string commandText, int startRecord, int maxRecords) 530 | { 531 | var command = CreateCommand(commandText); 532 | return ExecuteDictionary(command, startRecord, maxRecords); 533 | } 534 | 535 | public Dictionary ExecuteDictionary(string commandText) 536 | { 537 | var command = CreateCommand(commandText); 538 | return ExecuteDictionary(command); 539 | } 540 | 541 | #endregion 542 | 543 | #region ExecuteDictionaryAsync 544 | 545 | public Task> ExecuteDictionaryAsync(string commandText, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbConnection connection) 546 | { 547 | var command = CreateCommand(commandText); 548 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, startRecord, maxRecords, connection); 549 | } 550 | 551 | public Task> ExecuteDictionaryAsync(string commandText, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbTransaction transaction) 552 | { 553 | var command = CreateCommand(commandText); 554 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, startRecord, maxRecords, transaction); 555 | } 556 | 557 | public Task> ExecuteDictionaryAsync(string commandText, Converter keyConverter, Converter valueConverter, DbConnection connection) 558 | { 559 | var command = CreateCommand(commandText); 560 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, connection); 561 | } 562 | 563 | public Task> ExecuteDictionaryAsync(string commandText, Converter keyConverter, Converter valueConverter, DbTransaction transaction) 564 | { 565 | var command = CreateCommand(commandText); 566 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, transaction); 567 | } 568 | 569 | public Task> ExecuteDictionaryAsync(string commandText, int startRecord, int maxRecords, DbConnection connection) 570 | { 571 | var command = CreateCommand(commandText); 572 | return ExecuteDictionaryAsync(command, startRecord, maxRecords, connection); 573 | } 574 | 575 | public Task> ExecuteDictionaryAsync(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 576 | { 577 | var command = CreateCommand(commandText); 578 | return ExecuteDictionaryAsync(command, startRecord, maxRecords, transaction); 579 | } 580 | 581 | public Task> ExecuteDictionaryAsync(string commandText, DbConnection connection) 582 | { 583 | var command = CreateCommand(commandText); 584 | return ExecuteDictionaryAsync(command, connection); 585 | } 586 | 587 | public Task> ExecuteDictionaryAsync(string commandText, DbTransaction transaction) 588 | { 589 | var command = CreateCommand(commandText); 590 | return ExecuteDictionaryAsync(command, transaction); 591 | } 592 | 593 | public Task> ExecuteDictionaryAsync(string commandText, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords) 594 | { 595 | var command = CreateCommand(commandText); 596 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, startRecord, maxRecords); 597 | } 598 | 599 | public Task> ExecuteDictionaryAsync(string commandText, Converter keyConverter, Converter valueConverter) 600 | { 601 | var command = CreateCommand(commandText); 602 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter); 603 | } 604 | 605 | public Task> ExecuteDictionaryAsync(string commandText, int startRecord, int maxRecords) 606 | { 607 | var command = CreateCommand(commandText); 608 | return ExecuteDictionaryAsync(command, startRecord, maxRecords); 609 | } 610 | 611 | public Task> ExecuteDictionaryAsync(string commandText) 612 | { 613 | var command = CreateCommand(commandText); 614 | return ExecuteDictionaryAsync(command); 615 | } 616 | 617 | #endregion 618 | 619 | #region ExecuteObject 620 | 621 | public T ExecuteObject(string commandText, Converter converter, DbConnection connection) 622 | { 623 | var command = CreateCommand(commandText); 624 | return ExecuteObject(command, converter, connection); 625 | } 626 | 627 | public T ExecuteObject(string commandText, Converter converter, DbTransaction transaction) 628 | { 629 | var command = CreateCommand(commandText); 630 | return ExecuteObject(command, converter, transaction); 631 | } 632 | 633 | public T ExecuteObject(string commandText, DbConnection connection) 634 | where T : new() 635 | { 636 | var command = CreateCommand(commandText); 637 | return ExecuteObject(command, connection); 638 | } 639 | 640 | public T ExecuteObject(string commandText, DbTransaction transaction) 641 | where T : new() 642 | { 643 | var command = CreateCommand(commandText); 644 | return ExecuteObject(command, transaction); 645 | } 646 | 647 | public T ExecuteObject(string commandText, Converter converter) 648 | { 649 | var command = CreateCommand(commandText); 650 | return ExecuteObject(command, converter); 651 | } 652 | 653 | public T ExecuteObject(string commandText) 654 | where T : new() 655 | { 656 | var command = CreateCommand(commandText); 657 | return ExecuteObject(command); 658 | } 659 | 660 | #endregion 661 | 662 | #region ExecuteObjectAsync 663 | 664 | public Task ExecuteObjectAsync(string commandText, Converter converter, DbConnection connection) 665 | { 666 | var command = CreateCommand(commandText); 667 | return ExecuteObjectAsync(command, converter, connection); 668 | } 669 | 670 | public Task ExecuteObjectAsync(string commandText, Converter converter, DbTransaction transaction) 671 | { 672 | var command = CreateCommand(commandText); 673 | return ExecuteObjectAsync(command, converter, transaction); 674 | } 675 | 676 | public Task ExecuteObjectAsync(string commandText, DbConnection connection) 677 | where T : new() 678 | { 679 | var command = CreateCommand(commandText); 680 | return ExecuteObjectAsync(command, connection); 681 | } 682 | 683 | public Task ExecuteObjectAsync(string commandText, DbTransaction transaction) 684 | where T : new() 685 | { 686 | var command = CreateCommand(commandText); 687 | return ExecuteObjectAsync(command, transaction); 688 | } 689 | 690 | public Task ExecuteObjectAsync(string commandText, Converter converter) 691 | { 692 | var command = CreateCommand(commandText); 693 | return ExecuteObjectAsync(command, converter); 694 | } 695 | 696 | public Task ExecuteObjectAsync(string commandText) 697 | where T : new() 698 | { 699 | var command = CreateCommand(commandText); 700 | return ExecuteObjectAsync(command); 701 | } 702 | 703 | #endregion 704 | 705 | #region ExecuteList 706 | 707 | public List ExecuteList(string commandText, Converter converter, int startRecord, int maxRecords, DbConnection connection) 708 | { 709 | var command = CreateCommand(commandText); 710 | return ExecuteList(command, converter, startRecord, maxRecords, connection); 711 | } 712 | 713 | public List ExecuteList(string commandText, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 714 | { 715 | var command = CreateCommand(commandText); 716 | return ExecuteList(command, converter, startRecord, maxRecords, transaction); 717 | } 718 | 719 | public List ExecuteList(string commandText, Converter converter, DbConnection connection) 720 | { 721 | var command = CreateCommand(commandText); 722 | return ExecuteList(command, converter, connection); 723 | } 724 | 725 | public List ExecuteList(string commandText, Converter converter, DbTransaction transaction) 726 | { 727 | var command = CreateCommand(commandText); 728 | return ExecuteList(command, converter, transaction); 729 | } 730 | 731 | public List ExecuteList(string commandText, int startRecord, int maxRecords, DbConnection connection) 732 | where T : new() 733 | { 734 | var command = CreateCommand(commandText); 735 | return ExecuteList(command, startRecord, maxRecords, connection); 736 | } 737 | 738 | public List ExecuteList(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 739 | where T : new() 740 | { 741 | var command = CreateCommand(commandText); 742 | return ExecuteList(command, startRecord, maxRecords, transaction); 743 | } 744 | 745 | public List ExecuteList(string commandText, DbConnection connection) 746 | where T : new() 747 | { 748 | var command = CreateCommand(commandText); 749 | return ExecuteList(command, connection); 750 | } 751 | 752 | public List ExecuteList(string commandText, DbTransaction transaction) 753 | where T : new() 754 | { 755 | var command = CreateCommand(commandText); 756 | return ExecuteList(command, transaction); 757 | } 758 | 759 | public List ExecuteList(string commandText, Converter converter, int startRecord, int maxRecords) 760 | { 761 | var command = CreateCommand(commandText); 762 | return ExecuteList(command, converter, startRecord, maxRecords); 763 | } 764 | 765 | public List ExecuteList(string commandText, Converter converter) 766 | { 767 | var command = CreateCommand(commandText); 768 | return ExecuteList(command, converter); 769 | } 770 | 771 | public List ExecuteList(string commandText, int startRecord, int maxRecords) 772 | where T : new() 773 | { 774 | var command = CreateCommand(commandText); 775 | return ExecuteList(command, startRecord, maxRecords); 776 | } 777 | 778 | public List ExecuteList(string commandText) 779 | where T : new() 780 | { 781 | var command = CreateCommand(commandText); 782 | return ExecuteList(command); 783 | } 784 | 785 | #endregion 786 | 787 | #region ExecuteListAsync 788 | 789 | public Task> ExecuteListAsync(string commandText, Converter converter, int startRecord, int maxRecords, DbConnection connection) 790 | { 791 | var command = CreateCommand(commandText); 792 | return ExecuteListAsync(command, converter, startRecord, maxRecords, connection); 793 | } 794 | 795 | public Task> ExecuteListAsync(string commandText, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 796 | { 797 | var command = CreateCommand(commandText); 798 | return ExecuteListAsync(command, converter, startRecord, maxRecords, transaction); 799 | } 800 | 801 | public Task> ExecuteListAsync(string commandText, Converter converter, DbConnection connection) 802 | { 803 | var command = CreateCommand(commandText); 804 | return ExecuteListAsync(command, converter, connection); 805 | } 806 | 807 | public Task> ExecuteListAsync(string commandText, Converter converter, DbTransaction transaction) 808 | { 809 | var command = CreateCommand(commandText); 810 | return ExecuteListAsync(command, converter, transaction); 811 | } 812 | 813 | public Task> ExecuteListAsync(string commandText, int startRecord, int maxRecords, DbConnection connection) 814 | where T : new() 815 | { 816 | var command = CreateCommand(commandText); 817 | return ExecuteListAsync(command, startRecord, maxRecords, connection); 818 | } 819 | 820 | public Task> ExecuteListAsync(string commandText, int startRecord, int maxRecords, DbTransaction transaction) 821 | where T : new() 822 | { 823 | var command = CreateCommand(commandText); 824 | return ExecuteListAsync(command, startRecord, maxRecords, transaction); 825 | } 826 | 827 | public Task> ExecuteListAsync(string commandText, DbConnection connection) 828 | where T : new() 829 | { 830 | var command = CreateCommand(commandText); 831 | return ExecuteListAsync(command, connection); 832 | } 833 | 834 | public Task> ExecuteListAsync(string commandText, DbTransaction transaction) 835 | where T : new() 836 | { 837 | var command = CreateCommand(commandText); 838 | return ExecuteListAsync(command, transaction); 839 | } 840 | 841 | public Task> ExecuteListAsync(string commandText, Converter converter, int startRecord, int maxRecords) 842 | { 843 | var command = CreateCommand(commandText); 844 | return ExecuteListAsync(command, converter, startRecord, maxRecords); 845 | } 846 | 847 | public Task> ExecuteListAsync(string commandText, Converter converter) 848 | { 849 | var command = CreateCommand(commandText); 850 | return ExecuteListAsync(command, converter); 851 | } 852 | 853 | public Task> ExecuteListAsync(string commandText, int startRecord, int maxRecords) 854 | where T : new() 855 | { 856 | var command = CreateCommand(commandText); 857 | return ExecuteListAsync(command, startRecord, maxRecords); 858 | } 859 | 860 | public Task> ExecuteListAsync(string commandText) 861 | where T : new() 862 | { 863 | var command = CreateCommand(commandText); 864 | return ExecuteListAsync(command); 865 | } 866 | 867 | #endregion 868 | } 869 | } 870 | -------------------------------------------------------------------------------- /Src/DBHelpers/DBHelper.Core.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // Copyright 2010-2015 Natan Vivo - http://github.com/nvivo/dbhelpers 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | #endregion 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Configuration; 20 | using System.Data; 21 | using System.Data.Common; 22 | using System.Globalization; 23 | using System.Reflection; 24 | using System.Threading.Tasks; 25 | 26 | namespace DBHelpers 27 | { 28 | public partial class DBHelper 29 | { 30 | #region Constructors 31 | 32 | public DBHelper(DbProviderFactory providerFactory, string connectionString) 33 | { 34 | if (providerFactory == null) 35 | throw new ArgumentNullException("providerFactory", "You must provide a DbProviderFactory instance."); 36 | 37 | if (String.IsNullOrEmpty(connectionString)) 38 | throw new ArgumentException("The connection string cannot be empty.", "connectionString"); 39 | 40 | _factory = providerFactory; 41 | _connectionString = connectionString; 42 | } 43 | 44 | public DBHelper(string connectionStringName) 45 | { 46 | ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[connectionStringName]; 47 | 48 | if (css == null) 49 | throw new ArgumentException("The connection string you specified does not exist in your configuration file."); 50 | 51 | _factory = DbProviderFactories.GetFactory(css.ProviderName); 52 | _connectionString = css.ConnectionString; 53 | } 54 | 55 | #endregion 56 | 57 | #region Properties 58 | 59 | private DbProviderFactory _factory; 60 | private string _connectionString; 61 | 62 | public DbProviderFactory Factory 63 | { 64 | get 65 | { 66 | return _factory; 67 | } 68 | } 69 | 70 | public string ConnectionString 71 | { 72 | get 73 | { 74 | return _connectionString; 75 | } 76 | } 77 | 78 | #endregion 79 | 80 | #region Private Helpers 81 | 82 | protected static void FillFromReader(DbDataReader reader, int startRecord, int maxRecords, Action action) 83 | { 84 | if (startRecord < 0) 85 | throw new ArgumentOutOfRangeException("startRecord", "StartRecord must be zero or higher."); 86 | 87 | while (startRecord > 0) 88 | { 89 | if (!reader.Read()) 90 | return; 91 | 92 | startRecord--; 93 | } 94 | 95 | if (maxRecords > 0) 96 | { 97 | int i = 0; 98 | 99 | while (i < maxRecords && reader.Read()) 100 | { 101 | action(reader); 102 | i++; 103 | } 104 | } 105 | else 106 | { 107 | while (reader.Read()) 108 | action(reader); 109 | } 110 | } 111 | 112 | protected static async Task FillFromReaderAsync(DbDataReader reader, int startRecord, int maxRecords, Action action) 113 | { 114 | if (startRecord < 0) 115 | throw new ArgumentOutOfRangeException("startRecord", "StartRecord must be zero or higher."); 116 | 117 | while (startRecord > 0) 118 | { 119 | if (!await reader.ReadAsync()) 120 | return; 121 | 122 | startRecord--; 123 | } 124 | 125 | if (maxRecords > 0) 126 | { 127 | int i = 0; 128 | 129 | while (i < maxRecords && await reader.ReadAsync()) 130 | { 131 | action(reader); 132 | i++; 133 | } 134 | } 135 | else 136 | { 137 | while (await reader.ReadAsync()) 138 | action(reader); 139 | } 140 | } 141 | 142 | private string GetProviderParameterFormatString() 143 | { 144 | var builder = Factory.CreateCommandBuilder(); 145 | var type = builder.GetType(); 146 | var method = type.GetMethod("GetParameterPlaceholder", BindingFlags.NonPublic | BindingFlags.Instance); 147 | var index = 42; 148 | var parameterName = method.Invoke(builder, new object[] { index }).ToString(); 149 | return parameterName.Replace(index.ToString(CultureInfo.InvariantCulture), "{0}"); 150 | } 151 | 152 | #endregion 153 | 154 | #region Helper Methods and Extension Points 155 | 156 | public DbConnection CreateConnection() 157 | { 158 | DbConnection connection = Factory.CreateConnection(); 159 | connection.ConnectionString = ConnectionString; 160 | 161 | return connection; 162 | } 163 | 164 | public DbCommand CreateCommand(string commandText, params object[] parameters) 165 | { 166 | var len = parameters.Length; 167 | 168 | var command = Factory.CreateCommand(); 169 | command.CommandType = CommandType.Text; 170 | 171 | if (len > 0) 172 | { 173 | var formatValues = new string[len]; 174 | 175 | for (var i = 0; i < len; i++) 176 | { 177 | var parameter = parameters[i]; 178 | var rawValue = parameter as RawValue; 179 | 180 | if (rawValue != null) 181 | { 182 | formatValues[i] = rawValue.Value; 183 | } 184 | else 185 | { 186 | var dbParameter = Factory.CreateParameter(); 187 | var name = CreateParameterName(i); 188 | 189 | dbParameter.ParameterName = name; 190 | dbParameter.Value = parameter ?? DBNull.Value; 191 | 192 | formatValues[i] = name; 193 | command.Parameters.Add(dbParameter); 194 | } 195 | } 196 | 197 | command.CommandText = String.Format(commandText, formatValues); 198 | } 199 | else 200 | { 201 | command.CommandText = commandText; 202 | } 203 | 204 | return command; 205 | } 206 | 207 | private string _parameterFormat; 208 | 209 | protected virtual string CreateParameterName(int index) 210 | { 211 | if (_parameterFormat == null) 212 | _parameterFormat = GetProviderParameterFormatString(); 213 | 214 | return String.Format(_parameterFormat, index); 215 | } 216 | 217 | protected virtual Converter GetTypeConverter() 218 | { 219 | return (object o) => (T)DBConvert.To(o); 220 | } 221 | 222 | protected virtual Converter GetDataReaderConverter() 223 | where T : new() 224 | { 225 | return new DataReaderConverter().Convert; 226 | } 227 | 228 | protected virtual void OnExecuteCommand(DbCommand command) 229 | { } 230 | 231 | #endregion 232 | 233 | #region ExecuteNonQuery 234 | 235 | public int ExecuteNonQuery(DbCommand command, DbConnection connection) 236 | { 237 | OnExecuteCommand(command); 238 | 239 | command.Connection = connection; 240 | 241 | return command.ExecuteNonQuery(); 242 | } 243 | 244 | public int ExecuteNonQuery(DbCommand command, DbTransaction transaction) 245 | { 246 | OnExecuteCommand(command); 247 | 248 | command.Connection = transaction.Connection; 249 | command.Transaction = transaction; 250 | 251 | return command.ExecuteNonQuery(); 252 | } 253 | 254 | public int ExecuteNonQuery(DbCommand command) 255 | { 256 | int affectedRows; 257 | 258 | using (DbConnection connection = CreateConnection()) 259 | { 260 | connection.Open(); 261 | 262 | affectedRows = ExecuteNonQuery(command, connection); 263 | 264 | connection.Close(); 265 | } 266 | 267 | return affectedRows; 268 | } 269 | 270 | #endregion 271 | 272 | #region ExecuteNonQueryAsync 273 | 274 | public Task ExecuteNonQueryAsync(DbCommand command, DbConnection connection) 275 | { 276 | OnExecuteCommand(command); 277 | 278 | command.Connection = connection; 279 | 280 | return command.ExecuteNonQueryAsync(); 281 | } 282 | 283 | public Task ExecuteNonQueryAsync(DbCommand command, DbTransaction transaction) 284 | { 285 | OnExecuteCommand(command); 286 | 287 | command.Connection = transaction.Connection; 288 | command.Transaction = transaction; 289 | 290 | return command.ExecuteNonQueryAsync(); 291 | } 292 | 293 | public async Task ExecuteNonQueryAsync(DbCommand command) 294 | { 295 | int affectedRows; 296 | 297 | using (DbConnection connection = CreateConnection()) 298 | { 299 | await connection.OpenAsync(); 300 | 301 | affectedRows = await ExecuteNonQueryAsync(command, connection); 302 | 303 | connection.Close(); 304 | } 305 | 306 | return affectedRows; 307 | } 308 | 309 | #endregion 310 | 311 | #region ExecuteScalar 312 | 313 | public T ExecuteScalar(DbCommand command, Converter converter, DbConnection connection) 314 | { 315 | OnExecuteCommand(command); 316 | 317 | command.Connection = connection; 318 | 319 | var value = command.ExecuteScalar(); 320 | 321 | return converter(value); 322 | } 323 | 324 | public T ExecuteScalar(DbCommand command, Converter converter, DbTransaction transaction) 325 | { 326 | OnExecuteCommand(command); 327 | 328 | command.Connection = transaction.Connection; 329 | command.Transaction = transaction; 330 | 331 | var value = command.ExecuteScalar(); 332 | 333 | return converter(value); 334 | } 335 | 336 | public T ExecuteScalar(DbCommand command, Converter converter) 337 | { 338 | T o; 339 | 340 | using (DbConnection connection = CreateConnection()) 341 | { 342 | connection.Open(); 343 | 344 | o = ExecuteScalar(command, converter, connection); 345 | 346 | connection.Close(); 347 | } 348 | 349 | return o; 350 | } 351 | 352 | public T ExecuteScalar(DbCommand command, DbTransaction transaction) 353 | { 354 | return ExecuteScalar(command, GetTypeConverter(), transaction); 355 | } 356 | 357 | public T ExecuteScalar(DbCommand command, DbConnection connection) 358 | { 359 | return ExecuteScalar(command, GetTypeConverter(), connection); 360 | } 361 | 362 | public T ExecuteScalar(DbCommand command) 363 | { 364 | return ExecuteScalar(command, GetTypeConverter()); 365 | } 366 | 367 | #endregion 368 | 369 | #region ExecuteScalarAsync 370 | 371 | public async Task ExecuteScalarAsync(DbCommand command, Converter converter, DbConnection connection) 372 | { 373 | OnExecuteCommand(command); 374 | 375 | command.Connection = connection; 376 | 377 | var value = await command.ExecuteScalarAsync(); 378 | 379 | return converter(value); 380 | } 381 | 382 | public async Task ExecuteScalarAsync(DbCommand command, Converter converter, DbTransaction transaction) 383 | { 384 | OnExecuteCommand(command); 385 | 386 | command.Connection = transaction.Connection; 387 | command.Transaction = transaction; 388 | 389 | var value = await command.ExecuteScalarAsync(); 390 | 391 | return converter(value); 392 | } 393 | 394 | public async Task ExecuteScalarAsync(DbCommand command, Converter converter) 395 | { 396 | T o; 397 | 398 | using (DbConnection connection = CreateConnection()) 399 | { 400 | await connection.OpenAsync(); 401 | 402 | o = await ExecuteScalarAsync(command, converter, connection); 403 | 404 | connection.Close(); 405 | } 406 | 407 | return o; 408 | } 409 | 410 | public Task ExecuteScalarAsync(DbCommand command, DbConnection connection) 411 | { 412 | return ExecuteScalarAsync(command, GetTypeConverter(), connection); 413 | } 414 | 415 | public Task ExecuteScalarAsync(DbCommand command, DbTransaction transaction) 416 | { 417 | return ExecuteScalarAsync(command, GetTypeConverter(), transaction); 418 | } 419 | 420 | public Task ExecuteScalarAsync(DbCommand command) 421 | { 422 | return ExecuteScalarAsync(command, GetTypeConverter()); 423 | } 424 | 425 | #endregion 426 | 427 | #region ExecuteReader 428 | 429 | public DbDataReader ExecuteReader(DbCommand command, DbConnection connection) 430 | { 431 | OnExecuteCommand(command); 432 | 433 | command.Connection = connection; 434 | 435 | return command.ExecuteReader(); 436 | } 437 | 438 | public DbDataReader ExecuteReader(DbCommand command, DbTransaction transaction) 439 | { 440 | OnExecuteCommand(command); 441 | 442 | command.Connection = transaction.Connection; 443 | command.Transaction = transaction; 444 | 445 | return command.ExecuteReader(); 446 | } 447 | 448 | public DbDataReader ExecuteReader(DbCommand command) 449 | { 450 | OnExecuteCommand(command); 451 | 452 | DbConnection connection = CreateConnection(); 453 | command.Connection = connection; 454 | connection.Open(); 455 | 456 | return command.ExecuteReader(CommandBehavior.CloseConnection); 457 | } 458 | 459 | #endregion 460 | 461 | #region ExecuteReaderAsync 462 | 463 | public Task ExecuteReaderAsync(DbCommand command, DbConnection connection) 464 | { 465 | OnExecuteCommand(command); 466 | 467 | command.Connection = connection; 468 | 469 | return command.ExecuteReaderAsync(); 470 | } 471 | 472 | public Task ExecuteReaderAsync(DbCommand command, DbTransaction transaction) 473 | { 474 | OnExecuteCommand(command); 475 | 476 | command.Connection = transaction.Connection; 477 | command.Transaction = transaction; 478 | 479 | return command.ExecuteReaderAsync(); 480 | } 481 | 482 | public async Task ExecuteReaderAsync(DbCommand command) 483 | { 484 | OnExecuteCommand(command); 485 | 486 | DbConnection connection = CreateConnection(); 487 | command.Connection = connection; 488 | await connection.OpenAsync(); 489 | 490 | return await command.ExecuteReaderAsync(CommandBehavior.CloseConnection); 491 | } 492 | 493 | #endregion 494 | 495 | #region ExecuteDataTable 496 | 497 | public DataTable ExecuteDataTable(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 498 | { 499 | OnExecuteCommand(command); 500 | 501 | command.Connection = connection; 502 | 503 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 504 | adapter.SelectCommand = command; 505 | 506 | DataTable dt = new DataTable(); 507 | 508 | if (startRecord >= 0 || maxRecords >= 0) 509 | adapter.Fill(startRecord, maxRecords, dt); 510 | else 511 | adapter.Fill(dt); 512 | 513 | return dt; 514 | } 515 | 516 | public DataTable ExecuteDataTable(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 517 | { 518 | OnExecuteCommand(command); 519 | 520 | command.Connection = transaction.Connection; 521 | command.Transaction = transaction; 522 | 523 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 524 | adapter.SelectCommand = command; 525 | 526 | DataTable dt = new DataTable(); 527 | 528 | if (startRecord >= 0 || maxRecords >= 0) 529 | adapter.Fill(startRecord, maxRecords, dt); 530 | else 531 | adapter.Fill(dt); 532 | 533 | return dt; 534 | } 535 | 536 | public DataTable ExecuteDataTable(DbCommand command, int startRecord, int maxRecords) 537 | { 538 | DataTable dt; 539 | 540 | using (DbConnection connection = CreateConnection()) 541 | { 542 | connection.Open(); 543 | 544 | dt = ExecuteDataTable(command, startRecord, maxRecords, connection); 545 | 546 | connection.Close(); 547 | } 548 | 549 | return dt; 550 | } 551 | 552 | public DataTable ExecuteDataTable(DbCommand command, DbConnection connection) 553 | { 554 | return ExecuteDataTable(command, 0, 0, connection); 555 | } 556 | 557 | public DataTable ExecuteDataTable(DbCommand command, DbTransaction transaction) 558 | { 559 | return ExecuteDataTable(command, 0, 0, transaction); 560 | } 561 | 562 | public DataTable ExecuteDataTable(DbCommand command) 563 | { 564 | return ExecuteDataTable(command, 0, 0); 565 | } 566 | 567 | #endregion 568 | 569 | #region ExecuteDataTableAsync 570 | 571 | public Task ExecuteDataTableAsync(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 572 | { 573 | OnExecuteCommand(command); 574 | 575 | command.Connection = connection; 576 | 577 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 578 | adapter.SelectCommand = command; 579 | 580 | DataTable dt = new DataTable(); 581 | 582 | if (startRecord >= 0 || maxRecords >= 0) 583 | adapter.Fill(startRecord, maxRecords, dt); 584 | else 585 | adapter.Fill(dt); 586 | 587 | return Task.FromResult(dt); 588 | } 589 | 590 | public Task ExecuteDataTableAsync(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 591 | { 592 | OnExecuteCommand(command); 593 | 594 | command.Connection = transaction.Connection; 595 | command.Transaction = transaction; 596 | 597 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 598 | adapter.SelectCommand = command; 599 | 600 | DataTable dt = new DataTable(); 601 | 602 | if (startRecord >= 0 || maxRecords >= 0) 603 | adapter.Fill(startRecord, maxRecords, dt); 604 | else 605 | adapter.Fill(dt); 606 | 607 | return Task.FromResult(dt); 608 | } 609 | 610 | public async Task ExecuteDataTableAsync(DbCommand command, int startRecord, int maxRecords) 611 | { 612 | DataTable dt; 613 | 614 | using (DbConnection connection = CreateConnection()) 615 | { 616 | await connection.OpenAsync(); 617 | 618 | dt = await ExecuteDataTableAsync(command, startRecord, maxRecords, connection); 619 | 620 | connection.Close(); 621 | } 622 | 623 | return dt; 624 | } 625 | 626 | public Task ExecuteDataTableAsync(DbCommand command, DbConnection connection) 627 | { 628 | return ExecuteDataTableAsync(command, 0, 0, connection); 629 | } 630 | 631 | public Task ExecuteDataTableAsync(DbCommand command, DbTransaction transaction) 632 | { 633 | return ExecuteDataTableAsync(command, 0, 0, transaction); 634 | } 635 | 636 | public Task ExecuteDataTableAsync(DbCommand command) 637 | { 638 | return ExecuteDataTableAsync(command, 0, 0); 639 | } 640 | 641 | #endregion 642 | 643 | #region ExecuteDataSet 644 | 645 | public DataSet ExecuteDataSet(DbCommand command, DbConnection connection) 646 | { 647 | OnExecuteCommand(command); 648 | 649 | command.Connection = connection; 650 | 651 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 652 | adapter.SelectCommand = command; 653 | 654 | DataSet ds = new DataSet(); 655 | adapter.Fill(ds); 656 | 657 | return ds; 658 | } 659 | 660 | public DataSet ExecuteDataSet(DbCommand command, DbTransaction transaction) 661 | { 662 | OnExecuteCommand(command); 663 | 664 | command.Connection = transaction.Connection; 665 | command.Transaction = transaction; 666 | 667 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 668 | adapter.SelectCommand = command; 669 | 670 | DataSet ds = new DataSet(); 671 | adapter.Fill(ds); 672 | 673 | return ds; 674 | } 675 | 676 | public DataSet ExecuteDataSet(DbCommand command) 677 | { 678 | DataSet ds; 679 | 680 | using (DbConnection connection = CreateConnection()) 681 | { 682 | connection.Open(); 683 | 684 | ds = ExecuteDataSet(command, connection); 685 | 686 | connection.Close(); 687 | } 688 | 689 | return ds; 690 | } 691 | 692 | #endregion 693 | 694 | #region ExecuteDataSetAsync 695 | 696 | public Task ExecuteDataSetAsync(DbCommand command, DbConnection connection) 697 | { 698 | OnExecuteCommand(command); 699 | 700 | command.Connection = connection; 701 | 702 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 703 | adapter.SelectCommand = command; 704 | 705 | DataSet ds = new DataSet(); 706 | adapter.Fill(ds); 707 | 708 | return Task.FromResult(ds); 709 | } 710 | 711 | public Task ExecuteDataSetAsync(DbCommand command, DbTransaction transaction) 712 | { 713 | OnExecuteCommand(command); 714 | 715 | command.Connection = transaction.Connection; 716 | command.Transaction = transaction; 717 | 718 | DbDataAdapter adapter = Factory.CreateDataAdapter(); 719 | adapter.SelectCommand = command; 720 | 721 | DataSet ds = new DataSet(); 722 | adapter.Fill(ds); 723 | 724 | return Task.FromResult(ds); 725 | } 726 | 727 | public async Task ExecuteDataSetAsync(DbCommand command) 728 | { 729 | DataSet ds; 730 | 731 | using (DbConnection connection = CreateConnection()) 732 | { 733 | await connection.OpenAsync(); 734 | 735 | ds = await ExecuteDataSetAsync(command, connection); 736 | 737 | connection.Close(); 738 | } 739 | 740 | return ds; 741 | } 742 | 743 | #endregion 744 | 745 | #region ExecuteArray 746 | 747 | public T[] ExecuteArray(DbCommand command, Converter converter, int startRecord, int maxRecords, DbConnection connection) 748 | { 749 | List list = new List(); 750 | 751 | using (DbDataReader reader = ExecuteReader(command, connection)) 752 | { 753 | FillFromReader(reader, startRecord, maxRecords, r => 754 | { 755 | list.Add( 756 | converter(r.GetValue(0)) 757 | ); 758 | }); 759 | 760 | reader.Close(); 761 | } 762 | 763 | return list.ToArray(); 764 | } 765 | 766 | public T[] ExecuteArray(DbCommand command, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 767 | { 768 | List list = new List(); 769 | 770 | using (DbDataReader reader = ExecuteReader(command, transaction)) 771 | { 772 | FillFromReader(reader, startRecord, maxRecords, r => 773 | { 774 | list.Add( 775 | converter(r.GetValue(0)) 776 | ); 777 | }); 778 | 779 | reader.Close(); 780 | } 781 | 782 | return list.ToArray(); 783 | } 784 | 785 | public T[] ExecuteArray(DbCommand command, Converter converter, int startRecord, int maxRecords) 786 | { 787 | T[] arr; 788 | 789 | using (DbConnection connection = CreateConnection()) 790 | { 791 | connection.Open(); 792 | 793 | arr = ExecuteArray(command, converter, startRecord, maxRecords, connection); 794 | 795 | connection.Close(); 796 | } 797 | 798 | return arr; 799 | } 800 | 801 | public T[] ExecuteArray(DbCommand command, Converter converter, DbConnection connection) 802 | { 803 | return ExecuteArray(command, converter, 0, 0, connection); 804 | } 805 | 806 | public T[] ExecuteArray(DbCommand command, Converter converter, DbTransaction transaction) 807 | { 808 | return ExecuteArray(command, converter, 0, 0, transaction); 809 | } 810 | 811 | public T[] ExecuteArray(DbCommand command, Converter converter) 812 | { 813 | return ExecuteArray(command, converter, 0, 0); 814 | } 815 | 816 | public T[] ExecuteArray(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 817 | { 818 | return ExecuteArray(command, GetTypeConverter(), startRecord, maxRecords, connection); 819 | } 820 | 821 | public T[] ExecuteArray(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 822 | { 823 | return ExecuteArray(command, GetTypeConverter(), startRecord, maxRecords, transaction); 824 | } 825 | 826 | public T[] ExecuteArray(DbCommand command, int startRecord, int maxRecords) 827 | { 828 | return ExecuteArray(command, GetTypeConverter(), startRecord, maxRecords); 829 | } 830 | 831 | public T[] ExecuteArray(DbCommand command, DbConnection connection) 832 | { 833 | return ExecuteArray(command, GetTypeConverter(), connection); 834 | } 835 | 836 | public T[] ExecuteArray(DbCommand command, DbTransaction transaction) 837 | { 838 | return ExecuteArray(command, GetTypeConverter(), transaction); 839 | } 840 | 841 | public T[] ExecuteArray(DbCommand command) 842 | { 843 | return ExecuteArray(command, GetTypeConverter()); 844 | } 845 | 846 | #endregion 847 | 848 | #region ExecuteArrayAsync 849 | 850 | public async Task ExecuteArrayAsync(DbCommand command, Converter converter, int startRecord, int maxRecords, DbConnection connection) 851 | { 852 | List list = new List(); 853 | 854 | using (DbDataReader reader = await ExecuteReaderAsync(command, connection)) 855 | { 856 | await FillFromReaderAsync(reader, startRecord, maxRecords, r => 857 | { 858 | list.Add(converter(r.GetValue(0))); 859 | }); 860 | 861 | reader.Close(); 862 | } 863 | 864 | return list.ToArray(); 865 | } 866 | 867 | public async Task ExecuteArrayAsync(DbCommand command, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 868 | { 869 | List list = new List(); 870 | 871 | using (DbDataReader reader = await ExecuteReaderAsync(command, transaction)) 872 | { 873 | await FillFromReaderAsync(reader, startRecord, maxRecords, r => 874 | { 875 | list.Add(converter(r.GetValue(0))); 876 | }); 877 | 878 | reader.Close(); 879 | } 880 | 881 | return list.ToArray(); 882 | } 883 | 884 | public async Task ExecuteArrayAsync(DbCommand command, Converter converter, int startRecord, int maxRecords) 885 | { 886 | T[] arr; 887 | 888 | using (DbConnection connection = CreateConnection()) 889 | { 890 | await connection.OpenAsync(); 891 | 892 | arr = await ExecuteArrayAsync(command, converter, startRecord, maxRecords, connection); 893 | 894 | connection.Close(); 895 | } 896 | 897 | return arr; 898 | } 899 | 900 | public Task ExecuteArrayAsync(DbCommand command, Converter converter, DbConnection connection) 901 | { 902 | return ExecuteArrayAsync(command, converter, 0, 0, connection); 903 | } 904 | 905 | public Task ExecuteArrayAsync(DbCommand command, Converter converter, DbTransaction transaction) 906 | { 907 | return ExecuteArrayAsync(command, converter, 0, 0, transaction); 908 | } 909 | 910 | public Task ExecuteArrayAsync(DbCommand command, Converter converter) 911 | { 912 | return ExecuteArrayAsync(command, converter, 0, 0); 913 | } 914 | 915 | public Task ExecuteArrayAsync(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 916 | { 917 | return ExecuteArrayAsync(command, GetTypeConverter(), startRecord, maxRecords, connection); 918 | } 919 | 920 | public Task ExecuteArrayAsync(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 921 | { 922 | return ExecuteArrayAsync(command, GetTypeConverter(), startRecord, maxRecords, transaction); 923 | } 924 | 925 | public Task ExecuteArrayAsync(DbCommand command, int startRecord, int maxRecords) 926 | { 927 | return ExecuteArrayAsync(command, GetTypeConverter(), startRecord, maxRecords); 928 | } 929 | 930 | public Task ExecuteArrayAsync(DbCommand command, DbConnection connection) 931 | { 932 | return ExecuteArrayAsync(command, GetTypeConverter(), connection); 933 | } 934 | 935 | public Task ExecuteArrayAsync(DbCommand command, DbTransaction transaction) 936 | { 937 | return ExecuteArrayAsync(command, GetTypeConverter(), transaction); 938 | } 939 | 940 | public Task ExecuteArrayAsync(DbCommand command) 941 | { 942 | return ExecuteArrayAsync(command, GetTypeConverter()); 943 | } 944 | 945 | #endregion 946 | 947 | #region ExecuteDictionary 948 | 949 | public Dictionary ExecuteDictionary(DbCommand command, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbConnection connection) 950 | { 951 | Dictionary dict = new Dictionary(); 952 | 953 | using (DbDataReader reader = ExecuteReader(command, connection)) 954 | { 955 | FillFromReader(reader, startRecord, maxRecords, r => 956 | { 957 | dict.Add( 958 | keyConverter(r.GetValue(0)), 959 | valueConverter(r.GetValue(1)) 960 | ); 961 | }); 962 | 963 | reader.Close(); 964 | } 965 | 966 | return dict; 967 | } 968 | 969 | public Dictionary ExecuteDictionary(DbCommand command, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbTransaction transaction) 970 | { 971 | Dictionary dict = new Dictionary(); 972 | 973 | using (DbDataReader reader = ExecuteReader(command, transaction)) 974 | { 975 | FillFromReader(reader, startRecord, maxRecords, r => 976 | { 977 | dict.Add( 978 | keyConverter(r.GetValue(0)), 979 | valueConverter(r.GetValue(1)) 980 | ); 981 | }); 982 | 983 | reader.Close(); 984 | } 985 | 986 | return dict; 987 | } 988 | 989 | public Dictionary ExecuteDictionary(DbCommand command, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords) 990 | { 991 | Dictionary dict; 992 | 993 | using (DbConnection connection = CreateConnection()) 994 | { 995 | connection.Open(); 996 | 997 | dict = ExecuteDictionary(command, keyConverter, valueConverter, startRecord, maxRecords, connection); 998 | 999 | connection.Close(); 1000 | } 1001 | 1002 | return dict; 1003 | } 1004 | 1005 | public Dictionary ExecuteDictionary(DbCommand command, Converter keyConverter, Converter valueConverter, DbConnection connection) 1006 | { 1007 | return ExecuteDictionary(command, keyConverter, valueConverter, 0, 0, connection); 1008 | } 1009 | 1010 | public Dictionary ExecuteDictionary(DbCommand command, Converter keyConverter, Converter valueConverter, DbTransaction transaction) 1011 | { 1012 | return ExecuteDictionary(command, keyConverter, valueConverter, 0, 0, transaction); 1013 | } 1014 | 1015 | public Dictionary ExecuteDictionary(DbCommand command, Converter keyConverter, Converter valueConverter) 1016 | { 1017 | return ExecuteDictionary(command, keyConverter, valueConverter, 0, 0); 1018 | } 1019 | 1020 | public Dictionary ExecuteDictionary(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 1021 | { 1022 | return ExecuteDictionary(command, GetTypeConverter(), GetTypeConverter(), startRecord, maxRecords, connection); 1023 | } 1024 | 1025 | public Dictionary ExecuteDictionary(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 1026 | { 1027 | return ExecuteDictionary(command, GetTypeConverter(), GetTypeConverter(), startRecord, maxRecords, transaction); 1028 | } 1029 | 1030 | public Dictionary ExecuteDictionary(DbCommand command, int startRecord, int maxRecords) 1031 | { 1032 | return ExecuteDictionary(command, GetTypeConverter(), GetTypeConverter(), startRecord, maxRecords); 1033 | } 1034 | 1035 | public Dictionary ExecuteDictionary(DbCommand command, DbConnection connection) 1036 | { 1037 | return ExecuteDictionary(command, GetTypeConverter(), GetTypeConverter(), connection); 1038 | } 1039 | 1040 | public Dictionary ExecuteDictionary(DbCommand command, DbTransaction transaction) 1041 | { 1042 | return ExecuteDictionary(command, GetTypeConverter(), GetTypeConverter(), transaction); 1043 | } 1044 | 1045 | public Dictionary ExecuteDictionary(DbCommand command) 1046 | { 1047 | return ExecuteDictionary(command, GetTypeConverter(), GetTypeConverter()); 1048 | } 1049 | 1050 | #endregion 1051 | 1052 | #region ExecuteDictionaryAsync 1053 | 1054 | public async Task> ExecuteDictionaryAsync(DbCommand command, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbConnection connection) 1055 | { 1056 | Dictionary dict = new Dictionary(); 1057 | 1058 | using (DbDataReader reader = await ExecuteReaderAsync(command, connection)) 1059 | { 1060 | await FillFromReaderAsync(reader, startRecord, maxRecords, r => 1061 | { 1062 | dict.Add( 1063 | keyConverter(r.GetValue(0)), 1064 | valueConverter(r.GetValue(1)) 1065 | ); 1066 | }); 1067 | 1068 | reader.Close(); 1069 | } 1070 | 1071 | return dict; 1072 | } 1073 | 1074 | public async Task> ExecuteDictionaryAsync(DbCommand command, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords, DbTransaction transaction) 1075 | { 1076 | Dictionary dict = new Dictionary(); 1077 | 1078 | using (DbDataReader reader = await ExecuteReaderAsync(command, transaction)) 1079 | { 1080 | await FillFromReaderAsync(reader, startRecord, maxRecords, r => 1081 | { 1082 | dict.Add( 1083 | keyConverter(r.GetValue(0)), 1084 | valueConverter(r.GetValue(1)) 1085 | ); 1086 | }); 1087 | 1088 | reader.Close(); 1089 | } 1090 | 1091 | return dict; 1092 | } 1093 | 1094 | public async Task> ExecuteDictionaryAsync(DbCommand command, Converter keyConverter, Converter valueConverter, int startRecord, int maxRecords) 1095 | { 1096 | Dictionary dict; 1097 | 1098 | using (DbConnection connection = CreateConnection()) 1099 | { 1100 | await connection.OpenAsync(); 1101 | 1102 | dict = await ExecuteDictionaryAsync(command, keyConverter, valueConverter, startRecord, maxRecords, connection); 1103 | 1104 | connection.Close(); 1105 | } 1106 | 1107 | return dict; 1108 | } 1109 | 1110 | public Task> ExecuteDictionaryAsync(DbCommand command, Converter keyConverter, Converter valueConverter, DbConnection connection) 1111 | { 1112 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, 0, 0, connection); 1113 | } 1114 | 1115 | public Task> ExecuteDictionaryAsync(DbCommand command, Converter keyConverter, Converter valueConverter, DbTransaction transaction) 1116 | { 1117 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, 0, 0, transaction); 1118 | } 1119 | 1120 | public Task> ExecuteDictionaryAsync(DbCommand command, Converter keyConverter, Converter valueConverter) 1121 | { 1122 | return ExecuteDictionaryAsync(command, keyConverter, valueConverter, 0, 0); 1123 | } 1124 | 1125 | public Task> ExecuteDictionaryAsync(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 1126 | { 1127 | return ExecuteDictionaryAsync(command, GetTypeConverter(), GetTypeConverter(), startRecord, maxRecords, connection); 1128 | } 1129 | 1130 | public Task> ExecuteDictionaryAsync(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 1131 | { 1132 | return ExecuteDictionaryAsync(command, GetTypeConverter(), GetTypeConverter(), startRecord, maxRecords, transaction); 1133 | } 1134 | 1135 | public Task> ExecuteDictionaryAsync(DbCommand command, int startRecord, int maxRecords) 1136 | { 1137 | return ExecuteDictionaryAsync(command, GetTypeConverter(), GetTypeConverter(), startRecord, maxRecords); 1138 | } 1139 | 1140 | public Task> ExecuteDictionaryAsync(DbCommand command, DbConnection connection) 1141 | { 1142 | return ExecuteDictionaryAsync(command, GetTypeConverter(), GetTypeConverter(), connection); 1143 | } 1144 | 1145 | public Task> ExecuteDictionaryAsync(DbCommand command, DbTransaction transaction) 1146 | { 1147 | return ExecuteDictionaryAsync(command, GetTypeConverter(), GetTypeConverter(), transaction); 1148 | } 1149 | 1150 | public Task> ExecuteDictionaryAsync(DbCommand command) 1151 | { 1152 | return ExecuteDictionaryAsync(command, GetTypeConverter(), GetTypeConverter()); 1153 | } 1154 | 1155 | #endregion 1156 | 1157 | #region ExecuteObject 1158 | 1159 | public T ExecuteObject(DbCommand command, Converter converter, DbConnection connection) 1160 | { 1161 | T o; 1162 | 1163 | using (DbDataReader reader = ExecuteReader(command, connection)) 1164 | { 1165 | if (reader.Read()) 1166 | o = converter(reader); 1167 | else 1168 | o = default(T); 1169 | 1170 | reader.Close(); 1171 | } 1172 | 1173 | return o; 1174 | } 1175 | 1176 | public T ExecuteObject(DbCommand command, Converter converter, DbTransaction transaction) 1177 | { 1178 | T o; 1179 | 1180 | using (DbDataReader reader = ExecuteReader(command, transaction)) 1181 | { 1182 | if (reader.Read()) 1183 | o = converter(reader); 1184 | else 1185 | o = default(T); 1186 | 1187 | reader.Close(); 1188 | } 1189 | 1190 | return o; 1191 | } 1192 | 1193 | public T ExecuteObject(DbCommand command, Converter converter) 1194 | { 1195 | T o; 1196 | 1197 | using (DbConnection connection = CreateConnection()) 1198 | { 1199 | connection.Open(); 1200 | 1201 | o = ExecuteObject(command, converter, connection); 1202 | 1203 | connection.Close(); 1204 | } 1205 | 1206 | return o; 1207 | } 1208 | 1209 | public T ExecuteObject(DbCommand command, DbConnection connection) 1210 | where T : new() 1211 | { 1212 | var converter = GetDataReaderConverter(); 1213 | return ExecuteObject(command, converter, connection); 1214 | } 1215 | 1216 | public T ExecuteObject(DbCommand command, DbTransaction transaction) 1217 | where T : new() 1218 | { 1219 | var converter = GetDataReaderConverter(); 1220 | return ExecuteObject(command, converter, transaction); 1221 | } 1222 | 1223 | public T ExecuteObject(DbCommand command) 1224 | where T : new() 1225 | { 1226 | var converter = GetDataReaderConverter(); 1227 | return ExecuteObject(command, converter); 1228 | } 1229 | 1230 | #endregion 1231 | 1232 | #region ExecuteObjectAsync 1233 | 1234 | public async Task ExecuteObjectAsync(DbCommand command, Converter converter, DbConnection connection) 1235 | { 1236 | T o; 1237 | 1238 | using (DbDataReader reader = await ExecuteReaderAsync(command, connection)) 1239 | { 1240 | if (await reader.ReadAsync()) 1241 | o = converter(reader); 1242 | else 1243 | o = default(T); 1244 | 1245 | reader.Close(); 1246 | } 1247 | 1248 | return o; 1249 | } 1250 | 1251 | public async Task ExecuteObjectAsync(DbCommand command, Converter converter, DbTransaction transaction) 1252 | { 1253 | T o; 1254 | 1255 | using (DbDataReader reader = await ExecuteReaderAsync(command, transaction)) 1256 | { 1257 | if (await reader.ReadAsync()) 1258 | o = converter(reader); 1259 | else 1260 | o = default(T); 1261 | 1262 | reader.Close(); 1263 | } 1264 | 1265 | return o; 1266 | } 1267 | 1268 | public async Task ExecuteObjectAsync(DbCommand command, Converter converter) 1269 | { 1270 | T o; 1271 | 1272 | using (DbConnection connection = CreateConnection()) 1273 | { 1274 | await connection.OpenAsync(); 1275 | 1276 | o = await ExecuteObjectAsync(command, converter, connection); 1277 | 1278 | connection.Close(); 1279 | } 1280 | 1281 | return o; 1282 | } 1283 | 1284 | public Task ExecuteObjectAsync(DbCommand command, DbConnection connection) 1285 | where T : new() 1286 | { 1287 | var converter = GetDataReaderConverter(); 1288 | return ExecuteObjectAsync(command, converter, connection); 1289 | } 1290 | 1291 | public Task ExecuteObjectAsync(DbCommand command, DbTransaction transaction) 1292 | where T : new() 1293 | { 1294 | var converter = GetDataReaderConverter(); 1295 | return ExecuteObjectAsync(command, converter, transaction); 1296 | } 1297 | 1298 | public Task ExecuteObjectAsync(DbCommand command) 1299 | where T : new() 1300 | { 1301 | var converter = GetDataReaderConverter(); 1302 | return ExecuteObjectAsync(command, converter); 1303 | } 1304 | 1305 | #endregion 1306 | 1307 | #region ExecuteList 1308 | 1309 | public List ExecuteList(DbCommand command, Converter converter, int startRecord, int maxRecords, DbConnection connection) 1310 | { 1311 | var list = new List(); 1312 | 1313 | using (DbDataReader reader = ExecuteReader(command, connection)) 1314 | { 1315 | FillFromReader(reader, startRecord, maxRecords, r => 1316 | { 1317 | list.Add(converter(reader)); 1318 | }); 1319 | 1320 | reader.Close(); 1321 | } 1322 | 1323 | return list; 1324 | } 1325 | 1326 | public List ExecuteList(DbCommand command, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 1327 | { 1328 | var list = new List(); 1329 | 1330 | using (DbDataReader reader = ExecuteReader(command, transaction)) 1331 | { 1332 | FillFromReader(reader, startRecord, maxRecords, r => 1333 | { 1334 | list.Add(converter(reader)); 1335 | }); 1336 | 1337 | reader.Close(); 1338 | } 1339 | 1340 | return list; 1341 | } 1342 | 1343 | public List ExecuteList(DbCommand command, Converter converter, int startRecord, int maxRecords) 1344 | { 1345 | List list; 1346 | 1347 | using (DbConnection connection = CreateConnection()) 1348 | { 1349 | connection.Open(); 1350 | 1351 | list = ExecuteList(command, converter, startRecord, maxRecords, connection); 1352 | 1353 | connection.Close(); 1354 | } 1355 | 1356 | return list; 1357 | } 1358 | 1359 | public List ExecuteList(DbCommand command, Converter converter, DbConnection connection) 1360 | { 1361 | return ExecuteList(command, converter, 0, 0, connection); 1362 | } 1363 | 1364 | public List ExecuteList(DbCommand command, Converter converter, DbTransaction transaction) 1365 | { 1366 | return ExecuteList(command, converter, 0, 0, transaction); 1367 | } 1368 | 1369 | public List ExecuteList(DbCommand command, Converter converter) 1370 | { 1371 | return ExecuteList(command, converter, 0, 0); 1372 | } 1373 | 1374 | public List ExecuteList(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 1375 | where T : new() 1376 | { 1377 | var converter = GetDataReaderConverter(); 1378 | return ExecuteList(command, converter, startRecord, maxRecords, connection); 1379 | } 1380 | 1381 | public List ExecuteList(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 1382 | where T : new() 1383 | { 1384 | var converter = GetDataReaderConverter(); 1385 | return ExecuteList(command, converter, startRecord, maxRecords, transaction); 1386 | } 1387 | 1388 | public List ExecuteList(DbCommand command, int startRecord, int maxRecords) 1389 | where T : new() 1390 | { 1391 | var converter = GetDataReaderConverter(); 1392 | return ExecuteList(command, converter, startRecord, maxRecords); 1393 | } 1394 | 1395 | public List ExecuteList(DbCommand command, DbConnection connection) 1396 | where T : new() 1397 | { 1398 | var converter = GetDataReaderConverter(); 1399 | return ExecuteList(command, converter, connection); 1400 | } 1401 | 1402 | public List ExecuteList(DbCommand command, DbTransaction transaction) 1403 | where T : new() 1404 | { 1405 | var converter = GetDataReaderConverter(); 1406 | return ExecuteList(command, converter, transaction); 1407 | } 1408 | 1409 | public List ExecuteList(DbCommand command) 1410 | where T : new() 1411 | { 1412 | var converter = GetDataReaderConverter(); 1413 | return ExecuteList(command, converter); 1414 | } 1415 | 1416 | #endregion 1417 | 1418 | #region ExecuteListAsync 1419 | 1420 | public async Task> ExecuteListAsync(DbCommand command, Converter converter, int startRecord, int maxRecords, DbConnection connection) 1421 | { 1422 | var list = new List(); 1423 | 1424 | using (DbDataReader reader = await ExecuteReaderAsync(command, connection)) 1425 | { 1426 | await FillFromReaderAsync(reader, startRecord, maxRecords, r => 1427 | { 1428 | list.Add(converter(reader)); 1429 | }); 1430 | 1431 | reader.Close(); 1432 | } 1433 | 1434 | return list; 1435 | } 1436 | 1437 | public async Task> ExecuteListAsync(DbCommand command, Converter converter, int startRecord, int maxRecords, DbTransaction transaction) 1438 | { 1439 | var list = new List(); 1440 | 1441 | using (DbDataReader reader = await ExecuteReaderAsync(command, transaction)) 1442 | { 1443 | await FillFromReaderAsync(reader, startRecord, maxRecords, r => 1444 | { 1445 | list.Add(converter(reader)); 1446 | }); 1447 | 1448 | reader.Close(); 1449 | } 1450 | 1451 | return list; 1452 | } 1453 | 1454 | public async Task> ExecuteListAsync(DbCommand command, Converter converter, int startRecord, int maxRecords) 1455 | { 1456 | List list; 1457 | 1458 | using (DbConnection connection = CreateConnection()) 1459 | { 1460 | await connection.OpenAsync(); 1461 | 1462 | list = await ExecuteListAsync(command, converter, startRecord, maxRecords, connection); 1463 | 1464 | connection.Close(); 1465 | } 1466 | 1467 | return list; 1468 | } 1469 | 1470 | public Task> ExecuteListAsync(DbCommand command, Converter converter, DbConnection connection) 1471 | { 1472 | return ExecuteListAsync(command, converter, 0, 0, connection); 1473 | } 1474 | 1475 | public Task> ExecuteListAsync(DbCommand command, Converter converter, DbTransaction transaction) 1476 | { 1477 | return ExecuteListAsync(command, converter, 0, 0, transaction); 1478 | } 1479 | 1480 | public Task> ExecuteListAsync(DbCommand command, Converter converter) 1481 | { 1482 | return ExecuteListAsync(command, converter, 0, 0); 1483 | } 1484 | 1485 | public Task> ExecuteListAsync(DbCommand command, int startRecord, int maxRecords, DbConnection connection) 1486 | where T : new() 1487 | { 1488 | var converter = GetDataReaderConverter(); 1489 | return ExecuteListAsync(command, converter, startRecord, maxRecords, connection); 1490 | } 1491 | 1492 | public Task> ExecuteListAsync(DbCommand command, int startRecord, int maxRecords, DbTransaction transaction) 1493 | where T : new() 1494 | { 1495 | var converter = GetDataReaderConverter(); 1496 | return ExecuteListAsync(command, converter, startRecord, maxRecords, transaction); 1497 | } 1498 | 1499 | public Task> ExecuteListAsync(DbCommand command, int startRecord, int maxRecords) 1500 | where T : new() 1501 | { 1502 | var converter = GetDataReaderConverter(); 1503 | return ExecuteListAsync(command, converter, startRecord, maxRecords); 1504 | } 1505 | 1506 | public Task> ExecuteListAsync(DbCommand command, DbConnection connection) 1507 | where T : new() 1508 | { 1509 | var converter = GetDataReaderConverter(); 1510 | return ExecuteListAsync(command, converter, connection); 1511 | } 1512 | 1513 | public Task> ExecuteListAsync(DbCommand command, DbTransaction transaction) 1514 | where T : new() 1515 | { 1516 | var converter = GetDataReaderConverter(); 1517 | return ExecuteListAsync(command, converter, transaction); 1518 | } 1519 | 1520 | public Task> ExecuteListAsync(DbCommand command) 1521 | where T : new() 1522 | { 1523 | var converter = GetDataReaderConverter(); 1524 | return ExecuteListAsync(command, converter); 1525 | } 1526 | 1527 | #endregion 1528 | } 1529 | } 1530 | --------------------------------------------------------------------------------