public static DataTable getTableName() { DatabaseClass.createconnection(); tablelist = DatabaseClass.getConnection().GetSchema("Tables"); DatabaseClass.closeConnection(); return(tablelist); }
//It executes non queries like: INSERT AND UPDATE statements public static void ExecuteNonQuery(String query) { DatabaseClass.createconnection(); SqlDataAdapter adp = new SqlDataAdapter(query, DatabaseClass.connection); SqlCommand dataCommand = DatabaseClass.connection.CreateCommand(); dataCommand.CommandText = query; dataCommand.ExecuteNonQuery(); DatabaseClass.closeConnection(); }
//It returns all the data inside the given table public static DataSet getTableDatafromTable(String tableName) { DatabaseClass.createconnection(); String sqlquerydata = "SELECT * FROM [" + tableName + " ];"; datatableadapter = new SqlDataAdapter(sqlquerydata, DatabaseClass.connection); DatabaseClass.closeConnection(); dsData = new DataSet(); datatableadapter.Fill(dsData, "datatableall"); return(dsData); }
public static DataTable GetSchemaNameOfTable(String tableName) { DatabaseClass.createconnection(); sqlquery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + tableName + "';"; adp = new SqlDataAdapter(sqlquery, DatabaseClass.getConnection()); ds = new DataSet(); adp.Fill(ds, "datatableall"); datatable = ds.Tables["datatableall"]; DatabaseClass.closeConnection(); return(datatable); }
//Inserts a row and returns the output value (i.e. primary key of the newly inserted row) public static int ExecuteNonQueryAndGetInt(String query) { DatabaseClass.createconnection(); SqlDataAdapter adp = new SqlDataAdapter(query, DatabaseClass.connection); SqlCommand dataCommand = DatabaseClass.connection.CreateCommand(); dataCommand.CommandText = query; int id = (int)(dataCommand.ExecuteScalar()); DatabaseClass.closeConnection(); return(id); }
//It executes SELECT statements and returns the result in the form of DataTable public static DataTable ExecuteQuery(String s) { //Execute a query and return result in the form of DataTable DatabaseClass.createconnection(); SqlDataAdapter adp = new SqlDataAdapter(s, DatabaseClass.connection); DataSet ds = new DataSet(); adp.Fill(ds, "resultTable"); resultTable = ds.Tables["resultTable"]; DatabaseClass.closeConnection(); return(resultTable); }