📄 frmmain.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBUtility;
using Utility;
using System.Threading;
using System.Data.SqlClient;
namespace 异库数据导出工具
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
FrmMain.CheckForIllegalCrossThreadCalls = false;
}
private void Run()
{
System.Data.DataTable table = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
this.comAServe.Items.Clear();
this.comBServe.Items.Clear();
foreach (System.Data.DataRow row in table.Rows)
{
if (row["InstanceName"].ToString().Trim() != "")
{
this.comAServe.Items.Add(row["ServerName"].ToString().Trim() + "\\" + row["InstanceName"].ToString().Trim());
this.comBServe.Items.Add(row["ServerName"].ToString().Trim() + "\\" + row["InstanceName"].ToString().Trim());
}
else
{
this.comAServe.Items.Add(row["ServerName"].ToString().Trim());
this.comBServe.Items.Add(row["ServerName"].ToString().Trim() + "\\" + row["InstanceName"].ToString().Trim());
}
}
if (this.toolStripStatusLabel1.Text == "正在获取服务器列表...")
{
this.toolStripStatusLabel1.Text = "服务器列表获取完毕";
}
if (this.toolStripStatusLabel2.Text == "正在获取服务器列表...")
{
this.toolStripStatusLabel2.Text = "服务器列表获取完毕";
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
//遍历获取当前的数据服务器
Thread thr = new Thread(new ThreadStart(Run));
thr.Start();
this.toolStripStatusLabel1.Text = "正在获取服务器列表...";
this.toolStripStatusLabel2.Text = "正在获取服务器列表...";
}
private void btnAConnect_Click(object sender, EventArgs e)
{
string strConA = "SERVER=" + this.comAServe.Text.Trim().Replace("'","").Replace("'","") + ";" +
"Initial Catalog=master;" +
"User=sa;" +
"Password=" + this.txtAPwd.Text.Trim().Replace("'", "");
try
{
DataSet ds = DbHelperSQL.Query("SELECT Name FROM sysdatabases", strConA);
this.comADataBase.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
this.comADataBase.Items.Add(ds.Tables[0].Rows[i]["Name"].ToString());
}
this.toolStripStatusLabel1.Text = "成功获取数据库列表";
if (this.comADataBase.Items.Count > 0)
{
this.comADataBase.SelectedIndex = 0;
}
}
catch ( Exception ex)
{
Pub.ShowWarning("数据源连接失败!" + "\n" + ex.Message );
}
}
private void btnBConnect_Click(object sender, EventArgs e)
{
string strConB = "SERVER=" + this.comBServe.Text.Trim() + ";" +
"Initial Catalog=master;" +
"User=sa;" +
"Password=" + this.txtBPwd.Text.Trim().Replace("'", "");
try
{
DataSet ds = DbHelperSQL.Query("SELECT Name FROM sysdatabases", strConB);
this.comBDataBase.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
this.comBDataBase.Items.Add(ds.Tables[0].Rows[i]["Name"].ToString());
}
this.toolStripStatusLabel2.Text = "成功获取数据库列表";
if (this.comBDataBase.Items.Count > 0)
{
this.comBDataBase.SelectedIndex = 0;
}
}
catch (Exception ex)
{
Pub.ShowWarning("目标源连接失败!" + "\n" + ex.Message);
}
}
private void comADataBase_SelectedIndexChanged(object sender, EventArgs e)
{
//根据选择的数据库名,获取表名
string strConA = "SERVER=" + this.comAServe.Text.Trim().Replace("'","") + ";" +
"Initial Catalog=" + this.comADataBase .Text .Trim () + ";" +
"User=sa;" +
"Password=" + this.txtAPwd.Text.Trim().Replace("'", "");
try
{
DataSet ds = DbHelperSQL.Query("SELECT Name FROM sysobjects where xtype= 'u '", strConA);
this.comATable.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
this.comATable.Items.Add(ds.Tables[0].Rows[i]["Name"].ToString());
}
if (this.comATable.Items.Count > 0)
{
this.comATable.SelectedIndex = 0;
}
}
catch (Exception ex)
{
Pub.ShowWarning("获取表名失败!" + "\n" + ex.Message);
}
}
private void comBDataBase_SelectedIndexChanged(object sender, EventArgs e)
{
//根据选择的数据库名,获取表名
string strConB = "SERVER=" + this.comBServe.Text.Trim() + ";" +
"Initial Catalog=" + this.comBDataBase.Text.Trim() + ";" +
"User=sa;" +
"Password=" + this.txtBPwd.Text.Trim().Replace("'", "");
try
{
DataSet ds = DbHelperSQL.Query("SELECT Name FROM sysobjects where xtype= 'u '", strConB);
this.comBTable.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
this.comBTable.Items.Add(ds.Tables[0].Rows[i]["Name"].ToString());
}
if (this.comBTable.Items.Count > 0)
{
this.comBTable.SelectedIndex = 0;
}
}
catch (Exception ex)
{
Pub.ShowWarning("获取表名失败!" + "\n" + ex.Message);
}
}
DataSet ds_FileA;
DataSet ds_FileB;
/// <summary>
/// 数据源键值对
/// </summary>
System.Collections.Hashtable HasA;
/// <summary>
/// 数据源键值对
/// </summary>
System.Collections.Hashtable HasB;
private void comATable_SelectedIndexChanged(object sender, EventArgs e)
{
//根据选择的表名,获取字段名
string strConA = "SERVER=" + this.comAServe.Text.Trim().Replace("'","") + ";" +
"Initial Catalog=" + this.comADataBase.Text.Trim() + ";" +
"User=sa;" +
"Password=" + this.txtAPwd.Text.Trim().Replace("'", "");
try
{
ds_FileA = DbHelperSQL.Query("SELECT a.Name as Name ,a.Length as Len,b.Name as [Type] FROM syscolumns as a,systypes b where a.xtype=b.xtype AND b.Name <> 'sysname' AND id=object_id('" + this.comATable.Text.Trim() + "')", strConA);
this.listA.Items.Clear();
HasA = new System.Collections.Hashtable();
for (int i = 0; i < ds_FileA.Tables[0].Rows.Count; i++)
{
Model A = new Model ();
A.Name = this.ds_FileA.Tables[0].Rows[i]["Name"].ToString().Trim();
A.Len = Convert.ToInt32 (this.ds_FileA.Tables[0].Rows[i]["Len"].ToString().Trim());
A.Type = this.ds_FileA.Tables[0].Rows[i]["Type"].ToString().Trim();
HasA.Add(ds_FileA.Tables[0].Rows[i]["Name"].ToString(), A);
this.listA.Items.Add(A.Name + " " + A.Type + "(" + A.Len.ToString () + ")");
}
if (this.listA.Items.Count > 0)
{
this.listA.SelectedIndex = 0;
}
}
catch (Exception ex)
{
Pub.ShowWarning("获取字段名失败!" + "\n" + ex.Message);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -