简体   繁体   中英

MYSQL Database Connection to ASPX website using C#

Can anyone write a simple example of how to connect from my ASPX website to my Database? I'm just starting in ASP, so please keep it simple.

MysqlDB Name : ASP
Class : Database.cs

maybe this will help

first of all you need to include some reference you can download them at this link: http://dev.mysql.com/downloads/connector/net/

here is the code:

using MySql.Data.MysqlClient;

namespace OwnNameSpace
{
  public class Database
  {
    MySqlConnection connect;
    string connection = "Data Source=localhost;Database=ASP;User ID=(your ID)";
//constructor
public Database()
{
}

  // this if want to select something in your db
  public MySqlDataReader Select(string query)
  {

      connect = new MySqlConnection(connection);
      connect.Close();
      MySqlCommand command = connect.CreateCommand();
      command.CommandText = query;
      connect.Open();
      MySqlDataReader reader;
      return reader = command.ExecuteReader();
  }

  // this if want to insert/delete or update 
  public Boolean Modify(string query)
  {

      connect = new MySqlConnection(connection);
      MySqlCommand command = connect.CreateCommand();
      command.CommandText = query;
      connect.Open();
      try
      {
         command.ExecuteNonQuery();
         return true;
      }
      catch
      {
        return false;
      }

   }



 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM