0

If I am using Visual Studio 2019, how do I correctly set a new ASP.Net project to use MySQL for auth rather than Microsoft SQL?

I've got MySQL for VS installed along with their connector, when I look in server explorer I can see MySQL as a data source type, however when I am on the connected services section to add the required DB to the project itself, MySQL doesn't seem to come up as a data source.

I appreciate there are ways to "back door" it and built myself, however if possible I'd like to use the basic starting point you get with MSSQL on ASP.NET but with a MySQL database.

Thanks.

4

1 Answer 1

0

If you using ASP.NET (.NET Framework)!

Simple way to connect to MySQL:

  1. Add a new module (*if you are interested: using this method you can connect both MySQL and MSSQL databases at the same time.).

    Imports MySql.Data.MySqlClient
    

    Imports System.Data.SqlClient

    Module SQL
        Public Function GetConnectionMySQLServer() As MySqlConnection
            Dim conn As String = "server=xxx.xxx.xxx.xxx;
                                  user=xxx;
                                  database=xxx;
                                  port=xxxx;
                                  password=xxxx;"
            Return New MySqlConnection(conn)
        End Function
    
        Public Function GetConnectionSQLServer() As SqlConnection
            Dim conn As String = "server=xxx.xxx.xxx.xxx;
                                  User ID=xxx; 
                                  Password=xxx;
                                  Database=xxx"
            Return New SqlConnection(conn)
        End Function
    End Module
    
  2. Exmaple of using:

    Imports MySql.Data.MySqlClient

    Public Class Page
        Inherits System.Web.UI.Page
    
    Protected Sub btnAplly(sender As Object, e As EventArgs) Handles btnAplly.Click
        Using con As MySqlConnection = GetConnectionMySQLServer()
            con.Open()
            Dim sql As String = "select count(*) as cnt from users"
            Dim cmd As New SqlCommand(sql, con)
            Dim read As MySqlDataReader = cmd.ExecuteReader()
            read.Read()
            label1.Text = "exist " & read.Item("cnt") & " users"
            con.Close()
        End Using
    End Sub
    End Class
    

if you using c# you can use: https://converter.telerik.com/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but I'm trying (if possible) to do it with the service dependencie level in VS not by adding extra code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.