Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Last active December 20, 2018 11:35
Show Gist options
  • Save patrickmaciel/7aabc4adfac22f735c0c63db1197c574 to your computer and use it in GitHub Desktop.
Save patrickmaciel/7aabc4adfac22f735c0c63db1197c574 to your computer and use it in GitHub Desktop.
Executando uma function do postgres pelo C# - Entity Framework #csharp #entity #postgres
using (var context = new IABFLUIDAL.Context.IABModel())
{
using (DbContextTransaction tran = context.Database.BeginTransaction(IsolationLevel.ReadCommitted))
{
var conn = context.Database.Connection;
using (var command = conn.CreateCommand())
{
//command.CommandText = "flui.func_test_patrick";
//command.CommandType = CommandType.StoredProcedure;
//command.Parameters.Add(macaddress);
//DbParameter ProcessedFileName = command.CreateParameter();
//ProcessedFileName.DbType = DbType.String;
//ProcessedFileName.ParameterName = "@macaddress";
//ProcessedFileName.Value = macaddress;
//command.Parameters.Add(ProcessedFileName);
//command.Parameters.Add(new SqlParameter("macaddress", macaddress));
//using (var dr = command.ExecuteReader())
//{
// var rc = 0;
// int functionReturn = 0;
// while (dr.Read())
// {
// //Console.WriteLine((rc++) + " " + dr.GetString(0) + " " + dr.GetDateTime(1) + " " + dr.GetDecimal(2));
// functionReturn = dr.GetInt16(0);
// Console.WriteLine((rc++) + " " + functionReturn);
// }
//var functionReturn = (int) command.ExecuteScalar();
NpgsqlConnection connection = new NpgsqlConnection(context.Database.Connection.ConnectionString);
connection.Open(); /*OPEN DATABASE CONNECTION*/
NpgsqlCommand cmd = new NpgsqlCommand("flui.func_test_patrick", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@string", NpgsqlTypes.NpgsqlDbType.Text);
cmd.Parameters["@string"].Value = macaddress;
NpgsqlDataReader dr = cmd.ExecuteReader();
int functionReturn = 0;
while (dr.Read())
{
functionReturn = (int) dr.GetValue(0);
}
if (functionReturn == 1)
{
tran.Commit();
connection.Close();
return new ResponseData<IABDTO.APP_FLUI.GET.SimpleSyncDate>(ResponseData<IABDTO.APP_FLUI.UserMatchTemp>.SUCCESS, Resource.SaveSuccess, new IABDTO.APP_FLUI.GET.SimpleSyncDate()
{
LastSyncTime = DateTime.Now,
SyncPackageKey = syncPackageKey,
IsLastRoute = isLastRoute
}, null);
}
else
{
connection.Close();
return new ResponseData<IABDTO.APP_FLUI.GET.SimpleSyncDate>(ResponseData<IABDTO.APP_FLUI.UserMatchTemp>.FAIL, Resource.FunctionAPIFail, new IABDTO.APP_FLUI.GET.SimpleSyncDate()
{
LastSyncTime = DateTime.Now,
SyncPackageKey = syncPackageKey,
IsLastRoute = isLastRoute
}, null);
}
}
//var functionReturn = context.Database.ExecuteSqlCommand("select * from flui.func_test_patrick(@1)", macaddress);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment