using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace ConnectTest
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox 結果;
private System.Windows.Forms.Timer timer;
private System.Windows.Forms.Button 開始ボタン;
private System.Windows.Forms.Button 停止ボタン;
private System.Windows.Forms.Button クリアボタン;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
timer.Tick += new EventHandler(timer_Tick);
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.開始ボタン = new System.Windows.Forms.Button();
this.停止ボタン = new System.Windows.Forms.Button();
this.結果 = new System.Windows.Forms.TextBox();
this.timer = new System.Windows.Forms.Timer(this.components);
this.クリアボタン = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// 開始ボタン
//
this.開始ボタン.Location = new System.Drawing.Point(16, 16);
this.開始ボタン.Name = "開始ボタン";
this.開始ボタン.TabIndex = 0;
this.開始ボタン.Text = "開始";
this.開始ボタン.Click += new System.EventHandler(this.開始ボタン_Click);
//
// 停止ボタン
//
this.停止ボタン.Location = new System.Drawing.Point(104, 16);
this.停止ボタン.Name = "停止ボタン";
this.停止ボタン.TabIndex = 1;
this.停止ボタン.Text = "停止";
this.停止ボタン.Click += new System.EventHandler(this.停止ボタン_Click);
//
// 結果
//
this.結果.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.結果.Location = new System.Drawing.Point(16, 56);
this.結果.Multiline = true;
this.結果.Name = "結果";
this.結果.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.結果.Size = new System.Drawing.Size(440, 264);
this.結果.TabIndex = 2;
this.結果.Text = "";
this.結果.WordWrap = false;
//
// timer
//
this.timer.Interval = 1000;
//
// クリアボタン
//
this.クリアボタン.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.クリアボタン.Location = new System.Drawing.Point(384, 16);
this.クリアボタン.Name = "クリアボタン";
this.クリアボタン.TabIndex = 3;
this.クリアボタン.Text = "クリア";
this.クリアボタン.Click += new System.EventHandler(this.クリアボタン_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(472, 333);
this.Controls.Add(this.クリアボタン);
this.Controls.Add(this.結果);
this.Controls.Add(this.停止ボタン);
this.Controls.Add(this.開始ボタン);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void timer_Tick(object sender, EventArgs e)
{
CheckDbIsAlive();
}
private string connectionString = "Data Source=別マシン;Initial Catalog=Northwind;User ID=scott;Password=tiger;Min Pool Size=1;Max Pool Size=1";
private string connectionString2 = "Data Source=別マシン;Initial Catalog=Northwind;User ID=scott;Password=tiger;Pooling=false";
private bool isAlive = true;
protected virtual void CheckDbIsAlive()
{
string connStr = isAlive ? connectionString : connectionString2;
SqlConnection con = new SqlConnection(connStr);
try
{
結果.AppendText(DateTime.Now.ToString() + " : ");
SqlCommand cmd = new SqlCommand("select getDate()", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
reader.Close();
結果.AppendText("接続完了\n");
isAlive = true;
}
catch (Exception ex)
{
結果.AppendText(ex.Message + "\n");
isAlive = false;
}
finally
{
con.Close();
}
}
private void 開始ボタン_Click(object sender, System.EventArgs e)
{
timer.Start();
}
private void 停止ボタン_Click(object sender, System.EventArgs e)
{
timer.Stop();
}
private void クリアボタン_Click(object sender, System.EventArgs e)
{
結果.Text = string.Empty;
}
}
}
|