small tool to select, insert update and delete from SQL databases
-
Constructor : public dbConncector(String connStr)
Takes an String SonnectionString to init the object to use later.
String ConnectionStrong = ""; dbConncector db = new dbConncector(ConnectionStrong);
-
Constructor : public dbConncector(SqlConnection con)
Takes an System.Data.SqlClint.SqlConnection to init the object to use later.
sqlConnection con = new sqlConnection(); dbConncector db = new dbConncector(con);
-
public DataTable getAllFrom(String tableName)
Takes an String for the table to get all rows and return a System.Data.DataTable object.
dbConncector db = new dbConncector(con); DataTable result = db.getAllFrom("tableName");
-
public DataTable exec(String queiry)
Takes an String for the queiry and return a System.Data.DataTable object.
dbConncector db = new dbConncector(con); DataTable result = db.exec("select * from tableName");
-
public DataTable getAllFrom(String PeocName, List filds)
Takes an String for the table name, and a list of Field object contains the pharametrs with values to get only specific rows and return a System.Data.DataTable object.
// Select from student table the student they live in libya. dbConncector db = new dbConncector(con); Field v1 = new Field("student_location" , "Libya"); List<Field> values = new List<Field>(); values.add(v1); DataTable result = db.getAllFrom("student_table" , values);
-
public int getResultFromSp(String PeocName, List filds) (Used in stored procedures that insert, update or delete only )
Takes an String for the stored procedures name, and a list of Field object contains the pharametrs with values, it's retirn an int as a number of changes in database
// insert new student dbConncector db = new dbConncector(con); Field v1 = new Field("student_name" , "Islam"); Field v2 = new Field("student_location" , "Libya"); List<Field> values = new List<Field>(); values.add(v1); values.add(v2); int result = db.getAllFrom("student_table" , values);
Before you pass the Field object see this example.
dbConncector fb = new dbConncector();
String taegetTable = "sp_login";
List<Field> fields = new List<Field>();
fields.Add(new Field("email", "[email protected]")); // pharameter in sp_login stored procedures
fields.Add(new Field("password", "123456")); // pharameter in sp_login stored procedures
DataTable tb = fb.getAllFromSp(taegetTable, fields); //
tableName : student_info
student_id | student_name | student_location |
1 | islam | Libay |
2 | mdammed | Libay |
3 | ahmed | Egypt |
//create String variable for connectionString
String _cs = "";
//Create an object of dbConncector
dbConncector db = new dbConncector(_cs);
//get all student
DataTable result = db.getAllFrom("student_info");
// the object result has the result
//get only students from libya
Field v1 = new Field("student_location" , "Libya");
List<Field> values = new List<Field>();
values.add(v1);
DataTable result = db.getAllFrom("student_info" , values);
// the object result has the result
//insert useings tored procedures sp_insert_into_student_info
Field v1 = new Field("student_name" , "mostafa");
Field v2 = new Field("student_location" , "Libya");
List<Field> values = new List<Field>();
values.add(v1);
values.add(v2);
int result = db.getResultFromSp("sp_insert_into_student_info" , values);
// result must = 1