Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

# To get bulk data from database

// web.config(for reference only)

<?xml version="1.0"?>

<configuration>

<system.web>

<compilation debug="true" targetFramework="4.0"/>

</system.web>

<appSettings>

<add key="ConnectionString"
value="server=192.168.1.90;Database=BudgetHouse;Uid=SolsynchCora;Pwd=GenSol@123"/>

</appSettings>

</configuration>

//environment.ts
export const environment = {

production: false,

apiUrl: "http://localhost:9998"

//apiUrl: "http://192.168.1.207:9096"

};

//proxy.conf.json
{

"/abc/*": {

"target": "http://122.179.133.205:8001",

"secure": false

},

"/sap": {
"target": "http://192.168.1.200:30399",

"secure": false,

"changeOrigin": true,

"logLevel": "debug"

},

"/ACPLANACT_CDS/*": {

"target": "http://192.168.1.200:30399/sap/opu/odata/SOLVIE",

"secure": false

// dropdown.service.ts

getBudgetData(idUser: string, idCustomer: any, accountCat: any, accountGrp:any,


version:any,balshIndicator:string,companyCode:string,businessUnit:string)

return this.http.get<any>(`${environment.apiUrl}/api/BudgetHouse/BudgetData/${idUser}/$
{idCustomer}/${accountCat}/${accountGrp}/${version}/${balshIndicator}/${companyCode}/$
{businessUnit}`);

//BudgetHouseControler.cs

[HttpGet("BudgetData/{id}/{customer}/{accCat}/{accGrp}/{version}/{balshIndicator}/{companyCode}/{businessUnit}")]

public List<BudgetDataBalSheet> getBudgetData(string id, string customer, string accCat, string accGrp, string version,
string balshIndicator,string companyCode,string businessUnit)

List<BudgetDataBalSheet> dataList = BudgetHouseManager.getBudgetData(id, customer, accCat, accGrp, version,


balshIndicator, companyCode, businessUnit);

return dataList;

//BudgetHouseManager.cs
using BudgetHouseApi;

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Text.Json;

using System.Linq;

using System.Threading.Tasks;

namespace BudgetHouseApi.Models

public class BudgetHouseManager

internal static List<BudgetDataBalSheet> getBudgetData(string id, string customer, string accountCat, string accountGrp,
string version, string balshIndicator,string companyCode,string businessUnit)

List<BudgetDataBalSheet> dataList = new List<BudgetDataBalSheet>();

DataAccess da = new DataAccess();

DataTable dt = new DataTable();

BudgetDataBalSheet dataObj = new BudgetDataBalSheet();

DataSet ds;

string whereBUnit = businessUnit=="1" ? "BusinessUnit =''" : "BusinessUnit = '" + businessUnit + "'";

if (version == "0")

//ds = da.GetDataSetThroughSQL(" select * from [BudgetHouse].[dbo].[BudgetPlanBalanceSheet] where


BalshInd=0 and version = ( select MAX(VERSION) AS VERSION FROM BudgetPlanBalanceSheet where BalshInd=0 )");

ds = da.GetDataSetThroughSQL("SELECT BP.[AccountCategory],AG.[AccountGroup],AC.[Description] AS
AccountCategoryDescription,AG.[Description] AS AccountGroupDescription,BP.[CompanyCode],BP.[BusinessUnit],BP.
[Year],BP.[Amount],BP.[BalshInd],BP.[Currency],BP.[Version],BP.[Timestamp],BP.[UserId] FROM [BudgetHouse].[dbo].
[BudgetPlanBalanceSheet] BP JOIN [BudgetHouse].[dbo].[AccountGroup] AG ON BP.[AccountGroup] = AG.
[AccountGroup] JOIN [BudgetHouse].[dbo].[AccountCategory] AC ON BP.[AccountCategory] = AC.[AccountCategory]
WHERE BP.BalshInd = " + balshIndicator + " AND CompanyCode ='" + companyCode+ "' AND " +whereBUnit+" AND
BP.Version = (SELECT MAX(VERSION) AS VERSION FROM BudgetPlanBalanceSheet WHERE BalshInd = "+ balshIndicator +
")");

else

//ds = da.GetDataSetThroughSQL("Select * from dbo.BudgetPlanBalanceSheet WHERE VERSION = '" + version +


"'");

ds = da.GetDataSetThroughSQL("SELECT BP.[AccountCategory],AG.[AccountGroup],AC.[Description] AS
AccountCategoryDescription,AG.[Description] AS AccountGroupDescription,BP.[CompanyCode],BP.[BusinessUnit],BP.
[Year],BP.[Amount],BP.[BalshInd],BP.[Currency],BP.[Version],BP.[Timestamp],BP.[UserId] FROM [BudgetHouse].[dbo].
[BudgetPlanBalanceSheet] BP JOIN [BudgetHouse].[dbo].[AccountGroup] AG ON BP.[AccountGroup] = AG.
[AccountGroup] JOIN [BudgetHouse].[dbo].[AccountCategory] AC ON BP.[AccountCategory] = AC.[AccountCategory]
WHERE BP.BalshInd = " + balshIndicator + " AND VERSION = '" + version + "' AND CompanyCode ='" + companyCode + "'
AND " + whereBUnit + " ");

if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)

foreach (DataRow dr in ds.Tables[0].Rows)

dataObj = new BudgetDataBalSheet();

dataObj = BudgetDataBalSheet.setObjInfo(dr);

dataList.Add(dataObj);

return dataList;

}
• Method Signature: The method getBudgetData is declared as internal static, meaning it can only be accessed
within the same assembly, and it belongs to the class where it's defined. It returns a List<BudgetDataBalSheet>
and takes several parameters including id, customer, accountCat, accountGrp, version, balshIndicator,
companyCode, and businessUnit.

• Initialization: Inside the method, a new List<BudgetDataBalSheet> named dataList is initialized to store the
budget data. An instance of DataAccess class named da is created. DataTable dt and BudgetDataBalSheet object
dataObj are also declared but not used later in the code. Finally, a DataSet ds is declared.

• SQL Query Construction: Based on the value of the version parameter, different SQL queries are constructed to
fetch budget data from the database. If version is "0", it fetches data based on the maximum version available,
otherwise, it fetches data based on the specified version.

• Query Execution: The constructed SQL query is passed to the GetDataSetThroughSQL method of the DataAccess
class to execute the query and retrieve data from the database. The result is stored in the DataSet ds.

• Data Processing: If the dataset contains tables and the first table contains rows, it iterates through each row of
the table. For each row, a new BudgetDataBalSheet object is created and populated with data using the
setObjInfo method, which seems to be a static method of the BudgetDataBalSheet class. The populated object
is then added to the dataList.

• Return: Finally, the populated dataList is returned from the method.

• Overall, this method seems to be retrieving budget data from a database based on various criteria such as
version, company code, business unit, etc., and then returning it as a list of BudgetDataBalSheet objects.

// BugdetDataBalsheet.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Data;

namespace BudgetHouseApi.Models

public class BudgetDataBalSheet

private string _AccountCategory = "";

private string _AccountCategoryDesc = "";


private string _AccountGroup = "";

private string _AccountGroupDesc = "";

private string _Year = "";

private string _Amount = "";

private string _BalshInd = "";

private string _Currency = "";

private string _Version = "";

public string AccountCategory { get { return _AccountCategory; } set { _AccountCategory = value; } }

public string AccountCategoryDesc { get { return _AccountCategoryDesc; } set { _AccountCategoryDesc = value; } }

public string AccountGroup { get { return _AccountGroup; } set { _AccountGroup = value; } }

public string AccountGroupDesc { get { return _AccountGroupDesc; } set { _AccountGroupDesc = value; } }

public string Year { get { return _Year; } set { _Year = value; } }

public string Amount { get { return _Amount; } set { _Amount = value; } }

public string BalshInd { get { return _BalshInd; } set { _BalshInd = value; } }

public string Currency { get { return _Currency; } set { _Currency = value; } }

public string Version { get { return _Version; } set { _Version = value; } }

internal static BudgetDataBalSheet setObjInfo(DataRow dr)

BudgetDataBalSheet obj = new BudgetDataBalSheet();

obj.AccountCategory= dr["AccountCategory"].ToString().Trim();

obj.AccountCategoryDesc= dr["AccountCategoryDescription"].ToString().Trim();

obj.AccountGroup = dr["AccountGroup"].ToString().Trim();

obj.AccountGroupDesc = dr["AccountGroupDescription"].ToString().Trim();

obj.Year = dr["Year"].ToString().Trim();
obj.Amount = dr["Amount"].ToString().Trim();

obj.BalshInd = dr["BalshInd"].ToString().Trim();

obj.Currency = dr["Currency"].ToString().Trim();

obj.Version = dr["Version"].ToString().Trim();

return obj;

• In the provided code, the public string AccountCategory property is used for encapsulation purposes.
Encapsulation is a fundamental principle in object-oriented programming, which involves bundling the data (in
this case, _AccountCategory) and methods (getters and setters) that operate on the data together in a single
unit (in this case, the BudgetDataBalSheet class).

• By using properties like public string AccountCategory, you're providing controlled access to the
_AccountCategory field. This means that outside code can read and modify the _AccountCategory field only
through the getter and setter methods provided by the property.

• Changing the value of the AccountCategory property will modify the _AccountCategory field, I was referring to
code within the same class or within a derived class (if _AccountCategory was protected rather than private).
External code outside the class cannot directly modify private fields like _AccountCategory unless there are
public methods or properties provided by the class to do so.

setObjInfo Method:

• This method is defined as internal which means it can only be accessed within the same
assembly.

• It takes a DataRow object (dr) as input, which typically represents a row of data from a database
table.

• Inside the method:

• It creates a new instance of BudgetDataBalSheet.

• It retrieves data from the DataRow and assigns it to the properties of the BudgetDataBalSheet
object.

• ToString().Trim() is used to convert the data to a string and remove any leading or trailing white
spaces.
• The data is fetched from the DataRow using column names specified within square brackets,
such as dr["ColumnName"].

• Each property of the BudgetDataBalSheet object is assigned data from the corresponding
column in the DataRow.

• Finally, the method returns the populated BudgetDataBalSheet object.

// DataAccess.cs

using System;

using System.Configuration;

using System.Data;

using System.Globalization;

using System.Data.SqlClient;

namespace BudgetHouseApi

public class DataAccess

private SqlCommand cmd;

private SqlConnection con;

private SqlDataAdapter da;

private SqlTransaction Transaction;

#region ToMakeConnection

private void Make_Connection()

//string connectionString = "server=192.168.1.90;Database=BudgetHouse; Uid=SolsynchCora;


Pwd=GenSol@123;"; //ConfigurationManager.AppSettings["ConnectionString"].ToString().Trim();

string connectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString().Trim();

if (connectionString == null)

throw new Exception("Error in Config File.Please Check");

this.con = new SqlConnection(connectionString);

this.cmd = new SqlCommand();

this.cmd.CommandTimeout = 0xc350;

this.cmd.Connection = this.con;

#endregion

#region OpenConnection

public void Open_Connection()

try

if (this.con == null)

this.Make_Connection();

if (this.con.State == ConnectionState.Closed)

this.con.Open();

}
}

