0

谁能写一个简单的例子来说明如何从我的 ASPX 网站连接到我的数据库?我刚开始使用 ASP,所以请保持简单。

MysqlDB 名称:ASP
类:Database.cs

4

1 回答 1

2

也许这会有所帮助

首先,您需要包含一些参考资料,您可以在此链接下载它们:http: //dev.mysql.com/downloads/connector/net/

这是代码:

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

   }



 }
}
于 2012-09-23T19:32:07.123 回答