/// <summary> /// Open the Ini file. /// </summary> /// <param name="path">The file name and path.</param> public static void Open(string path) { _path = path; try { string line = string.Empty; string ID = string.Empty; StreamReader file = new StreamReader(_path); StringBuilder temp = new StringBuilder(255); while ((line = file.ReadLine()) != null) { if (line.Contains("[")) { ID = line.Substring(1, 36); Section section = new Section(); section.Id = ID; GetPrivateProfileString(ID, items[0], "", temp, 255, _path); section.InitialCatalog = temp.ToString(); GetPrivateProfileString(ID, items[1], "", temp, 255, _path); section.ProviderName = temp.ToString(); GetPrivateProfileString(ID, items[2], "", temp, 255, _path); section.DataSource = temp.ToString(); GetPrivateProfileString(ID, items[3], "", temp, 255, _path); section.DataSourceIndex = temp.ToString(); GetPrivateProfileString(ID, items[4], "", temp, 255, _path); section.ConnectionString = temp.ToString(); GetPrivateProfileString(ID, items[5], "", temp, 255, _path); section.UserId = temp.ToString(); GetPrivateProfileString(ID, items[6], "", temp, 255, _path); section.Password = temp.ToString(); sectionList.Add(section); } } file.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void TaskConnection() { string ID = string.Empty; string initialCatalog = string.Empty; string connectionString = string.Empty; bool IsID = false; initialCatalog = Path.GetFileName(textBox2.Text); MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(initialCatalog)); Guid guid = new Guid(hash); ID = guid.ToString("D").ToUpper(); foreach (var i in IniFile.SectionList) { if (i.Id == ID) { IsID = true; } } connectionString += "Provider=Microsoft.ACE.OLEDB.12.0;"; connectionString += "Data Source=" + textBox2.Text + ";"; if (textBox4.Text.Length > 0) { connectionString += "User ID=" + textBox3.Text + ";"; connectionString += "Password="******";"; } DatabaseEx dataEx = new DatabaseEx(Provider.OleDb, connectionString); if (textBox2.Text.Length > 0) { if (File.Exists(textBox2.Text)) { switch (_mainEx.Action) { case MainExAction.Test: if (dataEx.TestConnection()) MessageBox.Show("Test connection succeeded.", "T-FLEX CAD Extension", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case MainExAction.Add: Section section = new Section(); section.DatabaseEx = new DatabaseEx(Provider.OleDb, connectionString); if (section.DatabaseEx.TestConnection()) { section.Id = ID; section.InitialCatalog = initialCatalog; section.ProviderName = "System.Data.OleDb"; section.DataSource = textBox2.Text; section.DataSourceIndex = "1"; section.ConnectionString = '"' + connectionString + '"'; section.UserId = checkBox1.Checked ? textBox3.Text : null; section.Password = checkBox1.Checked ? textBox4.Text : null; if (IsID) IniFile.UpdateSection(section); else IniFile.AddSection(section); } else { _mainEx.Action = MainExAction.Close; } break; } } else { MessageBox.Show("Could not find file '" + textBox2.Text + "'.", "T-FLEX CAD Extension", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("This connection cannot be tested because no database file has been specified.", "T-FLEX CAD Extension", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void TaskConnection(ref Section item) { switch (item.ProviderName) { case "System.Data.OleDb": if (item.DatabaseEx == null) item.DatabaseEx = new DatabaseEx(Provider.OleDb, item.ConnectionString); break; case "System.Data.Odbc": if (item.DatabaseEx == null) item.DatabaseEx = new DatabaseEx(Provider.Odbc, item.ConnectionString); break; case "System.Data.SqlClient": if (item.DatabaseEx == null) item.DatabaseEx = new DatabaseEx(Provider.SqlClient, item.ConnectionString); break; } if (_mainEx.Action == MainExAction.Add) { item.DatabaseEx.Open(); } }
/// <summary> /// Update section the Ini file. /// </summary> /// <param name="item"></param> public static void UpdateSection(Section item) { WritePrivateProfileString(item.Id, items[0], item.InitialCatalog, _path); WritePrivateProfileString(item.Id, items[1], item.ProviderName, _path); WritePrivateProfileString(item.Id, items[2], item.DataSource, _path); WritePrivateProfileString(item.Id, items[3], item.DataSourceIndex, _path); WritePrivateProfileString(item.Id, items[4], item.ConnectionString, _path); WritePrivateProfileString(item.Id, items[5], item.UserId, _path); WritePrivateProfileString(item.Id, items[6], item.Password, _path); }
/// <summary> /// Remove section the Ini file. /// </summary> /// <param name="item"></param> public static void RemoveSection(Section item) { WritePrivateProfileString(item.Id, null, null, _path); sectionList.Remove(item); }
/// <summary> /// Append section the Ini file. /// </summary> /// <param name="item"></param> public static void AddSection(Section item) { UpdateSection(item); sectionList.Add(item); }