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 阅读(262)
评论(0) 编辑 收藏 所属分类:
.Net