catch (Exception exception)

throw exception;

#endregion

#region InsertData

private void Add_Parameter_In_Command(SqlParameter[] p)

if (this.cmd != null)

this.cmd.Parameters.Clear();

if (p != null)

foreach (SqlParameter parameter in p)

this.cmd.Parameters.Add(parameter);

#endregion

#region ExecuteReader with passing only stored procedure Name

public SqlDataReader ExecuteReader(string spName)

SqlDataReader reader;
try

this.Open_Connection();

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;

reader = this.cmd.ExecuteReader(CommandBehavior.CloseConnection);

catch (Exception exception)

throw exception;

return reader;

#endregion

#region ExecuteReader with passing stored procedure Name and SqlParameters

public SqlDataReader ExecuteReader(string spName, SqlParameter[] Oparam)

SqlDataReader reader;

try

this.Open_Connection();

this.Add_Parameter_In_Command(Oparam);

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;

reader = this.cmd.ExecuteReader(CommandBehavior.CloseConnection);

}
catch (Exception exception)

throw exception;

return reader;

#endregion

public SqlDataReader NExecuteReader(string spName, SqlParameter[] Oparam)

SqlDataReader reader;

try

this.Add_Parameter_In_Command(Oparam);

this.cmd.Transaction = this.Transaction;

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;

