You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Main/Mesnac.Gui.Edit/Dialog/FrmNewDataSource.cs

291 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.Data.Core.Interfaces;
using ICSharpCode.Data.Core.DatabaseObjects;
using Mesnac.Basic;
using Mesnac.Gui.Edit.Global;
using Mesnac.Gui.Edit.Common;
using Mesnac.Gui.Edit.ViewContent;
using Mesnac.Gui.Edit.Pad;
namespace Mesnac.Gui.Edit.Dialog
{
public partial class FrmNewDataSource : Form
{
private string _dataSourceName;
private DataSourceItem _dataSourceItem = null;
private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption"));
private string msg = String.Empty;
public FrmNewDataSource()
{
InitializeComponent();
this.InitUIMethod();
this.InitMethod();
}
public FrmNewDataSource(string dataSourceName)
{
this._dataSourceName = dataSourceName;
InitializeComponent();
this.InitUIMethod();
this.InitMethod2();
}
/// <summary>
/// 初始化界面元素
/// </summary>
public void InitUIMethod()
{
this.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Text")); //数据源向导
this.label1.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_label1")); //数据源名称
this.label2.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_label2")); //数据库驱动
this.label3.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_label3")); //服务器
this.label4.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_label4")); //用户名
this.label5.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_label5")); //密码
this.label6.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_label6")); //数据库
this.label7.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_label7")); //连接超时
this.btnTestConnect.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_btnTestConnect")); //测试连接
this.btnOk.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnOK"));
this.btnCancel.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnCancel"));
}
/// <summary>
/// 新建时,初始化的方法
/// </summary>
public void InitMethod()
{
//获取默认数据源名称
string dataSourceName = DataSourceFactory.Instance.GetNextDataSourceName();
this.txtDataSourceName.Text = dataSourceName;
//加载数据驱动类型
this.cmbDataDrivers.DataSource = DatabaseDriver.DatabaseDrivers;
}
/// <summary>
/// 修改时,初始化的方法
/// </summary>
public void InitMethod2()
{
this._dataSourceItem = DataSourceFactory.Instance.DataSources[this._dataSourceName];
this.txtDataSourceName.Text = this._dataSourceName;
this.txtDataSourceName.ReadOnly = true;
//加载数据驱动类型
this.cmbDataDrivers.DataSource = DatabaseDriver.DatabaseDrivers;
if (this._dataSourceItem != null)
{
foreach (IDatabaseDriver driver in this.cmbDataDrivers.Items)
{
if (driver.Name == this._dataSourceItem.Driver)
{
this.cmbDataDrivers.SelectedItem = driver;
break;
}
}
this.txtServer.Text = this._dataSourceItem.Server;
this.txtUserName.Text = this._dataSourceItem.UserName;
this.txtPassword.Text = this._dataSourceItem.Password;
this.cmbDataBase.Text = this._dataSourceItem.Database;
this.txtConnectionTimeout.Text = this._dataSourceItem.ConnectionTimeout.ToString();
}
}
/// <summary>
/// 验证窗体条件
/// </summary>
/// <returns>验证成功返回true否则返回false</returns>
private bool ValidateForm()
{
if (String.IsNullOrEmpty(this.txtDataSourceName.Text))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg1")); //请输入数据源名称!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtDataSourceName.Focus();
return false;
}
if (this.cmbDataDrivers.SelectedIndex == -1)
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg2")); //请选择数据源类型!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.cmbDataDrivers.Focus();
return false;
}
if (String.IsNullOrEmpty(this.txtServer.Text))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg3")); //请输入服务器!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtServer.Focus();
return false;
}
if (String.IsNullOrEmpty(this.txtUserName.Text))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg4")); //请输入用户名!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtUserName.Focus();
return false;
}
//if (String.IsNullOrEmpty(this.txtPassword.Text))
//{
// this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg5")); //请输入密码!
// MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
// this.txtPassword.Focus();
// return false;
//}
if (String.IsNullOrEmpty(this.txtConnectionTimeout.Text))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg12")); //请输入连接超时时间,单位(秒)!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtConnectionTimeout.Focus();
return false;
}
///判断连接超时是否为数字
bool flag = true;
string strConnectionTimeOut = this.txtConnectionTimeout.Text.Trim();
foreach (char c in strConnectionTimeOut)
{
if (!Char.IsDigit(c))
{
flag = false;
break;
}
}
if (flag == false)
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg13")); //连接超时必须为整数,单位(秒)!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtConnectionTimeout.Focus();
return false;
}
if (String.IsNullOrEmpty(this._dataSourceName))
{
if (DataSourceFactory.Instance.IsExists(this.txtDataSourceName.Text))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg6")); //数据源已存在,请更改数据源名称!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtDataSourceName.Focus();
return false;
}
}
return true;
}
/// <summary>
/// 测试连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTestConnect_Click(object sender, EventArgs e)
{
//验证
if (!this.ValidateForm())
{
return;
}
try
{
IDatabaseDriver driver = this.cmbDataDrivers.SelectedItem as IDatabaseDriver;
if (driver != null)
{
IDatasource datasource = driver.CreateNewIDatasource( this.txtServer.Text , this.txtUserName.Text , this.txtPassword.Text );
driver.PopulateDatabases(datasource);
if (datasource.Databases != null)
{
this.cmbDataBase.DataSource = datasource.Databases;
if (!String.IsNullOrEmpty(this._dataSourceName))
{
foreach (IDatabase db in this.cmbDataBase.Items)
{
if (db.ToString() == this._dataSourceItem.Database)
{
this.cmbDataBase.SelectedItem = db;
break;
}
}
}
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg7")); //连接测试成功,请选择数据库创建数据源!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg8")); //连接测试失败,请检查网络连接状况!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg9")); //连接测试失败,原因:缺少数据驱动!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg10")); //连接测试失败,原因:{0}!
this.msg = String.Format(this.msg, ex.Message);
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnOk_Click(object sender, EventArgs e)
{
//验证
if (!this.ValidateForm())
{
return;
}
if (this.cmbDataBase.SelectedIndex == -1)
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewDataSource_Msg11")); //请选择数据库!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.cmbDataBase.Focus();
return;
}
DataSourceItem dataSourceItem = new DataSourceItem();
dataSourceItem.Name = this.txtDataSourceName.Text.Trim();
IDatabaseDriver driver = this.cmbDataDrivers.SelectedItem as IDatabaseDriver;
dataSourceItem.Driver = driver.Name;
dataSourceItem.DriverAssembly = driver.GetType().Assembly.FullName;
dataSourceItem.DriverClass = driver.GetType().FullName;
dataSourceItem.Server = this.txtServer.Text.Trim();
dataSourceItem.UserName = this.txtUserName.Text.Trim();
dataSourceItem.Password = this.txtPassword.Text.Trim();
IDatabase database = this.cmbDataBase.SelectedItem as IDatabase;
dataSourceItem.Database = database.Name;
int intConnectionTimeout = 5;
int.TryParse(this.txtConnectionTimeout.Text, out intConnectionTimeout);
dataSourceItem.ConnectionTimeout = intConnectionTimeout;
dataSourceItem.DataSourceClass = database.GetType().FullName;
if (!String.IsNullOrEmpty(this._dataSourceName))
{
//修改
DataSourceFactory.Instance.Modify(dataSourceItem.Name, dataSourceItem);
}
else
{
//添加
DataSourceFactory.Instance.Add(dataSourceItem.Name, dataSourceItem);
}
DisplayUtil<ProjectWindow>.SaveProjectDataSource(); //保存至文件
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
}
}