Skip to content

Commit

Permalink
SqlDb doc
Browse files Browse the repository at this point in the history
  • Loading branch information
il-katta committed Sep 12, 2022
1 parent 78bfded commit f3b0027
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions ACUtils.SqlDb/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ACUtils.SqlDb

Helpers functions to query MSSQL databases


## Example usage


Expand Down Expand Up @@ -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<string>("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<string>("myvalue"));
}
catch (ACUtils.Exceptions.NotFoundException)
{
// when DataTable.Rows.Count == 0
}
catch (ACUtils.Exceptions.TooMuchResultsException)
{
// when DataTable.Rows.Count > 1
}

```

Expand All @@ -40,9 +65,19 @@ Console.WriteLine(dt.Rows[0].Field<string>("myvalue"));
using ACUtils;

ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;");

string myvalue = db.QuerySingleValue<string>("SELECT myvalue FROM table WHERE id = @id", "@id".WithValue("123"));
Console.WriteLine(myvalue);
try
{
string myvalue = db.QuerySingleValue<string>("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
}
```


Expand All @@ -53,8 +88,9 @@ using ACUtils;

class MyModel : ACUtils.DBModel<MyModel>
{
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;");
Expand All @@ -73,8 +109,9 @@ using ACUtils;

class MyModel : ACUtils.DBModel<MyModel>
{
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;");
Expand Down

0 comments on commit f3b0027

Please sign in to comment.