using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Windows.Forms;
          using System.Data.SqlClient;
          using System.Data.OleDb;

          namespace CSL.Nouka.Library.Presentation
          {
              
          //-------------------------------------------------------------------------
              /// <summary>
              
          /// FrmKihonSetting
              
          /// </summary>
              //-------------------------------------------------------------------------
              public partial class FrmKihonSetting : Form
              {
                  
          private string connStr = "";
                  
          private OleDbConnection mySqlConnection; 

                  
          //-------------------------------------------------------------------------
                  /// <summary>
                  
          /// init FrmKihonSetting
                  
          /// </summary>
                  //-------------------------------------------------------------------------
                  public FrmKihonSetting()
                  {
                      InitializeComponent();

                      
          this.txtServerName.Text = CSL.Nouka.Library.Properties.Settings.Default.DBSettingServerName;
                      
          this.cmbCertification.SelectedIndex = Convert.ToInt32(CSL.Nouka.Library.Properties.Settings.Default.DBSettingCmbCertificationSelectedIndex);
                      
          this.txtUserName.Text   = CSL.Nouka.Library.Properties.Settings.Default.DBSettingTxtUserName;
                      
          this.txtPswd.Text       = CSL.Nouka.Library.Properties.Settings.Default.DBSettingTxtPswd;
                  }

                  
          //-------------------------------------------------------------------------
                  /// <summary>
                  
          /// Windows    認証 ----- 0
                  
          /// SQL Server 認証 ----- 1
                  
          /// </summary>
                  
          /// <param name="sender"></param>
                  
          /// <param name="e"></param>
                  //-------------------------------------------------------------------------
                  private void cmbCertification_SelectedIndexChanged(object sender, EventArgs e)
                  {
                      
          if (cmbCertification.SelectedIndex.ToString() == "0")
                      {
                          
          this.labelUserName.Enabled = false;
                          
          this.labelPswd.Enabled     = false;
                          
          this.txtUserName.Enabled   = false;
                          
          this.txtPswd.Enabled       = false;
                      }
                      
          else if (cmbCertification.SelectedIndex.ToString() == "1")
                      {
                          
          this.labelUserName.Enabled = true;
                          
          this.labelPswd.Enabled     = true;
                          
          this.txtUserName.Enabled   = true;
                          
          this.txtPswd.Enabled       = true;
                      }
                  }

                  
          //-------------------------------------------------------------------------
                  /// <summary>
                  
          /// OK
                  
          /// </summary>
                  
          /// <param name="sender"></param>
                  
          /// <param name="e"></param>
                  //-------------------------------------------------------------------------
                  private void btnOK_Click(object sender, EventArgs e)
                  {
                      
          if (cmbCertification.SelectedIndex.ToString() == "0")
                      {
                          connStr 
          = "Provider=SQLOLEDB;Server=" + this.txtServerName.Text + ";Trusted_Connection=Yes;Database=Nouka";
                      }
                      
          else
                      {
                          connStr 
          = "Provider=SQLOLEDB;Server=" + this.txtServerName.Text + ";uid=" + this.txtUserName.Text + ";pwd=" + this.txtPswd.Text + ";Database=Nouka";
                          CSL.Nouka.Library.Properties.Settings.Default.DBSettingTxtUserName 
          = this.txtUserName.Text;
                          CSL.Nouka.Library.Properties.Settings.Default.DBSettingTxtPswd 
          = this.txtPswd.Text;
                      }
                      CSL.Nouka.Library.Properties.Settings.Default.ConnStr 
          = connStr;
                      CSL.Nouka.Library.Properties.Settings.Default.DBSettingCmbCertificationSelectedIndex 
          = cmbCertification.SelectedIndex.ToString();
                      CSL.Nouka.Library.Properties.Settings.Default.DBSettingServerName 
          = this.txtServerName.Text;
                      
          if (isConnectSucceed(connStr))
                      {
                          CSL.Nouka.Library.Properties.Settings.Default.Save();
                          MessageBox.Show(
          "データベースに接続しました。""接続", MessageBoxButtons.OK);
                          
          this.Close();
                      }
                      
          else
                      {
                          MessageBox.Show(
          "接続失敗しました。""接続", MessageBoxButtons.OK);
                      }
                  }

                  
          //-------------------------------------------------------------------------
                  /// <summary>
                  
          /// Cancel
                  
          /// </summary>
                  
          /// <param name="sender"></param>
                  
          /// <param name="e"></param>
                  //-------------------------------------------------------------------------
                  private void btnCancel_Click(object sender, EventArgs e)
                  {
                      
          this.Close();
                  }

                  
          //-------------------------------------------------------------------------
                  /// <summary>
                  
          /// Test Connection
                  
          /// </summary>
                  
          /// <param name="connStr"></param>
                  
          /// <returns></returns>
                  //-------------------------------------------------------------------------
                  private bool isConnectSucceed(string connStr)
                  {
                      
          bool isSucceed = false;
                      
          try
                      {
                          mySqlConnection 
          = new OleDbConnection(connStr);
                          mySqlConnection.Open();
                          isSucceed 
          = true;
                      }
                      
          catch
                      {
                          isSucceed 
          = false;
                      }
                      
          finally
                      {
                          mySqlConnection.Close();
                      }
                      
          return isSucceed;
                  }
              }
          }








          posted on 2010-11-24 16:13 Ying-er 閱讀(267) 評論(0)  編輯  收藏 所屬分類: .Net
          主站蜘蛛池模板: 黄石市| 白朗县| 深泽县| 专栏| 乌拉特后旗| 加查县| 修武县| 宝山区| 连平县| 武定县| 大洼县| 方山县| 平山县| 香河县| 靖江市| 麻栗坡县| 肃南| 扶风县| 常德市| 明溪县| 石台县| 仙游县| 英山县| 南平市| 承德县| 萨迦县| 当阳市| 桃园市| 永德县| 浦江县| 顺义区| 荆州市| 大厂| 清新县| 琼中| 宜春市| 阿坝县| 太白县| 青龙| 隆林| 东辽县|