ASP.NET Use SQLconnection Connect MySQL

ASP.NET use SqlConnection connect MySQL

SqlConnection is for SQL Server. You need MySqlConnection - this is not part of the .NET Framework, so you will have to download it and reference it in your project. You can then create a MySqlConnection object and connect to MySQL in your application:

MySqlConnection connection = new MySqlConnection(myConnString);

You will also have to use the MySqlCommand object rather than the SqlCommand object.

http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlconnection.html

Connecting MySQL database with Visual Studio

To connect to MySQL, you need MySqlConnection and a proper MySQL connection string:

private void DataAdd_Load(object sender, EventArgs e)
{
try
{
var conn = new MySqlConnection(@"Server=192.168.1.10;Database=myDB;Uid=myUsername;Pwd=myPassword;");
conn.Open();
MessageBox.Show("Connected to database");
}
catch (Exception e1)
{
MessageBox.Show("Connection failed");
}
}

How to correctly connect ASP.net Backend to local mysql server?

The SqlServerDbContextOptionsExtensions.UseSqlServer Method is used to configure the context to connect to a Microsoft SQL Server database, instead of MySQL database.

To connect with MySQL Database, you could install the MySql.Data package, then, use the MySqlConnection() method to open a connection to a MySQL Server database.

Here are some related articles about using MySQL with Asp.net core and EF Core, please check them (You could also search "asp.net core with mysql and entity framework core" online and find more tutorials):

How To Connect MySQL With ASP.NET Core

EF core Database Providers

How to properly use EF Core in ASP.NET Core with MySql Database

MVC application not able to connect to MySQL after deployment

Workbench is just a GUI to connect to and manage the MySQL server. I don't think that is your issue (...but it could be...I'd come back to that after confirming code-side issues aren't a factor).

The error you posted above is referring to SQL Server suggesting you have some kind of config issue server-side.

For reference, this is a working connection string using MySQL and the 'connector' nuget packages.

<add name="[your_connection_name]" 
connectionString="Server=[my_db_ip];Database=[your_db_name];Uid=[your_username];Pwd=[your_password];"
providerName="MySql.Data.MySqlClient" />

How do you make a call to your database locally? could we see some code?

Initial things to check:

  • are you 100% sure you are connecting to and can read/update data in your local MySQL server?
  • the live server accepts connections on 127.0.0.1 (sometimes it can be localhost for example)
  • check that MySQL is setup correctly in your web.config file.

update based on your web.config

I've taken a look and comparitively there are a few differences between your web.config and my one but my site is very old (MVC3 / MySQL Connector 6.8.3) so things might not work the same now. I also use my own UnitOfWork/Dapper integration to access the DB. I also use Auth/Membership where as you don't seem to.

Anyway, I've also noticed that entry for defaultConnectionFactory seems to refer to local connection. I'd check this.

You also have System.Data.SqlClient (SQL Server) reference in providers (which could be what the live server is picking up. This should probably be something like this (with your MySQL version):

<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>

Possible help? Entity Framework defaultConnectionFactory for MySQL

Here (for reference) are the sections I have:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
...
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.8.3.0" newVersion="6.8.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MySql.Data.Entity" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.8.3.0" newVersion="6.7.5.0" />
</dependentAssembly>
...
</assemblyBinding>
</runtime>

also:

I have:





I did once have a case where the live server required DbProviderFactories but locally I didn't. This was a long time ago though!

Hope that helps!

Azure - ASP.NET MVC connect to mysql database

SqlConnection is for SQL Server Databases. you should use MySqlConnection instead.

using (MySqlConnection conn = new MySqlConnection("server..."){}

MySQL database and ASP.NET global connection

You might be missing the required DLL file, MySQL.Data in a production environment. You can use the NuGet package* MySQL.Data* then build and deploy.

Alternatively, you can place the MySQL.Data DLL file in your bin folder and deploy.



Related Topics



Leave a reply



Submit