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
          主站蜘蛛池模板: 保定市| 新泰市| 贺兰县| 灵璧县| 万载县| 石棉县| 资阳市| 永登县| 资中县| 永泰县| 霍城县| 泽普县| 河北省| 贵定县| 望谟县| 紫阳县| 鲁甸县| 高尔夫| 高台县| 胶州市| 中西区| 巫山县| 云林县| 娄底市| 敦煌市| 哈巴河县| 山东| 安仁县| 商都县| 通许县| 琼结县| 澄迈县| 高唐县| 铁岭市| 石泉县| 台山市| 陈巴尔虎旗| 芜湖市| 安新县| 广丰县| 贡嘎县|