From f3b0027903af46d9fa76c43408e9dd79696d9751 Mon Sep 17 00:00:00 2001 From: Andrea Cattaneo Date: Mon, 12 Sep 2022 13:49:07 -0700 Subject: [PATCH] SqlDb doc --- ACUtils.SqlDb/README.md | 51 +++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/ACUtils.SqlDb/README.md b/ACUtils.SqlDb/README.md index 3d62dea..8863b5f 100644 --- a/ACUtils.SqlDb/README.md +++ b/ACUtils.SqlDb/README.md @@ -1,5 +1,8 @@ # ACUtils.SqlDb +Helpers functions to query MSSQL databases + + ## Example usage @@ -30,6 +33,28 @@ ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master System.Data.DataTable dt = db.QueryDataTable("SELECT myvalue FROM table WHERE col = @col", "@col".WithValue("my val")); Console.WriteLine(dt.Rows[0].Field("myvalue")); +``` + + +### QueryDataRow + +```csharp +using System.Data; +using ACUtils; + +ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;"); +try { + System.Data.DataRow dr = db.QueryDataRow("SELECT myvalue FROM table WHERE id = @id", "@id".WithValue("123")); + Console.WriteLine(dr.Field("myvalue")); +} +catch (ACUtils.Exceptions.NotFoundException) +{ + // when DataTable.Rows.Count == 0 +} +catch (ACUtils.Exceptions.TooMuchResultsException) +{ + // when DataTable.Rows.Count > 1 +} ``` @@ -40,9 +65,19 @@ Console.WriteLine(dt.Rows[0].Field("myvalue")); using ACUtils; ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;"); - -string myvalue = db.QuerySingleValue("SELECT myvalue FROM table WHERE id = @id", "@id".WithValue("123")); -Console.WriteLine(myvalue); +try +{ + string myvalue = db.QuerySingleValue("SELECT myvalue FROM table WHERE id = @id", "@id".WithValue("123")); + Console.WriteLine(myvalue); +} +catch (ACUtils.Exceptions.NotFoundException) +{ + // when DataTable.Rows.Count == 0 +} +catch (ACUtils.Exceptions.TooMuchResultsException) +{ + // when DataTable.Rows.Count > 1 +} ``` @@ -53,8 +88,9 @@ using ACUtils; class MyModel : ACUtils.DBModel { - public int id { get; set; } - public string myvalue { get; set; } + public int id { get; set; } // create a property with same name of table column + [DbField("myvalue")] // property can be also named different + public string anotherName { get; set; } } ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;"); @@ -73,8 +109,9 @@ using ACUtils; class MyModel : ACUtils.DBModel { - public int id { get; set; } - public string myvalue { get; set; } + public int id { get; set; } // create a property with same name of table column + [DbField("myvalue")] // property can be also named different + public string anotherName { get; set; } } ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;");