0

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

2

1 Answer 1

2

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;
      }

   }



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

1 Comment

You'll need to make sure that all of your MySqlDataReader, MySqlConnection, and MySqlCommand objects are properly disposed of.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.