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    認(rèn)証 ----- 0
                  
          /// SQL Server 認(rèn)証 ----- 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 閱讀(266) 評論(0)  編輯  收藏 所屬分類: .Net
          主站蜘蛛池模板: 鹤山市| 开封市| 正阳县| 大冶市| 讷河市| 浦县| 休宁县| 永平县| 龙胜| 龙海市| 香港 | 和林格尔县| 抚松县| 鱼台县| 远安县| 定陶县| 明水县| 黎城县| 绥阳县| 沾化县| 五原县| 泾川县| 西华县| 奉贤区| 武隆县| 繁峙县| 安多县| 漳浦县| 泸西县| 乌拉特后旗| 烟台市| 辰溪县| 同仁县| 东丽区| 西华县| 莆田市| 百色市| 同江市| 彭泽县| 从化市| 丽水市|