public IEnumerable <object> Run(MySqlConnectionInfo connectionString, string text, Action <Type> returnTypeCreated = null) { using (var connection = new MySqlConnection()) { connection.ConnectionString = connectionString.ToConnectionString(); connection.Open(); //sqlConnection.ChangeDatabase(databaseName); using (var sqlCommand = new MySqlCommand(text, connection)) { using (var sqlReader = sqlCommand.ExecuteReader()) { List <(string name, Type type)> fields = new List <(string name, Type type)>(); for (int i = 0; i < sqlReader.FieldCount; i++) { var name = sqlReader.GetName(i); var type = sqlReader.GetFieldType(i); fields.Add((name, type)); } Type returnType = new ExpressionHelper().CreateAnonymousType(fields); returnTypeCreated?.Invoke(returnType); while (sqlReader.Read()) { var item = Activator.CreateInstance(returnType); for (int i = 0; i < sqlReader.FieldCount; i++) { var name = sqlReader.GetName(i); returnType.GetField(name).SetValue(item, sqlReader.GetValue(i)); } yield return(item); } } } } }
public async Task TryConnectAsync(MySqlConnectionInfo connectionString, CancellationToken ct) { using (MySqlConnection connection = new MySqlConnection()) { connection.ConnectionString = connectionString.ToConnectionString(); await connection.OpenAsync(ct); } }
public void TryConnect(MySqlConnectionInfo connectionString, string databaseName) { using (MySqlConnection connection = new MySqlConnection()) { connection.ConnectionString = connectionString.ToConnectionString(); connection.Open(); connection.ChangeDatabase(databaseName); } }
private void SetupMySqlOptions(DbConnection connection) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseMySql(connection); _options.Initialize(optionsBuilder.Options); MySqlConnectionInfo.SetServerVersion((MySqlConnection)connection, _serviceProvider); }
public List <string> GetDatabases(MySqlConnectionInfo connectionInfo) { using (MySqlConnection connection = new MySqlConnection()) { connection.ConnectionString = connectionInfo.ToConnectionString(); connection.Open(); var databasesNames = connection.GetSchema("Databases").AsEnumerable().Select(s => s[1].ToString()).ToList(); return(databasesNames); } }
public Configuration() { // Because the Package Manager Console (NuGet) instantiates YourDbContext with the empty constructor, // a custom connection must be specified. Based on http://www.devart.com/blogs/dotconnect/?p=5603 // Note that the MySqlProviderFactory must also be present in Web.config or App.config in the *startup project* // for this to work! Configuration example: /* * <system.data> * <DbProviderFactories> * <clear /> * <remove invariant="Devart.Data.MySql" /> * <add name="dotConnect for MySQL" invariant="Devart.Data.MySql" description="Devart dotConnect for MySQL" type="Devart.Data.MySql.MySqlProviderFactory, Devart.Data.MySql, Version=6.30.196.0, Culture=neutral, PublicKeyToken=09af7300eec23701" /> * </DbProviderFactories> * </system.data> */ // Apply the IgnoreSchemaName workaround MySqlEntityProviderConfig.Instance.Workarounds.IgnoreSchemaName = true; // Create a custom connection to specify the database and set a SQL generator for MySql. var connectionInfo = MySqlConnectionInfo.CreateConnection("<Your ConnectionString>"); TargetDatabase = connectionInfo; SetSqlGenerator(connectionInfo.GetInvariantName(), new MySqlEntityMigrationSqlGenerator()); // Enable automatic migrations if you like AutomaticMigrationsEnabled = false; // There is some problem with referencing EntityFramework 4.3.1.0 for me, so another fix that needs // to be applied in Web.config is this: /* * <runtime> * <assemblyBinding> * <!-- This redirection is needed for EntityFramework Migrations through the Package Manager Console (NuGet) --> * <dependentAssembly> * <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" /> * <bindingRedirect oldVersion="4.3.0.0" newVersion="4.3.1.0" /> * </dependentAssembly> * </assemblyBinding> * </runtime> */ // After these Web.config additions, running migrations in Package Manager Console should be as easy as: // Update-Database -Verbose -ProjectName Your.MigrationsProject // Creating new migrations: // Add-Migration -Name MigrationDescription -ProjectName Your.MigrationsProject }
public List <string> GetViews(MySqlConnectionInfo connectionInfo, string database) { using (MySqlConnection connection = new MySqlConnection()) { connection.ConnectionString = connectionInfo.ToConnectionString(); connection.Open(); connection.ChangeDatabase(database); var views = connection.GetSchema("Tables") .AsEnumerable() .Where(x => x["TABLE_TYPE"]?.ToString() == "VIEW") .Where(x => x["TABLE_SCHEMA"]?.ToString() == database) .Select(t => t["TABLE_NAME"]?.ToString()) .ToList(); return(views); } }
public void ConnectionInfo(MySqlConnectionInfo info, string expected) { Assert.AreEqual(expected, info.ConnectionString); }
private void btnSubmit_Click(object sender, RoutedEventArgs e) { try { var sel = cbProvider.SelectedItem; switch (sel) { case ConnectionProvider.MsSql: var conInfo = new MsSqlConnectionInfo() { Server = txtServer.Text, Port = Convert.ToInt32(string.IsNullOrWhiteSpace(txtPort.Text) ? "-1" : txtPort.Text), TrustedConnection = chkIntegratedSecurity.IsChecked.Value, Username = txtUsername.Text, Password = txtPassword.Password, Database = cbDatabase.Text }; Sql = new MsSqlService(); Sql.Build(conInfo); Sql.TestConnect(); break; case ConnectionProvider.PostgreSql: var npgInfo = new NpgConnectionInfo { Server = txtServer.Text, Port = Convert.ToInt32(string.IsNullOrWhiteSpace(txtPort.Text) ? "-1" : txtPort.Text), TrustedConnection = chkIntegratedSecurity.IsChecked.Value, Username = txtUsername.Text, Password = txtPassword.Password, Database = cbDatabase.Text }; Sql = new NpgService(); Sql.Build(npgInfo); Sql.TestConnect(); break; case ConnectionProvider.MySql: if (chkIntegratedSecurity.IsChecked == true) { throw new InvalidOperationException("Integrated Security doesnt supported by MySql Server"); } var myInfo = new MySqlConnectionInfo { Server = txtServer.Text, Port = Convert.ToInt32(string.IsNullOrWhiteSpace(txtPort.Text) ? "-1" : txtPort.Text), Username = txtUsername.Text, Password = txtPassword.Password, Database = cbDatabase.Text }; Sql = new MySqlService(); Sql.Build(myInfo); Sql.TestConnect(); break; } DialogResult = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void cbDatabase_DropDownOpened(object sender, EventArgs e) { cbDatabase.ItemsSource = new List <string> { "<Loading databases>" }; var sel = Enum.Parse <ConnectionProvider>(cbProvider.SelectedItem.ToString()); try { switch (sel) { case ConnectionProvider.MsSql: var conInfo = new MsSqlConnectionInfo { Server = txtServer.Text, Port = Convert.ToInt32(string.IsNullOrWhiteSpace(txtPort.Text) ? "-1" : txtPort.Text), TrustedConnection = chkIntegratedSecurity.IsChecked.Value, Username = txtUsername.Text, Password = txtPassword.Password }; var mssql = new MsSqlService(); cbDatabase.ItemsSource = mssql.LoadDatabase(conInfo); break; case ConnectionProvider.PostgreSql: var npgInfo = new NpgConnectionInfo { Server = txtServer.Text, Port = Convert.ToInt32(string.IsNullOrWhiteSpace(txtPort.Text) ? "-1" : txtPort.Text), TrustedConnection = chkIntegratedSecurity.IsChecked.Value, Username = txtUsername.Text, Password = txtPassword.Password }; var npgsql = new NpgService(); cbDatabase.ItemsSource = npgsql.LoadDatabase(npgInfo); break; case ConnectionProvider.MySql: if (chkIntegratedSecurity.IsChecked == true) { throw new InvalidOperationException("Integrated Security doesnt supported by MySql Server"); } var myInfo = new MySqlConnectionInfo { Server = txtServer.Text, Port = Convert.ToInt32(string.IsNullOrWhiteSpace(txtPort.Text) ? "-1" : txtPort.Text), Username = txtUsername.Text, Password = txtPassword.Password }; var mysql = new MySqlService(); cbDatabase.ItemsSource = mysql.LoadDatabase(myInfo); break; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }