Global Koneksi

Posted: Monday,October 26, 2009 in C#, Technology
Tags: , , , ,

public static class GlobalFunction
{
public static string servername = “.”;
public static string databasename = “Database_General”;
public static string username = “”;
public static string password = “”;

public static int setKoneksi()
{
string sourceFile = “data.ini”;
if (!File.Exists(sourceFile))
{
return 0;
}
FileStream file = new FileStream(sourceFile, FileMode.OpenOrCreate, FileAccess.Read);
TextReader tr = new StreamReader(file);
int NumberOfLines = 1000;
string[] ListLines = new string[NumberOfLines];

//Read the number of lines and put them in the array
for (int i = 1; i < NumberOfLines; i++)
{
ListLines[i] = tr.ReadLine();
if (ListLines[i] == null)
{
break;
}
}
servername = GlobalFunction.Mid(ListLines[1], ListLines[1].IndexOf(‘=’) + 1).Trim();
databasename = GlobalFunction.Mid(ListLines[2], ListLines[2].IndexOf(‘=’) + 1).Trim();
username = GlobalFunction.Mid(ListLines[3], ListLines[3].IndexOf(‘=’) + 1).Trim();
password = GlobalFunction.Mid(ListLines[4], ListLines[4].IndexOf(‘=’) + 1).Trim();

// Close StreamReader
tr.Close();
//sr.Close();

// Close file
file.Close();

return 1;
}

/*————————————————————————————————*/

public NilaiAccess()
{
GlobalFunction.Koneksi();
}
public PerubahanNilai GetDataNilai(string thnajaran, string kdsem, string nim, string kelas, string kdmtk, string kdjenisnilai)
{
PerubahanNilai nilai = new PerubahanNilai();

SqlCommand cmd = new SqlCommand();
cmd.Connection = GlobalFunction.con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = “spr_prak_get_nilaiawal”;
cmd.Parameters.Add(“@thnajaran”, SqlDbType.VarChar).Value = thnajaran;
cmd.Parameters.Add(“@kdsem”, SqlDbType.VarChar).Value = kdsem;
cmd.Parameters.Add(“@nim”, SqlDbType.VarChar).Value = nim;
cmd.Parameters.Add(“@kelas”, SqlDbType.VarChar).Value = kelas;
cmd.Parameters.Add(“@kdmtk”, SqlDbType.VarChar).Value = GlobalFunction.StringIndexOfChar(kdmtk, ‘-‘).Trim();
cmd.Parameters.Add(“@kdjenisnilai”, SqlDbType.VarChar).Value = Convert.ToInt32(kdjenisnilai);

using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
nilai.Nilaiawal = sdr[“nilai”].ToString();
}
}

return nilai;
}

public bool SimpanPerubahanNilai(PerubahanNilai ubahnilai)
{

SqlCommand cmd = new SqlCommand();
cmd.Connection = GlobalFunction.con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = “spr_save_tr_perubahan_nilai”;
cmd.Parameters.Add(“@username”, SqlDbType.VarChar).Value = ActiveUser.User;
cmd.Parameters.Add(“@kdlab”, SqlDbType.VarChar).Value = ActiveUser.Kdlab;
cmd.Parameters.Add(“@thnajaran”, SqlDbType.VarChar).Value = ubahnilai.Thnajaran;
cmd.Parameters.Add(“@kdsem”, SqlDbType.VarChar).Value = ubahnilai.Kdsem;
cmd.Parameters.Add(“@nim”, SqlDbType.VarChar).Value = ubahnilai.Nim;
cmd.Parameters.Add(“@kelas”, SqlDbType.VarChar).Value = ubahnilai.Kelas;
cmd.Parameters.Add(“@kdmtk”, SqlDbType.VarChar).Value = GlobalFunction.StringIndexOfChar(ubahnilai.Kdmtk, ‘-‘).Trim();
cmd.Parameters.Add(“@kdjenisnilai”, SqlDbType.Int).Value = Convert.ToInt32(ubahnilai.Kdjenisnilai);
cmd.Parameters.Add(“@nilaiawal”, SqlDbType.VarChar).Value = Convert.ToInt32(ubahnilai.Nilaiawal);
cmd.Parameters.Add(“@nilaiubah”, SqlDbType.VarChar).Value = Convert.ToInt32(ubahnilai.Nilaiubah);

cmd.Parameters.Add(new SqlParameter(“@return”, SqlDbType.Int, 32));
cmd.Parameters[“@return”].Direction = ParameterDirection.Output;

cmd.ExecuteNonQuery();

int affectedRow = Convert.ToInt32(cmd.Parameters[“@return”].Value);

if (affectedRow == 0)
{
return false;
}
else return true;

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s