アットウィキロゴ
■C#


  • デバッグ用コンパイル
#if
DEBUG
   Console.WriteLine("デバッグバージョンです。");
#endif

  • 文字列が数値であるか判定
public static bool IsNumeric(string stTarget)
{
   double dNullable;

   return double.TryParse(
       stTarget,
       System.Globalization.NumberStyles.Any,
       null,
       out dNullable
   );
}

  • 文字列が対象範囲内の数値であるか判定
public static bool IsAreaNumeric(String sTarget)
{
   double dNullable;
   if (!double.TryParse(
       sTarget,
       System.Globalization.NumberStyles.Any,
       null,
       out dNullable
   ))
   {
       return false;
   }
   //MessageBox.Show(dNullable.ToString());
   return true;
}

  • 文字列が対象範囲内の数値であるか判定
public static bool IsAreaNumeric(String sTarget, double dFrom, double dTo)
{
   double dNullable;
   if (!double.TryParse(
       sTarget,
       System.Globalization.NumberStyles.Any,
       null,
       out dNullable
   ))
   {
       return false;
   }
   if (dNullable < dFrom || dTo < dNullable)
   {
       return false;
   }
   return true;
}

  • 半角のみの文字列か判定
public static bool isHankaku(string str)
{
   Encoding sjisEnc = Encoding.GetEncoding("Shift_JIS");
   int num = sjisEnc.GetByteCount(str);
   return num == str.Length;
}

  • プロパティ(Settings.settings)の値を取得
String s = Properties.Settings.Default.[キー];


  • 手動接続(1)
#コネクション
System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
#接続文字列の設定
cn.ConnectionString = Properties.Settings.Default.db1ConnectionString;
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
cmd.Connection = cn;
cmd.CommandText ="SELECT * FROM 会員マスタ ";
cn.Open();
System.Data.OleDb.OleDbDataReader reader;
reader = cmd.ExecuteReader();
while (reader.Read())
{
String s = reader["支店コード"].ToString();
	:
	:
}
cn.Close();

  • 手動接続(2)
#コネクション
System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
#接続文字列の設定
cn.ConnectionString = Properties.Settings.Default.db1ConnectionString;
String sSql ="SELECT * FROM 会員マスタ ";
System.Data.OleDb.OleDbDataAdapter da
   = new System.Data.OleDb.OleDbDataAdapter(sSql, cn);
DataSet ds = new DataSet("YubinTable");
da.Fill(ds, "YubinTable");
DataTable dt = ds.Tables["YubinTable"];
int dtRowCnt = dt.Rows.Count;
foreach (DataRow myRow in dt.Rows)
{
String s = myRow["支店コード"].ToString();
	:
	:
}


  • クエリアダプタ
public static String getCalYm()
{
   String sRet = "";
   String sPym = "";
   DateTime dt = DateTime.Now;
   sPym = dt.ToString("yyyyMMdd");
   //            MessageBox.Show(sPym);
   db1DataSetTableAdapters.QueriesTableAdapter qta
       = new db1DataSetTableAdapters.QueriesTableAdapter();
   try
   {
       sRet = qta.ScalarQuery(sPym, sPym).ToString();
   }
   catch
   {
       MessageBox.Show("カレンダマスタに該当データがありません。");
   }
   return sRet;
}

  • ファイル出力
public static bool bOutFile(String sData,String sFileName)
{
   try
   {
       System.IO.StreamWriter writer = new StreamWriter(sFileName);
       writer.WriteLine(sData);
       //ファイルへ書き込み
       writer.Flush();
       //ファイルのロックを解除
       writer.Close();
       Console.WriteLine("書き込み終了");
   }
   catch (Exception e)
   {
       Console.WriteLine("ERR:" + e);
       return false;
   }
   return true;
}

  • フォーマット
private void setKaihiFormat()
{
   int nf;
   Int32.TryParse(月額授業料TextBox.Text, out nf);
   月額授業料TextBox.Text = String.Format("{0:C}", nf);
}

  • デバッグ用計測
Console.WriteLine("[Start]" + DateTime.Now.ToString("HH:mm:ss fff"))


  • タブ隠し
Appearance:FlatButtons
ItemSize:0,1
SizeMode:Fixed
(制御)
this.tabControl1.SelectedTab = tabPage1;

  • INIファイル値取得
inifile.getIniItem("項目名", "キー名")

※接続文字列についてのみ、
  setting.Setting(app.comfig.xml)にて設定。


  • ShowDialogについては必ずDispose()すること。
   Form2 form2 = new Form2() ;
   form2.ShowDialog() ;
    :
    :
   form2.Dispose() ;

  • アプリ呼び出し
try
{
   System.Diagnostics.Process proc =
       new System.Diagnostics.Process();

   String sDbc = Properties.Settings.Default.db1ConnectionString;
   int n = sDbc.IndexOf("C:");
   //MessageBox.Show(sDbc.Substring(n)); 

   proc.StartInfo.FileName = sDbc.Substring(n);
   proc.Start();

}catch(Exception ex){
   //TODO
   MessageBox.Show("Access起動エラー");
}

  • Pass付MDBに接続
Source=C:\cdb\dbc.mdb;Jet OLEDB:Database Password=pass

  • OLEDDBConnectを可変
get{
   return ((string)(this["db1ConnectionString"]));
}
set{
   this["db1ConnectionString"] = value;
}



最終更新:2009年07月16日 09:12