Difference between revisions of "Connect MySQL to ASP.Net Web App"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==Install MySQL.Data== Using the Nuget Package Manager install the following: * MySql.Data; * MySql.Data.MySqlClient;")
 
(Using the Connection)
 
(19 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
* MySql.Data;
 
* MySql.Data;
 
* MySql.Data.MySqlClient;
 
* MySql.Data.MySqlClient;
 +
 +
==Creating the connection==
 +
Now using the page you want to connect, open the Model `cs` code.
 +
 +
At the top add the following
 +
<syntaxhighlight lang=c#>
 +
using MySql.Data;
 +
using MySql.Data.MySqlClient;
 +
</syntaxhighlight>
 +
 +
 +
You need to have a local webserver running with an installation of MySQL & PHP. I have used the USBWebServer from your SQL lessons. I have also created this connection string. The string below includes the name of the database, mine is just called `test`. The string should be inside the subclass of `PageModel`:
 +
 +
<syntaxhighlight lang=c#>
 +
string connection = "server=localhost;user=root;database=test;port=3306;password=usbw;";
 +
</syntaxhighlight>
 +
 +
Finally I created this method to get the connection, it should be inside the subclass of `PageModel`:
 +
<syntaxhighlight lang=c#>
 +
        public MySqlConnection GetConnection
 +
        {
 +
            get
 +
            {
 +
                return new MySqlConnection(connection);
 +
            }
 +
        }
 +
</syntaxhighlight>
 +
 +
==Using the Connection==
 +
Now, in the OnGet method I have added the following code, which will create, open, and close the database connection:
 +
<syntaxhighlight lang=c#>
 +
public void OnGet()
 +
{
 +
    using var connection = GetConnection; // this gets the connection
 +
 +
    connection.Open(); // you must open the connection
 +
           
 +
    connection.Close();
 +
}
 +
</syntaxhighlight>

Latest revision as of 07:24, 23 October 2024

Install MySQL.Data

Using the Nuget Package Manager install the following:

  • MySql.Data;
  • MySql.Data.MySqlClient;

Creating the connection

Now using the page you want to connect, open the Model `cs` code.

At the top add the following

using MySql.Data;
using MySql.Data.MySqlClient;


You need to have a local webserver running with an installation of MySQL & PHP. I have used the USBWebServer from your SQL lessons. I have also created this connection string. The string below includes the name of the database, mine is just called `test`. The string should be inside the subclass of `PageModel`:

string connection = "server=localhost;user=root;database=test;port=3306;password=usbw;";

Finally I created this method to get the connection, it should be inside the subclass of `PageModel`:

        public MySqlConnection GetConnection 
        {
            get
            {
                return new MySqlConnection(connection);
            }
        }

Using the Connection

Now, in the OnGet method I have added the following code, which will create, open, and close the database connection:

public void OnGet()
{
    using var connection = GetConnection; // this gets the connection

    connection.Open(); // you must open the connection
            
    connection.Close();
}