DataManager use Examples

BASIC USE
DataManager DM = new DataManager("dbname", "catalog", "username", "password");
DataTable res = DM.getAllRecords("table to read");
  1. getRecord (return a record from a table)
  2. insRecord (insert a record from a table)
  3. updRecord (update a table's record)
  4. delRecord (delete a table's record)
  5. getAllRecords (return all records of a table)
class DataManager has 6 methods that map this operation and don't manage operation's success or fail. Basically will throw a new Exception on error occure. Unmanaged Exception Method are this:
DataTable Response = DM.getRecord("parameter that you can see with intellisense");
int Response = DM.insRecord("parameter that you can see with intellisense");
int Response = DM.updRecord(" 2 overload for this method");
int Response = DM.delRecord("parameter that you can see with intellisense");
DataTable Response = DM.getAllRecords("parameter that you can see with intellisense");
int return value in insRecord Method contain id field value of new record inserted
int return = -1 on other method if an Exception will not trowned mean that an error occur
ADVANCED USE
Repository Pattern allow this operation:
  1. getRecordM (return a record from a table)
  2. insRecordM (insert a record from a table)
  3. updRecordM (update a table's record)
  4. delRecordM (delete a table's record)
  5. getAllRecordsM (return all records of a table)
class DataManager has 6 methods that map this operation and manage operation's success or fail with instance of class that implement IResponse interface. IResponse interface and Response class are included as dependency of MagicData 2.1.0 so you can use DataManager in this way:
IResponse Response = DM.getRecordM("parameter that you can see with intellisense");
IResponse Response = DM.insRecordM("parameter that you can see with intellisense");
IResponse Response = DM.updRecordM(" 2 overload for this method");
IResponse Response = DM.delRecordM("parameter that you can see with intellisense");
IResponse Response = DM.getAllRecordsM("parameter that you can see with intellisense");
Managed Exception Method (indicated by "M" at the end of methods)
Manage response of a database interaction with Response class allow to intercept possible errors allowing you to manage this situation. so you can simply do:
//....
IResponse Response = DM.insRecordM("parameter list");
if(Response.fail()){
// manage error
string error = Response.error();
}
//.....
if you are reading from a table readed records is stored in Response property and can read with data() method;
//....
var data = null;
IResponse Response = DM.getAllRecordsM("parameter list");
//manage possible error
if(Response.fail()){
//code to custom manage errors
throw new Exception("your exception message");
}
//assing readed data;
data = Response.data();
//....
But..
in this specific situation Response data property contain only a DataTable instance so you can typing
data variable so:
DataTable data = null;
IResponse Response = DM.getAllRecordsM("parameter list");
//manage possible error
if(Response.fail()){
//code to custom manage errors
throw new Exception("your exception message");
}
//assing readed data;
data = (DataTable)Response.data();
//....
More documentation on Response class cooming soon;
insRecord and updRecord two words only;
insRecord and updRecord have a dynamic data parameter. data is an istance of class that represent table record. Example:
you have to write a new record in user table, user table has iduser(setted as primary key), name, surname field. So you have to define a class that map record table
public class user{
int iduser {get;set;}
string name {get;set;}
string surname{get;set;}
}
now create a new instance and assign value: user newuser = new user();
//newuser.iduser = -1//it is not necessary because it is setted as primary key
newuser.name = "Luigi";
newuser.surname ="Senzamici";
now: IResponse Response = DM.insRecordM("user", newuser);
It is usefull in Post allowed Controller like this
[HttpPost]
public ActionResult User(user newuser){
DataManager DM = new DataManger("parameter list");
Response = DM.insRecordM("user", newuser);
//... your code
}
//... your code
}
Questo sito o gli strumenti terzi da questo utilizzati si avvalgono di cookie necessari al funzionamento ed utili alle finalità illustrate nella cookie policy. Se vuoi saperne di più o negare il consenso a tutti o ad alcuni cookie, consulta settaggio cookie. Chiudendo questo banner, scorrendo questa pagina, cliccando su un link o proseguendo la navigazione in altra maniera, acconsenti all’uso dei cookie. Cookies Policy