reader = this.cmd.ExecuteReader();

catch (Exception exception)

throw exception;

return reader;

// Get Data Through SQL


public int GetRowCount(string SQL)

int no = 0;

try

this.Open_Connection();

this.cmd.CommandType = CommandType.Text;

this.cmd.CommandText = SQL;

no = (int)this.cmd.ExecuteScalar();

this.Close_Connection();

catch (Exception exception)

throw exception;

return no;

public string ExecuteSQLQuery(string SQL)

string ErrorMsg = "";

try

this.Open_Connection();

this.cmd.CommandText = SQL;

this.cmd.CommandType = CommandType.Text;

this.cmd.ExecuteNonQuery();
}

catch (Exception exception)

ErrorMsg = exception.ToString();

finally

this.Close_Connection();

return ErrorMsg;

public DataSet GetDataSetThroughSQL(string SQL)

DataSet ds = new DataSet();

try

this.Open_Connection();

this.cmd.CommandType = CommandType.Text;

this.cmd.CommandText = SQL;

this.da = new SqlDataAdapter(this.cmd);

this.da.Fill(ds, SQL);

this.Close_Connection();

this.da.Dispose();

catch (Exception exception)

{
throw exception;

return ds;

public DataSet GetDataSetThroughSQL(string SQL, SqlParameter[] parameters)

DataSet ds = new DataSet();

try

this.Open_Connection();

this.cmd.CommandType = CommandType.Text;

this.cmd.CommandText = SQL;

if (parameters != null)

foreach (SqlParameter parameter in parameters)

this.cmd.Parameters.Add(parameter);

this.da = new SqlDataAdapter(this.cmd);

this.da.Fill(ds, SQL);

this.Close_Connection();

this.da.Dispose();

}
catch (Exception exception)

