com.janoserdelyi.DataSource 1.7.0

dotnet add package com.janoserdelyi.DataSource --version 1.7.0
                    
NuGet\Install-Package com.janoserdelyi.DataSource -Version 1.7.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="com.janoserdelyi.DataSource" Version="1.7.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="com.janoserdelyi.DataSource" Version="1.7.0" />
                    
Directory.Packages.props
<PackageReference Include="com.janoserdelyi.DataSource" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add com.janoserdelyi.DataSource --version 1.7.0
                    
#r "nuget: com.janoserdelyi.DataSource, 1.7.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package com.janoserdelyi.DataSource@1.7.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=com.janoserdelyi.DataSource&version=1.7.0
                    
Install as a Cake Addin
#tool nuget:?package=com.janoserdelyi.DataSource&version=1.7.0
                    
Install as a Cake Tool

DataSource

A seriously old (2004'ish?) shim layer for data access that i have been updating and use all the time!

This can be used to connect to Postgresql, MSSQL, and MySQL. There was some stubbing in for SQLite, but it's incomplete. As of this writing it's really only been used for Postgresql for about 6 years so MSSQL and MySQL are bound to have fallen behind some.

This code can likely use an overhaul, and yes it's ugly. But it does have one thing going for it - it's been used in projects for nearly 20 years in some form or fashion so it has been well-tested!

Creating Connections

Named connections can be created two ways :

  1. XML file
  2. Programmatically

XML file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<connections>
		
		<connection>
			<name>MyConnection</name>
			<dbtype>postgresql</dbtype>
			<server>some.database.hostname.or.ip.address</server>
			<database>datbase-name-here</database>
			<username>foo</username>
			<password>abc123</password>
			
			<port>5432</port>
		</connection>
		
		
		
	</connections>
</configuration>

Programmatically

ConnectionPropertyBag connectionPropertyBag = new ConnectionPropertyBag () {
	DatabaseType = DatabaseType.Postgresql,
	Name = "MyConnection",
	Server = "some.database.hostname.or.ip.address",
	Database = "datbase-name-here",
	Username = "foo",
	Password = "abc123",
	Port = "5432"
};

ConnectionManager.Instance.AddConnection (connectionPropertyBag);

There are more connection options, like timeouts and application name.

Connections are stored in the ConnectionManager Instance singleton keyed by Name

Usage

Battle-tested syntax with easy access to everything

inserting a record

using (Connection conn = ConnectionManager.Instance.GetConnection ("MyConnection")) {
	using (Command cmd = conn.GetCommand ("insert into foo (some_field) values (:some_field) on conflict (some_field) do nothing;")) {
		cmd.CommandType = CommandType.Text;
		cmd.CH.Append ("some_field", someVariable);
		return cmd.ExecuteScalar ();
	}
}

selecting records

using (Connection conn = ConnectionManager.Instance.GetConnection ("MyConnection")) {
	using (Command cmd = conn.GetCommand ("select * from foo where bar = :bar;")) {
		cmd.CommandType = CommandType.Text;
		cmd.CH.Append ("bar", barVariable);
		using (IDataReader dr = cmd.ExecuteReader ()) {
			if (dr.Read ()) {
				Console.WriteLine ("id : " + cmd.DRH.GetInt ("id").ToString ());
				Console.WriteLine ("create_dt : " + cmd.DRH.GetDateTime ("create_dt").ToString ());
			}
		}
	}
}

The .Append(paramName, paramValue) method has many overloads for just about any datatype. There are a few exceptions where types cannot be determined, such as inserting JSON or JSONB since they are strings. For those cases there is .AppendJson(...) and .AppendJsonb(...)

New terse syntax

inserting a record

new Connect ("MyConnection").Query ("insert into foo (some_field) values (:some_field) on conflict (some_field) do nothing;").Append ("some_field", someVariable).Go();

selecting records to select in a generic fashion, .Go() takes a Func<com.janoserdelyi.DataSource.Command cmd, T>

IList<string> results = new Connect ("MyConnection").Query ("select bar from foo;").Go<IList<string>> (getStrings);

private static IList<string> getStrings (
	com.janoserdelyi.DataSource.Command cmd
) {
	using (IDataReader dr = cmd.ExecuteReader ()) {
		IList<string> rets = new List<string> ();
		while (dr.Read ()) {
			rets.Add (cmd.DRH.GetString ("bar"));
		}
		return rets;
	}
}		

If getStrings is not something you would use in multiple functions, you can easily inline the Func.

IList<string> results = new Connect ("MyConnection").Query ("select bar from foo;").Go<IList<string>> ((cmd) => {
	using (IDataReader dr = cmd.ExecuteReader ()) {
		IList<string> rets = new List<string> ();
		while (dr.Read ()) {
			rets.Add (cmd.DRH.GetString ("bar"));
		}
		return rets;
	}
});		
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on com.janoserdelyi.DataSource:

Package Downloads
DbObject

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.7.0 104 3/22/2026
1.6.4 240 8/22/2025
1.6.3 189 6/22/2025
1.6.2 193 6/22/2025
1.6.1 462 3/24/2025
1.6.0 151 2/27/2025
1.5.1 184 4/29/2024
1.5.0 166 4/29/2024
1.4.2 156 4/29/2024
1.3.0 302 11/30/2023