Skip to content

Commit 68c9161

Browse files
committed
added first save method
1 parent 9211294 commit 68c9161

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/GenericSQLEntityHandler/SQLHandler.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.SqlClient;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -8,5 +9,29 @@ namespace GenericSQLEntityHandler
89
{
910
public static class SQLHandler
1011
{
12+
#region Save Methods
13+
/// <summary>
14+
/// Save a single entity to the database
15+
/// </summary>
16+
/// <param name="connString">connection string</param>
17+
/// <param name="entity">entity that needs to be committed to the database</param>
18+
/// <param name="keys"></param>
19+
/// <param name="identity">coloumn in database the represents the id</param>
20+
/// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param>
21+
/// <typeparam name="T">entity type</typeparam>
22+
/// <returns>true or false for success status</returns>
23+
public static bool Save<T>(string connString, T entity, string[] keys = null, string identity = "Id", string tableName = null) where T : class
24+
{
25+
if (keys == null || keys.Length == 0)
26+
keys = new[] { identity };
27+
28+
using (SqlConnection connection = new SqlConnection(connString))
29+
{
30+
connection.Open();
31+
GenericSQLEntityHandler entityDataHandler = new GenericSQLEntityHandler(connection);
32+
return entityDataHandler.SaveEntity(entity, tableName ?? typeof(T).Name, keys, SaveType.InsertOrUpdate, identity);
33+
}
34+
}
35+
#endregion Save Methods
1136
}
1237
}

0 commit comments

Comments
 (0)