throw exception;

return ds;

public DataTable GetDataTableThroughSQL(string SQL)

DataTable tbl = new DataTable();

try

this.Open_Connection();

this.cmd.CommandType = CommandType.Text;

this.cmd.CommandText = SQL;

this.da = new SqlDataAdapter(this.cmd);

tbl.Locale = CultureInfo.InvariantCulture;

this.da.FillSchema(tbl, SchemaType.Source);

this.Close_Connection();

this.da.Dispose();

catch (Exception exception)

throw exception;

return tbl;

}
/* Get Data Through Stored Procedure */

public string ExecuteSP(string spname)

string ErrorMsg = "";

try

this.Open_Connection();

this.cmd.CommandText = spname;

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.ExecuteNonQuery();

catch (Exception exception)

ErrorMsg = exception.ToString();

finally

this.Close_Connection();

return ErrorMsg;

public string ExecuteSP(string spName, SqlParameter[] sqlparam)

string ErrorMsg = "";

try
{

this.Open_Connection();

this.Add_Parameter_In_Command(sqlparam);

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;

this.cmd.ExecuteNonQuery();

catch (Exception exception)

ErrorMsg = exception.ToString();

finally

this.Close_Connection();

return ErrorMsg;

public DataSet GetDataSetThroughSP(string spName)

DataSet ds = new DataSet();

try

this.Open_Connection();

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;
this.da = new SqlDataAdapter(this.cmd);

this.da.Fill(ds, spName);

catch (Exception exception)

throw new Exception(exception.ToString());

finally

this.Close_Connection();

this.da.Dispose();

return ds;

public DataSet GetDataSetThroughSP(string spName, SqlParameter[] sqlparam)

DataSet set2;

try

this.Open_Connection();

DataSet dataSet = new DataSet();

if (sqlparam != null)

this.Add_Parameter_In_Command(sqlparam);

this.cmd.CommandType = CommandType.StoredProcedure;
this.cmd.CommandText = spName;

this.da = new SqlDataAdapter(this.cmd);

this.da.Fill(dataSet);

set2 = dataSet;

catch (Exception exception)

throw new Exception(exception.ToString());

finally

this.Close_Connection();

this.da.Dispose();

return set2;

#region BulkInsert

public string BulkInsert(string spName, DataTable dt)

string ErrorMsg = "";

try

this.Open_Connection();

this.cmd.Parameters.AddWithValue("@DataTable", dt);

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;

this.cmd.ExecuteNonQuery();
}

