static void Main(string[] args) { DataConnectionDialog dcd = new DataConnectionDialog(); DataConnectionConfiguration dcs = new DataConnectionConfiguration(null); dcs.LoadConfiguration(dcd); if (DataConnectionDialog.Show(dcd) == DialogResult.OK) { // load tables using (SqlConnection connection = new SqlConnection(dcd.ConnectionString)) { connection.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM sys.Tables", connection); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(reader.HasRows); } } } } dcs.SaveConfiguration(dcd); }
/// <summary> /// Connection (and reconnection) to the database using Microsoft's native interface /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void connect(object sender, EventArgs e) { DataConnectionDialog dcd = new DataConnectionDialog(); DataConnectionConfiguration dcc = new DataConnectionConfiguration(null); dcc.LoadConfiguration(dcd); // Initialize connection string from current or previous (saved) connection string if (mySqlServer1.ConnectionPool.GetConnection(0) != null && mySqlServer1.ConnectionPool.GetConnection(0).State == ConnectionState.Open) { dcd.ConnectionString = mySqlServer1.ConnectionPool.GetConnection(0).ConnectionString; } else if (!string.IsNullOrEmpty(AppSettings.Default.LastConnectionString)) { dcd.ConnectionString = AppSettings.Default.LastConnectionString; } if (DataConnectionDialog.Show(dcd) == DialogResult.OK) { AppSettings.Default.LastConnectionString = dcd.ConnectionString; AppSettings.Default.Save(); dcc.SaveConfiguration(dcd); var succ = mySqlServer1.RefreshServers(dcd.ConnectionString); if (succ) { DatabaseBrowserDockForm.RefreshObjects(); } } }
/// <summary> /// Create connection string using Microsoft's ConnectionUI class /// </summary> /// <param name="DataSource">Default catalog</param> /// <param name="SaveConfiguration">true to save, false not to save</param> /// <returns></returns> public bool GetConnection(ref string DataSource, bool SaveConfiguration = false) { var success = false; var dcd = new DataConnectionDialog(); var dcs = new DataConnectionConfiguration(null); dcs.LoadConfiguration(dcd); if (DataConnectionDialog.Show(dcd) == DialogResult.OK) { DbProviderFactory factory = DbProviderFactories.GetFactory(dcd.SelectedDataProvider.Name); using (var connection = factory.CreateConnection()) { connection.ConnectionString = dcd.ConnectionString; ConnectionString = connection.ConnectionString; DataSource = connection.DataSource; connection.Open(); var cmd = connection.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES"; var dt = new DataTable(); dt.Load(cmd.ExecuteReader()); TableNames = dt.AsEnumerable() .Select(row => row.Field <string>("table_name")) .OrderBy(field => field) .ToList(); } var builder = new SqlConnectionStringBuilder() { ConnectionString = dcd.ConnectionString }; ServerName = builder.DataSource; InitialCatalog = builder.InitialCatalog; if (SaveConfiguration) { dcs.SaveConfiguration(dcd); } if (TableNames.Count > 0) { success = true; } } return(success); }
private string GetConnectionString() { string connectionString = null; DataConnectionDialog dcd = new DataConnectionDialog(); DataConnectionConfiguration dcs = new DataConnectionConfiguration(null); dcs.LoadConfiguration(dcd); if (DataConnectionDialog.Show(dcd) == DialogResult.OK) { connectionString = dcd.ConnectionString; } dcs.SaveConfiguration(dcd); return(connectionString); }
private void выборПодключенияToolStripMenuItem_Click(object sender, EventArgs e) { string connectionString = null; DataConnectionDialog dcd = new DataConnectionDialog(); DataConnectionConfiguration dcs = new DataConnectionConfiguration(null); dcs.LoadConfiguration(dcd); if (DataConnectionDialog.Show(dcd) == DialogResult.OK) { connectionString = dcd.ConnectionString; } dcs.SaveConfiguration(dcd); File.WriteAllText(@"connectionsString.connstr", connectionString); }
static void Main(string[] args) { DataConnectionDialog dcd = new DataConnectionDialog(); DataConnectionConfiguration dcs = new DataConnectionConfiguration(null); dcs.LoadConfiguration(dcd); if (DataConnectionDialog.Show(dcd) == DialogResult.OK) { DbProviderFactory factory = DbProviderFactories.GetFactory(dcd.SelectedDataProvider.Name); using (var connection = factory.CreateConnection()) { connection.ConnectionString = dcd.ConnectionString; connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES"; using (var reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(reader["name"]); } } } // load tables //using (SqlConnection connection = new SqlConnection(dcd.ConnectionString)) //{ // connection.Open(); // SqlCommand cmd = new SqlCommand("SELECT * FROM sys.Tables", connection); // using (SqlDataReader reader = cmd.ExecuteReader()) // { // while (reader.Read()) // { // Console.WriteLine(reader.HasRows); // } // } //} } dcs.SaveConfiguration(dcd); }
private void chooseDatabaseBtn_Click(object sender, EventArgs e) { DataConnectionDialog dcd = new DataConnectionDialog(); DataConnectionConfiguration dcs = new DataConnectionConfiguration(null); dcs.LoadConfiguration(dcd); if (DataConnectionDialog.Show(dcd) == DialogResult.OK) { databaseTxt.Text = dcd.ConnectionString; connectionString = dcd.ConnectionString; dataSourceType = dcd.SelectedDataSource.DisplayName; Console.WriteLine("here it is :" + dataSourceType); switch (dataSourceType) { //case "Microsoft SQL Server Compact 3.5": // try // { // using (SqlCeConnection con = new SqlCeConnection(connectionString)) // { // tableTxt.Items.Clear(); // con.Open(); // using (SqlCeCommand command = new SqlCeCommand("SELECT table_name FROM INFORMATION_SCHEMA.Tables", con)) // { // SqlCeDataReader reader = command.ExecuteReader(); // while (reader.Read()) // { // tableTxt.Items.Add(reader.GetString(0)); // } // } // con.Close(); // } // dcs.SaveConfiguration(dcd); // tableTxt.Enabled = true; // } // catch (Exception ex) // { // MessageBox.Show("Unable to establish Connection..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // } // break; case "Microsoft SQL Server": try { using (SqlConnection con = new SqlConnection(connectionString)) { tableTxt.Items.Clear(); con.Open(); using (SqlCommand command = new SqlCommand("SELECT table_name FROM INFORMATION_SCHEMA.Tables", con)) { SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { tableTxt.Items.Add(reader.GetString(0)); } } con.Close(); } dcs.SaveConfiguration(dcd); tableTxt.Enabled = true; } catch (Exception ex) { MessageBox.Show("Unable to establish Connection..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; default: MessageBox.Show("Not Implemented Yet..!!"); break; } } }