コーディング中常に使用される共通関数
/// <summary>
/// 年
/// </summary>
private const string MC_YEAR = "年";
/// <summary>
/// 月
/// </summary>
private const string MC_MONTH = "月";
/// <summary>
/// 日
/// </summary>
private const string MC_DAY = "日";
/// <summary>
/// NULL
/// </summary>
private const string MC_NULL = "NULL";
/// <summary>
/// =
/// </summary>
private const string MC_EQUAL = "=";
#region 共通チェックとデータ変換
/// <summary>
/// 和暦→西暦
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private string JACalendarToEN(object date)
{
string strRet = string.Empty;
if (IsDBNullOrNullOrEmpty(date) == false)
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
System.Globalization.Calendar cal = new System.Globalization.GregorianCalendar();
ci.DateTimeFormat.Calendar = cal;
strRet = DateTime.Parse(date.ToString()).ToString("yyyy/MM/dd", ci);
}
return strRet;
}
/// <summary>
/// 西暦→和暦
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private string ENCalendarToJA(object date)
{
string strRet = string.Empty;
if (IsDBNullOrNullOrEmpty(date) == false)
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ja-JP");
System.Globalization.Calendar cal = new System.Globalization.JapaneseCalendar();
ci.DateTimeFormat.Calendar = cal;
strRet = DateTime.Parse(date.ToString()).ToString("ggy年MM月dd日", ci);
}
return strRet;
}
/// <summary>
/// テキストボックス場合、DBデータへ変換
/// </summary>
/// <param name="strData"></param>
/// <returns></returns>
private string ConvertTxtToDBData(string strData)
{
string strRet = MC_NULL;
if (!string.IsNullOrEmpty(strData))
{
strRet = this.ChangeString(strData);
}
return strRet;
}
/// <summary>
/// 日付場合、DBデータへ変換
/// </summary>
/// <param name="strData"></param>
/// <returns></returns>
private string ConvertDateToDBData(string strData)
{
string strRet = MC_NULL;
if (!string.IsNullOrEmpty(strData))
{
strRet = this.JACalendarToEN(strData);
}
return strRet;
}
/// <summary>
/// コンボボックス場合、DBデータへ変換
/// </summary>
/// <param name="strData"></param>
/// <returns></returns>
private string ConvertCmbToDBData(object oData)
{
string strRet = MC_NULL;
if (!IsDBNullOrNullOrEmpty(oData))
{
strRet = oData.ToString();
}
return strRet;
}
/// <summary>
/// コンボボックス場合、コンボボックスvalueデータへ変換
/// </summary>
/// <param name="strData"></param>
/// <returns></returns>
private object ConvertToCmbData(object oData)
{
object oRet = DBNull.Value;
if (IsDBNullOrNullOrEmpty(oData) == false)
{
oRet = oData;
}
return oRet;
}
/// <summary>
/// チェックボックス場合、DBデータへ変換
/// </summary>
/// <param name="strData"></param>
/// <returns></returns>
private string ConvertChkToDBData(bool bData)
{
return Convert.ToInt32(bData).ToString();
}
/// <summary>
/// 文字列取得
/// </summary>
/// <param name="oData"></param>
/// <returns></returns>
private string ConvertToStringData(object oData)
{
string strRet = string.Empty;
if (IsDBNullOrNullOrEmpty(oData) == false)
{
strRet = oData.ToString();
}
return strRet;
}
/// <summary>
/// TRUE OR FALSE取得
/// </summary>
/// <param name="oData"></param>
/// <returns></returns>
private bool ConvertToBoolData(object oData)
{
bool bRet = false;
if (IsDBNullOrNullOrEmpty(oData) == false)
{
if (oData.ToString() == "1" || oData.ToString() == "0")
{
bRet = oData.ToString() == "1" ? true : false;
}
else
{
bRet = Convert.ToBoolean(oData);
}
}
return bRet;
}
/// <summary>
/// DBNull、Null、Emptyの判定
/// </summary>
/// <param name="oData"></param>
/// <returns></returns>
private bool IsDBNullOrNullOrEmpty(object oData)
{
bool bRet = false;
if (oData == DBNull.Value || oData == null || string.IsNullOrEmpty(oData.ToString().Trim()))
{
bRet = true;
}
return bRet;
}
/// <summary>
/// 数字判定
/// </summary>
/// <param name="oData"></param>
/// <returns></returns>
private bool IsNumber(object oData)
{
bool bRet = true;
try
{
if (!IsDBNullOrNullOrEmpty(oData))
{
double dblData = double.Parse(oData.ToString());
}
}
catch
{
bRet = false;
}
return bRet;
}
/// <summary>
/// 数字判定(小数点を含まない)
/// </summary>
/// <param name="oData"></param>
/// <returns></returns>
private bool IsNumberNotDot(object oData)
{
bool bRet = true;
try
{
if (!IsDBNullOrNullOrEmpty(oData))
{
if (oData.ToString().Contains("."))
{
bRet = false;
}
if (bRet)
{
double dblData = double.Parse(oData.ToString());
}
}
}
catch
{
bRet = false;
}
return bRet;
}
/// <summary>
/// 郵便番号、電話番号判定
/// </summary>
/// <param name="oData"></param>
/// <returns></returns>
private bool IsPostCodeOrPhone(object oData)
{
bool bRet = true;
try
{
if (!IsDBNullOrNullOrEmpty(oData))
{
string strPostCodeOrPhone = oData.ToString().Replace("-", "");
if (!IsNumberNotDot(strPostCodeOrPhone))
{
bRet = false;
}
}
}
catch
{
bRet = false;
}
return bRet;
}
/// <summary>
/// 数字また/
/// </summary>
/// <returns></returns>
private bool IsDateKey(object oData)
{
bool bRet = true;
try
{
if (!IsDBNullOrNullOrEmpty(oData))
{
string strPostCodeOrPhone = oData.ToString().Replace("/", "");
if (!IsNumberNotDot(strPostCodeOrPhone))
{
bRet = false;
}
}
}
catch
{
bRet = false;
}
return bRet;
}
/// <summary>
/// 画面上の和暦日付の取得
/// </summary>
/// <param name="cmbGengo"></param>
/// <param name="txtNen"></param>
/// <param name="txtTuki"></param>
/// <param name="txtHi"></param>
/// <returns></returns>
private string GetControlDateData(ComboBox cmbGengo, TextBox txtNen, TextBox txtTuki, TextBox txtHi)
{
string strDate = string.Empty;
strDate = cmbGengo.Text + txtNen.Text + txtTuki.Text + txtHi.Text;
if (!string.IsNullOrEmpty(strDate)
&& !string.IsNullOrEmpty(cmbGengo.Text)
&& !string.IsNullOrEmpty(txtNen.Text)
&& !string.IsNullOrEmpty(txtTuki.Text)
&& !string.IsNullOrEmpty(txtHi.Text))
{
strDate = cmbGengo.Text + txtNen.Text + MC_YEAR + txtTuki.Text + MC_MONTH + txtHi.Text + MC_DAY;
}
return strDate;
}
/// <summary>
/// 画面上の和暦日付のチェック
/// </summary>
/// <param name="strDate"></param>
/// <returns></returns>
private bool ChkControlJADateData(string strDate)
{
bool bRet = true;
try
{
if (!string.IsNullOrEmpty(strDate))
{
string strDateRet = this.JACalendarToEN(strDate);
}
}
catch
{
bRet = false;
}
return bRet;
}
/// <summary>
/// 画面上の西暦日付のチェック
/// </summary>
/// <param name="strDate"></param>
/// <returns></returns>
private bool ChkControlENDateData(string strDate)
{
bool bRet = true;
try
{
if (!string.IsNullOrEmpty(strDate))
{
string strDateRet = this.ENCalendarToJA(strDate);
}
}
catch
{
bRet = false;
}
return bRet;
}
#endregion
#region SQL文作成共通
/// <summary>
/// 新規場合、追加SQL文の作成
/// </summary>
/// <param name="strSqlInsCol"></param>
/// <param name="strSqlInsVal"></param>
private void GetInsertSQL(ref string strSqlInsCol, ref string strSqlInsVal)
{
string strSqlInsColRet = string.Empty;
string strSqlInsValRet = string.Empty;
string strCol = string.Empty;
string strVal = string.Empty;
//文字列場合、"'"追加と"'"を"''"に変更する
//カラム
strCol = "カラム列";
this.SubAddCol(strCol, ref strSqlInsColRet);
strVal = this.ConvertTxtToDBData("カラ'ム値");
this.SubAddVal(this.SubAddPos(strVal), ref strSqlInsValRet);
strSqlInsCol = strSqlInsColRet;
strSqlInsVal = strSqlInsValRet;
}
/// <summary>
/// 台帳編集場合、更新SQL文の作成
/// </summary>
/// <param name="strSqlInsCol"></param>
/// <param name="strSqlInsVal"></param>
private void GetUpdateSQL(ref string strSqlUpdSet)
{
string strSqlUpdSetRet = string.Empty;
string strCol = string.Empty;
string strVal = string.Empty;
//文字列場合、"'"追加と"'"を"''"に変更する
//カラム
strCol = "カラム列";
strVal = this.ConvertTxtToDBData("カラム値");
this.SubUpdSet(strCol, SubAddPos(strVal), ref strSqlUpdSetRet);
strSqlUpdSet = strSqlUpdSetRet;
}
/// <summary>
/// カラム名の追加
/// </summary>
/// <param name="strCol"></param>
/// <param name="strSql"></param>
private void SubAddCol(string strCol, ref string strSqlInsColRet)
{
if (IsDBNullOrNullOrEmpty(strSqlInsColRet))
{
strSqlInsColRet = strCol;
}
else
{
strSqlInsColRet = strSqlInsColRet + "," + strCol;
}
}
/// <summary>
/// 値の追加
/// </summary>
/// <param name="strVal"></param>
/// <param name="strSql"></param>
private void SubAddVal(string strVal, ref string strSqlInsValRet)
{
if (IsDBNullOrNullOrEmpty(strSqlInsValRet))
{
strSqlInsValRet = strVal;
}
else
{
strSqlInsValRet = strSqlInsValRet + "," + strVal;
}
}
/// <summary>
/// カラム、値の追加
/// </summary>
/// <param name="strCol"></param>
/// <param name="strVal"></param>
/// <param name="strSql"></param>
private void SubUpdSet(string strCol, string strVal, ref string strSqlUpdSetRet)
{
if (IsDBNullOrNullOrEmpty(strSqlUpdSetRet))
{
strSqlUpdSetRet = strCol + MC_EQUAL + strVal;
}
else
{
strSqlUpdSetRet = strSqlUpdSetRet + "," + "\r\n" + strCol + MC_EQUAL + strVal;
}
}
/// <summary>
/// 'の追加
/// </summary>
/// <param name="strVal"></param>
/// <returns></returns>
private string SubAddPos(string strVal)
{
string strValRet = strVal;
if (strVal != MC_NULL)
{
strValRet = "'" + strVal + "'";
}
return strValRet;
}
/// <summary>
/// "''"に変更する
/// </summary>
/// <param name="strVal"></param>
/// <returns></returns>
private string ChangeString(string strVal)
{
return strVal.Replace("'", "''");
}
#endregion