PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How to use mySql instead of Microsoft SQL server-VBForums
Results 1 to 2 of 2

Thread: How to use mySql instead of Microsoft SQL server

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    238

    How to use mySql instead of Microsoft SQL server

    Please can someone help me out on how I can use mySql in my database application instead of using Microsoft SQL server? Due to some reasons, I don't want to use Microsoft sql server.
    Nothing I post is Self Reliable. Use it at your own risk

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,048

    Re: How to use mySql instead of Microsoft SQL server

    ADO.NET is basically the same regardless of the data source, so your application will change very little. In fact, you can even convert an application that currently uses SQL Server to use MySQL with very little effort.

    Firstly, you need to reference Connector/Net in your project. You can add it as a NuGet package on a per project basis or you can install it on your dev machine and then reference it directly. You can find the download for Connector/Net and other MySQL tools here.

    Once you've done that, you simply use types from the MySql.Data.MySqlClient namespace instead of the equivalent types from the System.Data.SqlClient namespace. For instance, where you would use a SqlConnection to connect to SQL Server, you use a MySqlConnection to connect to MySQL. If you install the MySQL tools for VS then you can also do things like create a typed DataSet that uses MySqlClient types under the hood.

    The pattern of your code will be exactly the same. For example, here's some example code I wrote using SQL Server:
    vb.net Code:
    1. Private Sub ExecuteQuery()
    2.     Dim connection As New SqlConnection(My.Settings.ConnectionString)
    3.     Dim command As New SqlCommand("SELECT * FROM Data WHERE DateAndTime >= @StartDate AND DateAndTime < @EndDate", connection)
    4.  
    5.     command.Parameters.AddWithValue("@StartDate", fromDatePicker.Value.Date)
    6.     command.Parameters.AddWithValue("@EndDate", toDatePicker.Value.Date.AddDays(1))
    7.  
    8.     Dim adapter As New SqlDataAdapter(command)
    9.     Dim table As New DataTable
    10.  
    11.     adapter.Fill(table)
    12.     DataGridView1.DataSource = table
    13. End Sub
    and here's that code converted for MySQL:
    vb.net Code:
    1. Private Sub ExecuteQuery()
    2.     Dim connection As New MySqlConnection(My.Settings.ConnectionString)
    3.     Dim command As New MySqlCommand("SELECT * FROM Data WHERE DateAndTime >= @StartDate AND DateAndTime < @EndDate", connection)
    4.  
    5.     command.Parameters.AddWithValue("@StartDate", fromDatePicker.Value.Date)
    6.     command.Parameters.AddWithValue("@EndDate", toDatePicker.Value.Date.AddDays(1))
    7.  
    8.     Dim adapter As New MySqlDataAdapter(command)
    9.     Dim table As New DataTable
    10.  
    11.     adapter.Fill(table)
    12.     DataGridView1.DataSource = table
    13. End Sub
    The only changes are the 'My' on the type names. You'd have to change the connection string, of course. You can find details of what's needed for that here. You may need to some minor changes to the SQL code in some cases too, although in other cases you won't need any. MySQL even added support for "@" as a parameter prefix to Connector/Net for maximum compatibility.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width