using System; using System.Data; using System.Text; using System.Windows.Forms; using System.IO; using HotelPms.Share.IO; namespace HotelPms.Share.Windows.Tool { public partial class DBLogin : Form { private string listFile = @"\LoginInfo.vic"; private string userID = string.Empty; private string passWord = string.Empty; private string dataSource = string.Empty; private string catalog = string.Empty; /// ƒ†[ƒU[ public string UseID { get { return userID; } set { userID = value; } } /// ƒpƒXƒ[ƒh public string PassWord { get { return passWord; } set { passWord = value; } } /// ƒf[ƒ^ƒx[ƒXŠÝ‚̃pƒ\ƒRƒ“–¼ public string DataSource { get { return dataSource; } set { dataSource = value; } } /// ƒf[ƒ^ƒx[ƒX–¼ public string Catalog { get { return catalog; } set { catalog = value; } } public DBLogin() { //SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance; //System.Data.DataTable table = instance.GetDataSources(); InitializeComponent(); // Display the contents of the table. //DisplayData(table); } private void Login_Shown(object sender, EventArgs e) { if (this.cmbSrcDataSource.Items.Count > 0) { this.cmbSrcDataSource.SelectedIndex = 0; } if (!File.Exists(Application.StartupPath + listFile)) { return; } listBox1.Items.Clear(); using (StreamReader sr = new StreamReader(Application.StartupPath + listFile, Encoding.Default)) { string line = string.Empty; while ((line = sr.ReadLine()) != null) { listBox1.Items.Add(line); } sr.Close(); } } private void DisplayData(System.Data.DataTable table) { foreach (System.Data.DataRow row in table.Rows) { this.cmbSrcDataSource.Items.Add(row["ServerName"].ToString()); } } private void Save() { string line = cmbSrcDataSource.Text + "," + txtDstUser.Text + "," + txtDstPassword.Text + "," + txtDstCatalog.Text; if (!listBox1.Items.Contains(line)) { listBox1.Items.Add(line); } StringBuilder fileInfo = new StringBuilder(); foreach (string item in listBox1.Items) { fileInfo.Append(item + Environment.NewLine); } FileOperation.Delete(Application.StartupPath + listFile); File.WriteAllText(Application.StartupPath + listFile, fileInfo.ToString(), Encoding.Default); } private void btnLogin_Click(object sender, EventArgs e) { userID = txtDstUser.Text; passWord = txtDstPassword.Text; dataSource = cmbSrcDataSource.Text; catalog = txtDstCatalog.Text; Save(); this.DialogResult = DialogResult.OK; this.Close(); } private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if(listBox1.SelectedIndex == -1) { return;} string[] items = listBox1.Items[listBox1.SelectedIndex].ToString().Split(new char[]{','}); cmbSrcDataSource.Text = items[0]; txtDstUser.Text = items[1]; txtDstPassword.Text = items[2]; txtDstCatalog.Text = items[3]; } private void cmbSrcDataSource_DropDown(object sender, EventArgs e) { ComboBox obj = sender as ComboBox; if (obj.Items.Count == 0) { this.Cursor = System.Windows.Forms.Cursors.AppStarting; //SqlDataSourceEnumerator sqlServerList = System.Data.Sql.SqlDataSourceEnumerator.Instance; //using (DataTable dtServerList = sqlServerList.GetDataSources()) //{ // foreach (DataRow dRow in dtServerList.Rows) // { // obj.Items.Add(dRow["ServerName"].ToString()); // } //} this.Cursor = System.Windows.Forms.Cursors.Default; } } } }