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 belocalhost
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
How to Tell JSON.Net Globally to Apply the Stringenumconverter to All Enums
Calling a C# Library from Python
How to Call (Iron)Python Code from a C# App
Variable Declaration in a C# Switch Statement
Add "Everyone" Privilege to Folder Using C#.Net
Are Static Methods Thread Safe
Why Aren't Variables Declared in "Try" in Scope in "Catch" or "Finally"
What Are Checked Exceptions in Java/C#
Stroke Width Transform (Swt) Implementation (Java, C#...)
C# Namespace Alias - What's the Point
How to Create a Task (Tpl) Running a Sta Thread
How Does Creating a Instance of Class Inside of the Class Itself Works
How to Upload a File to an Sftp Server in C# (.Net)
Linq to SQL: Multiple Joins on Multiple Columns. Is This Possible