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 a6df63c commit 78bfded
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
87 changes: 87 additions & 0 deletions ACUtils.SqlDb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# ACUtils.SqlDb

## Example usage


### QueryDataSet

```csharp
using System.Data;
using ACUtils;

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

System.Data.DataSet ds = db.QueryDataSet(
"SELECT myvalue FROM table WHERE col = @col AND id > @id",
"@col".WithValue("my val"),
"@id".WithValue(123)
);
Console.WriteLine(ds.Tables[0].Rows[0].Field<string>("myvalue"));
```


### QueryDataTable

```csharp
using System.Data;
using ACUtils;

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

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"));

```


### QuerySingleValue

```csharp
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);
```


### QueryMany<T>

```csharp
using ACUtils;

class MyModel : ACUtils.DBModel<MyModel>
{
public int id { get; set; }
public string myvalue { get; set; }
}

ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;");
System.Collections.Generic.List<MyModel> models = db.QueryMany<MyModel>("select id, myvalue FROM table WHERE id > @id", "@id".WithValue(1));
foreach(MyModel model in models)
{
// ...
}
```


### QueryManyAsync<T>

```csharp
using ACUtils;

class MyModel : ACUtils.DBModel<MyModel>
{
public int id { get; set; }
public string myvalue { get; set; }
}

ACUtils.SqlDb db = new ACUtils.SqlDb("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;");
System.Collections.Generic.IAsyncEnumerable<MyModel> models = db.QueryManyAsync<MyModel>("select id, myvalue FROM table WHERE id > @id", "@id".WithValue(1));
await foreach (MyModel model in models)
{
// ...
}
```

8 changes: 4 additions & 4 deletions ACUtils.SqlDb/SqlDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public SqlDb(string connectionString)

#region QueryDataTable
/// <summary>
/// Eseque la query e restituisce il DataTable del risultato
/// Esegue la query e restituisce il DataTable del risultato
/// </summary>
/// <example>
/// db.QueryDataTable("SELECT * FROM A WHERE B = @B", "@B".WithValue("1"));
Expand Down Expand Up @@ -537,10 +537,10 @@ public static bool Execute(SqlConnection connection, string queryString)

#endregion

public static T GetColVal<T>(DataRow dataRow, string columName)
public static T GetColVal<T>(DataRow dataRow, string columnName)
{
// dataRow.Field<T>(columName)
object val = dataRow[columName];
// dataRow.Field<T>(columnName)
object val = dataRow[columnName];
switch (val.GetType().Name)
{
case "DBNull":
Expand Down

0 comments on commit 78bfded

Please sign in to comment.