catch (Exception ex)

ErrorMsg = ex.Message;

finally

this.Close_Connection();

return ErrorMsg;

internal DataSet GetDataSetThroughSP(string v, object sqlparam)

throw new NotImplementedException();

public int BulkInsert(DataTable DtSAP, string SPName, string tblName)

int num = -1;

DataTable dataTable = new DataTable();

DataSet dataSet = new DataSet();

try

cmd = new SqlCommand();

this.Open_Connection();

cmd.CommandText = SPName;
cmd.CommandType = CommandType.StoredProcedure;

cmd.Connection = this.con;

cmd.Parameters.AddWithValue(tblName, DtSAP);

num = 1;

cmd.ExecuteNonQuery();

catch (Exception ex)

num = -1;

throw ex;

finally

this.Close_Connection();

return num;

#endregion

/* T-SQL Steps */

// Step 1 : Open Connection

public void TSqlOpen_Connection()

Open_Connection();

}
// Step 2 : Begin Transaction

public void TSqlBegin_Transaction()

if (this.con.State == ConnectionState.Open)

this.Transaction = this.con.BeginTransaction();

// Step 3 : Execute SQL or Stored Procedure

public string TSqlExecuteSQL(string sql)

string ErrorMsg = "";

try

this.cmd.Transaction = this.Transaction;

this.cmd.CommandType = CommandType.Text;

this.cmd.CommandText = sql;

this.cmd.ExecuteNonQuery();

catch (Exception exception)

ErrorMsg = exception.ToString();

return ErrorMsg;

}
public string TSqlExecuteSP(string spName, SqlParameter[] sqlparam)

string ErrorMsg = "";

try

this.cmd.Transaction = this.Transaction;

this.Add_Parameter_In_Command(sqlparam);

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;

this.cmd.ExecuteNonQuery();

catch (Exception ex)

ErrorMsg = ex.Message;

return ErrorMsg;

public DataSet TSqlGetDataSetThroughSP(string spName, SqlParameter[] sqlparam)

DataSet ds = new DataSet();

try

this.cmd.Transaction = this.Transaction;

if (sqlparam != null)

{
this.Add_Parameter_In_Command(sqlparam);

this.cmd.CommandType = CommandType.StoredProcedure;

this.cmd.CommandText = spName;

this.da = new SqlDataAdapter(this.cmd);

this.da.Fill(ds);

catch (Exception exception)

throw new Exception(exception.ToString());

return ds;

public DataSet TSqlGetDataSetThroughSQL(string SQL)

DataSet ds = new DataSet();

try

this.cmd.Transaction = this.Transaction;

this.cmd.CommandType = CommandType.Text;

this.cmd.CommandText = SQL;

this.da = new SqlDataAdapter(this.cmd);

this.da.Fill(ds, SQL);

catch (Exception exception)

{
throw exception;

return ds;

// Step 4 : Rollback Transaction on Error

public void ROLLBACK_TRANSACTION()

if (this.con.State == ConnectionState.Open)

this.Transaction.Rollback();

// Step 5 : Commit Transaction

public void COMMIT_TRANSACTION()

if (this.con.State == ConnectionState.Open)

this.Transaction.Commit();

// Step 6 : Close Connection

public void Close_Connection()

try
{

if ((this.con != null) && (this.con.State == ConnectionState.Open))

this.con.Close();

catch (Exception exception)

throw exception;

You might also like