public class DataAccess
{
private string _DBProviderName;
public string DBProviderName
{
get { return _DBProviderName; }
set { _DBProviderName = value; }
}
private string _ConnectionString;
public string ConnectionString
{
get { return _ConnectionString; }
set { _ConnectionString = value; }
}
private DbProviderFactory _dpf;
private DbProviderFactory GetDBProviderFactory()
{
return _dpf;
}
public string GetParameterChar( )
{
string ret=string.Empty;
switch (_DBProviderName)
{
case "System.Data.OracleClient":
ret = ":";
break;
case "System.Data.SqlClient":
ret = "@";
break;
}
return ret;
}
public string GetDbType()
{
string ret = string.Empty;
switch (_DBProviderName)
{
case "System.Data.OracleClient":
ret = "Oracle";
break;
case "System.Data.SqlClient":
ret = "SQL";
break;
}
return ret;
}
public string GetDBIdColumn(string columnName)
{
string ret = string.Empty;
switch (_DBProviderName)
{
case "System.Data.OracleClient":
ret = columnName + ",";
break;
case "System.Data.SqlClient":
ret = "";
break;
}
return ret;
}
public string GetDBSequence(string columnName,out decimal idValue,DbTransaction trans)
{
string ret = string.Empty;
idValue = 0;
switch (_DBProviderName)
{
case "System.Data.OracleClient":
DataAccess db = new DataAccess();
DbCommand cmd = db.GetDBCommand();
DataTable dt = null;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT SEQ_" + columnName + ".NEXTVAL FROM DUAL";
try
{
dt = db.GetDataTableTrans(cmd, trans);
idValue = decimal.Parse(dt.Rows[0][0].ToString());
ret = idValue.ToString() + ",";
}
catch
{
ret = "";
idValue = 0;
}
break;
case "System.Data.SqlClient":
ret = "";
idValue = 0;
break;
}
return ret;
}
public string GetDBIdentity(string columnName, out decimal idValue, DbTransaction trans)
{
string ret = string.Empty;
idValue = 0;
switch (_DBProviderName)
{
case "System.Data.OracleClient":
ret = "";
idValue = 0;
break;
case "System.Data.SqlClient":
DataAccess db = new DataAccess();
DbCommand cmd = db.GetDBCommand();
DataTable dt = null;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT @@IDENTITY";
try
{
dt = db.GetDataTableTrans(cmd, trans);
idValue = decimal.Parse(dt.Rows[0][0].ToString());
ret = idValue.ToString();
}
catch
{
ret = "";
idValue = 0;
}
break;
}
return ret;
}
public DataAccess(string ProviderName, string ConnectionString)
{
_DBProviderName = ProviderName;
_ConnectionString = ConnectionString;
if (DbProviderFactories.GetFactoryClasses().Select("InvariantName='" + _DBProviderName + "'").Length == 0)
{
throw new Exception("Invalid .NET Data Provider specification: " + _DBProviderName);
return;
}
_dpf = DbProviderFactories.GetFactory(_DBProviderName);
}
public DataAccess()
{
_DBProviderName = global::Global.Utility.Properties.Settings.Default.ProviderName;
_ConnectionString = global::Global.Utility.Properties.Settings.Default.ConnectionString;
if (DbProviderFactories.GetFactoryClasses().Select("InvariantName='" + _DBProviderName + "'").Length == 0)
{
throw new Exception("Invalid .NET Data Provider specification: " + _DBProviderName);
return;
}
_dpf = DbProviderFactories.GetFactory(_DBProviderName);
}
private DataTable GetDataTable(string sqlSELECT)
{
DataTable dt = new DataTable();
DbDataAdapter da = GetDBDataAdapter();
DbCommand cmd = GetDBCommand();
switch (GetDbType())
{
case "Oracle":
sqlSELECT = sqlSELECT.Replace('@', ':');
break;
}
cmd.CommandText = sqlSELECT;
cmd.CommandType = CommandType.Text;
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
da.SelectCommand = cmd;
try
{
da.Fill(dt);
}
catch (Exception ex)
{
throw new Exception(ex.Message + "-->" + sqlSELECT);
}
finally
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
cmd.Dispose();
da.Dispose();
}
return dt;
}
public DataTable GetDataTableTrans(DbCommand cmd,DbTransaction trans)
{
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
DataTable dt = new DataTable();
DbDataAdapter da = GetDBDataAdapter();
DbConnection cn = null;
switch (GetDbType())
{
case "Oracle":
cmd.CommandText = cmd.CommandText.Replace('@', ':');
break;
}
if (trans == null)
{
cn = GetDBConnection();
cn.Open();
}
else
{
cn = trans.Connection;
cmd.Transaction = trans;
}
cmd.CommandTimeout = 0;
cmd.Connection = cn;
da.SelectCommand = cmd;
try
{
da.Fill(dt);
}
catch (Exception ex)
{
throw new Exception(ex.Message + "-->" + cmd.CommandText);
}
finally
{
if (trans == null)
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}
cmd.Dispose();
da.Dispose();
}
return dt;
}
public DataSet GetDataSetTrans(DbCommand cmd, DbTransaction trans)
{
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
DbDataAdapter da = GetDBDataAdapter();
DbConnection cn = null;
DataSet ds = new DataSet();
switch (GetDbType())
{
case "Oracle":
cmd.CommandText = cmd.CommandText.Replace('@', ':');
break;
}
if (trans == null)
{
cn = GetDBConnection();
cn.Open();
}
else
{
cn = trans.Connection;
cmd.Transaction = trans;
}
cmd.Connection = cn;
cmd.CommandTimeout = 0;
da.SelectCommand = cmd;
try
{
da.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message + "-->" + cmd.CommandText);
}
finally
{
if (trans == null)
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}
cmd.Dispose();
da.Dispose();
}
return ds;
}
public long SQLExecuteTrans(DbCommand cmd, DbTransaction trans)
{
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
DbConnection cn = null;
long result = 0;
switch (GetDbType())
{
case "Oracle":
cmd.CommandText = cmd.CommandText.Replace('@', ':');
break;
}
if (trans == null)
{
cn = GetDBConnection();
//cmd.Connection.ConnectionString = "Data Source=10.86.77.161;Initial Catalog=KARTAL2010PROD;Persist Security Info=True;User ID=sa;Password=sqlKartal123123";
cn.Open();
}
else
{
cn = trans.Connection;
cmd.Transaction = trans;
}
cmd.Connection = cn;
try
{
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message + "-->" + cmd.CommandText);
}
finally
{
if (trans == null)
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}
cmd.Dispose();
}
return result;
}
public void SQLExecute(string sql)
{
DbCommand cmd = GetDBCommand();
switch (GetDbType())
{
case "Oracle":
sql = sql.Replace('@', ':');
break;
}
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message + "-->" + sql);
}
finally
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
cmd.Dispose();
}
}
public long SQLExecute(string sql, DbTransaction trans)
{
DbConnection cn = null;
long result = 0;
DbCommand cmd = GetDBCommand();
switch (GetDbType())
{
case "Oracle":
sql = sql.Replace('@', ':');
break;
}
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
if (trans == null)
{
cn = GetDBConnection();
cn.Open();
}
else
{
cn = trans.Connection;
cmd.Transaction = trans;
}
cmd.Connection = cn;
try
{
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message + "-->" + cmd.CommandText);
}
finally
{
if (trans == null)
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}
cmd.Dispose();
}
return result;
}
public DataRow GetDataRow(DbCommand cmd, DbTransaction trans)
{
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
DataTable dt = GetDataTableTrans(cmd,trans);
if (dt.Rows.Count > 0)
{
return dt.Rows[0];
}
else
{
return null;
}
}
public string GetValue(DbCommand cmd, DbTransaction trans)
{
//SONRADAN EKLENDİ HATALI İŞLEMLERE HANGİ KULLANICIN SEBEP OLDUĞUNU TESPİT ETMEK İÇİN
//SORUNLAR GİDERİLDİKTEN SONRA SİLİNEBİLİR! - 28.09.2011 KADİR
decimal idKullanici = 0;
try
{
idKullanici = Convert.ToDecimal(HttpContext.Current.Session["UserId"].ToString());
}
catch
{
idKullanici = 0;
}
cmd.CommandText += " /*USERID = " + idKullanici.ToString() + "*/ ";
//END
DbConnection cn = null;
string result;
if (trans == null)
{
cn = GetDBConnection();
cn.Open();
}
else
{
cn = trans.Connection;
cmd.Transaction = trans;
}
cmd.Connection = cn;
try
{
result = cmd.ExecuteScalar().ToString() + "";
}
catch (Exception ex)
{
throw new Exception(ex.Message + "-->" + cmd.CommandText);
}
finally
{
if (trans == null)
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}
cmd.Dispose();
}
return result;
}
public DbConnection GetDBConnection()
{
DbConnection dbConn = GetDBProviderFactory().CreateConnection();
dbConn.ConnectionString = _ConnectionString;
return dbConn;
}
public DbCommand GetDBCommand()
{
DbCommand dbCmd = GetDBProviderFactory().CreateCommand();
dbCmd.Connection = GetDBConnection();
return dbCmd;
}
public DbDataAdapter GetDBDataAdapter()
{
DbDataAdapter dbAdap = GetDBProviderFactory().CreateDataAdapter();
return dbAdap;
}
public DbParameter GetDBParameter()
{
DbParameter dbAdap = GetDBProviderFactory().CreateParameter();
return dbAdap;
}
public DbTransaction GetTransaction()
{
DbConnection dbConn = GetDBProviderFactory().CreateConnection();
dbConn.ConnectionString = _ConnectionString;
dbConn.Open();
DbTransaction dbTrans = dbConn.BeginTransaction();
return dbTrans;
}
}
C# Code Samples
28 Ocak 2012 Cumartesi
Encrypt - Decrypt String Values
public class Encrytion
{
public static string passwordText = "xx&b[m-u797B*AAe/L+YPhKa#(K+YijMFtAIa{s7(%]w9aS\\]22_z=34.L+">g^g_@fN5Ai*M:LY#q8>K+YijMFtAIa{s7(%]w9aS\\]22_z=34.L+'6Tg(vz{wBXBnt\\ADtxBED";
public Encrytion()
{
}
public static string Encrypt(string clearText)
{
return Encrypt(clearText, passwordText);
}
public static string Decrypt(string chipperText)
{
return Decrypt(chipperText, passwordText);
}
public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
// PasswordDeriveBytes is for getting Key and IV.
// Using PasswordDeriveBytes object we are first getting 32 bytes for the Key (the default
//Rijndael key length is 256bit = 32bytes) and then 16 bytes for the IV.
// IV should always be the block size, which is by default 16 bytes (128 bit) for Rijndael.
byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
// Encrypt a byte array into a byte array using a key and an IV
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
// Algorithm. Rijndael is available on all platforms.
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
//CryptoStream is for pumping our data.
cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return encryptedData;
}
public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
byte[] decryptedData = ms.ToArray();
return decryptedData;
}
// Decrypt a string into a string using a password
// Uses Decrypt(byte[], byte[], byte[])
public static string Decrypt(string cipherText, string Password)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedData);
}
}
public class CryptorEngine
{
///
/// Encrypt a string using dual encryption method. Return a encrypted cipher Text
///
/// string to be encrypted
/// use hashing? send to for extra secirity
///
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// Get the key from config file
//string key = (string)settingsReader.GetValue("xx&b[m-u797B", typeof(String));
string key = "xx&b[m-u797B";
//System.Windows.Forms.MessageBox.Show(key);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
///
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
///
/// encrypted string
/// Did you use hashing to encrypt this data? pass true is yes
///
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
//string key = (string)settingsReader.GetValue("xx&b[m-u797B", typeof(String));
string key = "xx&b[m-u797B";
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
{
public static string passwordText = "xx&b[m-u797B*AAe/L+YPhKa#(K+YijMFtAIa{s7(%]w9aS\\]22_z=34.L+">g^g_@fN5Ai*M:LY#q8>K+YijMFtAIa{s7(%]w9aS\\]22_z=34.L+'6Tg(vz{wBXBnt\\ADtxBED";
public Encrytion()
{
}
public static string Encrypt(string clearText)
{
return Encrypt(clearText, passwordText);
}
public static string Decrypt(string chipperText)
{
return Decrypt(chipperText, passwordText);
}
public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
// PasswordDeriveBytes is for getting Key and IV.
// Using PasswordDeriveBytes object we are first getting 32 bytes for the Key (the default
//Rijndael key length is 256bit = 32bytes) and then 16 bytes for the IV.
// IV should always be the block size, which is by default 16 bytes (128 bit) for Rijndael.
byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
// Encrypt a byte array into a byte array using a key and an IV
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
// Algorithm. Rijndael is available on all platforms.
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
//CryptoStream is for pumping our data.
cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return encryptedData;
}
public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
byte[] decryptedData = ms.ToArray();
return decryptedData;
}
// Decrypt a string into a string using a password
// Uses Decrypt(byte[], byte[], byte[])
public static string Decrypt(string cipherText, string Password)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedData);
}
}
public class CryptorEngine
{
///
/// Encrypt a string using dual encryption method. Return a encrypted cipher Text
///
/// string to be encrypted
/// use hashing? send to for extra secirity
///
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// Get the key from config file
//string key = (string)settingsReader.GetValue("xx&b[m-u797B", typeof(String));
string key = "xx&b[m-u797B";
//System.Windows.Forms.MessageBox.Show(key);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
///
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
///
/// encrypted string
/// Did you use hashing to encrypt this data? pass true is yes
///
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
//string key = (string)settingsReader.GetValue("xx&b[m-u797B", typeof(String));
string key = "xx&b[m-u797B";
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
Set Cookie Values
public bool SetCookie(string cookiename, string cookievalue, int iDaysToExpire)
{
try
{
HttpCookie objCookie = new HttpCookie(cookiename);
Response.Cookies.Add(objCookie);
objCookie.Values.Add(cookiename, Encrytion.Encrypt(cookievalue));
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies[cookiename].Expires = dtExpiry;
}
catch (Exception e)
{
return false;
}
return true;
}
public string GetCookie(string cookiename)
{
string cookyval = "";
try
{
cookyval = Request.Cookies[cookiename].Value.Replace(cookiename + "=", "");
cookyval = Encrytion.Decrypt(cookyval);
}
catch (Exception e)
{
cookyval = "";
}
return cookyval;
}
{
try
{
HttpCookie objCookie = new HttpCookie(cookiename);
Response.Cookies.Add(objCookie);
objCookie.Values.Add(cookiename, Encrytion.Encrypt(cookievalue));
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies[cookiename].Expires = dtExpiry;
}
catch (Exception e)
{
return false;
}
return true;
}
public string GetCookie(string cookiename)
{
string cookyval = "";
try
{
cookyval = Request.Cookies[cookiename].Value.Replace(cookiename + "=", "");
cookyval = Encrytion.Decrypt(cookyval);
}
catch (Exception e)
{
cookyval = "";
}
return cookyval;
}
25 Aralık 2009 Cuma
Parse Date String
string dateString = "27-02-2007";//
DateTime dt = DateTime.Parse(dateString);
// Throws a FormatException exception
IFormatProvider culture = new CultureInfo("fr-FR", true);
DateTime dt = DateTime.ParseExact(dateString, "dd-mm-yyyy", culture);
Console.WriteLine(dt);
output shows : 1/27/2007 0:02:00
DateTime dt = DateTime.Parse(dateString);
// Throws a FormatException exception
IFormatProvider culture = new CultureInfo("fr-FR", true);
DateTime dt = DateTime.ParseExact(dateString, "dd-mm-yyyy", culture);
Console.WriteLine(dt);
output shows : 1/27/2007 0:02:00
14 Şubat 2008 Perşembe
Backup SourceSafe Database
@echo off@title Backing up SourceSafe databasesset SsPath=C:\Program Files\Microsoft Visual SourceSafe\set BakPath=C:\deneme_sourcesrv_backup\ "%SsPath%ssarc.exe" -d- -s"C:\deneme_sourcesrv" -i- -yadmin, -o@"%BakPath%ArgusBackup(%DATE%).txt" "%BakPath%ArgusBackup(%DATE%).ssa" $/ echo Finished backups@echo on
1 Şubat 2008 Cuma
Formatting Numbers
decimal theDecNumber = 12345.678m; //the "m" creates a literal of type decimal from a
double //Using the ToString Method //the number in the format string is the precision specifier Console.WriteLine("No formatting: " + theDecNumber.ToString());
Console.WriteLine("Currency formatting: " + theDecNumber.ToString("C"));
Console.WriteLine("Exponential formatting: " + theDecNumber.ToString("E"));
Console.WriteLine("Fixed-point formatting: " + theDecNumber.ToString("F2")); Console.WriteLine("General formatting: " + theDecNumber.ToString("G"));
Console.WriteLine("Number formatting to 2 decimal places: " + theDecNumber.ToString("N2"));
Console.WriteLine("Number formatting to 3 decimal places: " + theDecNumber.ToString("N3"));
Console.WriteLine("Number formatting to 4 decimal places: " + theDecNumber.ToString("N4"));
Console.WriteLine("Percent formatting: " + theDecNumber.ToString("P0")); int theIntNumber = 123456;
Console.WriteLine("Hexidecimal formatting (for integers): {0} = {1}", theIntNumber, theIntNumber.ToString("X"));
double theDblNumber = 1234567890;
Console.WriteLine("Custom formatting: {0} to US telephone {1}", theDblNumber, theDblNumber.ToString( "(###) ### - ####" ));
double //Using the ToString Method //the number in the format string is the precision specifier Console.WriteLine("No formatting: " + theDecNumber.ToString());
Console.WriteLine("Currency formatting: " + theDecNumber.ToString("C"));
Console.WriteLine("Exponential formatting: " + theDecNumber.ToString("E"));
Console.WriteLine("Fixed-point formatting: " + theDecNumber.ToString("F2")); Console.WriteLine("General formatting: " + theDecNumber.ToString("G"));
Console.WriteLine("Number formatting to 2 decimal places: " + theDecNumber.ToString("N2"));
Console.WriteLine("Number formatting to 3 decimal places: " + theDecNumber.ToString("N3"));
Console.WriteLine("Number formatting to 4 decimal places: " + theDecNumber.ToString("N4"));
Console.WriteLine("Percent formatting: " + theDecNumber.ToString("P0")); int theIntNumber = 123456;
Console.WriteLine("Hexidecimal formatting (for integers): {0} = {1}", theIntNumber, theIntNumber.ToString("X"));
double theDblNumber = 1234567890;
Console.WriteLine("Custom formatting: {0} to US telephone {1}", theDblNumber, theDblNumber.ToString( "(###) ### - ####" ));
3 Aralık 2007 Pazartesi
Creating Dropdown Button
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeMyToolBar();
}
public void InitializeMyToolBar()
{
ToolBarButton toolBarButton3 = new ToolBarButton();
ToolBar toolBar1 = new ToolBar();
toolBar1.ButtonSize = new System.Drawing.Size(75, 23);
toolBar1.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
MenuItem menuItem1 = new MenuItem("Print");
MenuItem menuItem2 = new MenuItem("Close");
menuItem1.Click += new System.EventHandler(this.Print_Click);
menuItem2.Click += new System.EventHandler(this.Close_Click);
MenuItem[] mi = new MenuItem[2] {menuItem1, menuItem2};
ContextMenu contextMenu1 = new ContextMenu(mi);
toolBar1.Buttons.Add(toolBarButton3);
toolBar1.ShowToolTips = true;
toolBarButton3.Text = "Operations";
toolBarButton3.Style = ToolBarButtonStyle.DropDownButton;
toolBarButton3.ToolTipText = "Operations";
toolBarButton3.ImageIndex = 0;
toolBarButton3.DropDownMenu = contextMenu1;
panel1.Controls.Add(toolBar1);
}
private void Print_Click(object sender, EventArgs e)
{
MessageBox.Show("Print");
}
private void Close_Click(object sender, EventArgs e)
{
MessageBox.Show("Close");
}
}
{
public Form1()
{
InitializeComponent();
InitializeMyToolBar();
}
public void InitializeMyToolBar()
{
ToolBarButton toolBarButton3 = new ToolBarButton();
ToolBar toolBar1 = new ToolBar();
toolBar1.ButtonSize = new System.Drawing.Size(75, 23);
toolBar1.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
MenuItem menuItem1 = new MenuItem("Print");
MenuItem menuItem2 = new MenuItem("Close");
menuItem1.Click += new System.EventHandler(this.Print_Click);
menuItem2.Click += new System.EventHandler(this.Close_Click);
MenuItem[] mi = new MenuItem[2] {menuItem1, menuItem2};
ContextMenu contextMenu1 = new ContextMenu(mi);
toolBar1.Buttons.Add(toolBarButton3);
toolBar1.ShowToolTips = true;
toolBarButton3.Text = "Operations";
toolBarButton3.Style = ToolBarButtonStyle.DropDownButton;
toolBarButton3.ToolTipText = "Operations";
toolBarButton3.ImageIndex = 0;
toolBarButton3.DropDownMenu = contextMenu1;
panel1.Controls.Add(toolBar1);
}
private void Print_Click(object sender, EventArgs e)
{
MessageBox.Show("Print");
}
private void Close_Click(object sender, EventArgs e)
{
MessageBox.Show("Close");
}
}
Kaydol:
Kayıtlar (Atom)