ASP.NET操作MySql数据库的教程

作者:袖梨 2022-06-25

一、把MySql.Data.dll放到BIN目录下。 

二、这是aspx.cs的全部源码,修改参数直接运行即可!  

usingMySql.Data.MySqlClient;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
publicpartialclassLogin : System.Web.UI.Page
{
  publicstaticclassMySqlHelper
  {
    publicstaticintExecuteNonQuery(stringconnectionString, CommandType commandtype,stringcommandText)
    {
      returnExecuteNonQuery(connectionString, commandtype, commandText,null);
    }
    publicstaticintExecuteNonQuery(stringconnectionString, CommandType commandtype,stringcommandText,paramsMySqlParameter[] commandParameters)
    {
      if(string.IsNullOrEmpty(connectionString))
      {
        thrownewException("connectionString exception");
      }
      intresult = 0;
      MySqlConnection con =null;
      try
      {
        using(con =newMySqlConnection(connectionString))
        {
          con.Open();
          MySqlCommand command =newMySqlCommand(commandText, con);
          command.CommandType = commandtype;
          result = command.ExecuteNonQuery();
        }
        returnresult;
      }
      catch(Exception ex)
      {
        throwex;
      }
      finally
      {
        if(con.State == ConnectionState.Open)
        {
          con.Close();
        }
      }
    }
  }
  protectedvoidPage_Load(objectsender, EventArgs e)
  {
    stringconnectionString ="server=localhost;uid=root;pwd=;database=zentaopro;";
    LogManage_SqlDate.WriteLog("connectionString=:"+ connectionString);
    stringsql ="insert user(account) values('china2')";
    MySqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
    Console.Read();
  }
}

相关文章

精彩推荐