欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET操作MySql数据库的实例代码讲解

程序员文章站 2022-05-14 21:34:11
一、把mysql.data.dll放到bin目录下。  二、这是aspx.cs的全部源码,修改参数直接运行即可!   usin...

一、把mysql.data.dll放到bin目录下。 

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

using mysql.data.mysqlclient; 
using system; 
using system.collections.generic; 
using system.data; 
using system.linq; 
using system.web; 
using system.web.ui; 
using system.web.ui.webcontrols; 
public partial class login : system.web.ui.page 
{ 
  public static class mysqlhelper 
  { 
    public static int executenonquery(string connectionstring, commandtype commandtype, string commandtext) 
    { 
      return executenonquery(connectionstring, commandtype, commandtext, null); 
    } 
    public static int executenonquery(string connectionstring, commandtype commandtype, string commandtext, params mysqlparameter[] commandparameters) 
    { 
      if (string.isnullorempty(connectionstring)) 
      { 
        throw new exception("connectionstring exception"); 
      } 
      int result = 0; 
      mysqlconnection con = null; 
      try 
      { 
        using (con = new mysqlconnection(connectionstring)) 
        { 
          con.open(); 
          mysqlcommand command = new mysqlcommand(commandtext, con); 
          command.commandtype = commandtype; 
          result = command.executenonquery(); 
        } 
        return result; 
      } 
      catch (exception ex) 
      { 
        throw ex; 
      } 
      finally 
      { 
        if (con.state == connectionstate.open) 
        { 
          con.close(); 
        } 
      } 
    } 
  } 
  protected void page_load(object sender, eventargs e) 
  { 
    string connectionstring = "server=localhost;uid=root;pwd=;database=zentaopro;"; 
    logmanage_sqldate.writelog("connectionstring=:" + connectionstring); 
    string sql = "insert user(account) values('china2')"; 
    mysqlhelper.executenonquery(connectionstring, commandtype.text, sql); 
    console.read(); 
  } 
} 

以上所述是小编给大家介绍的asp.net操作mysql数据库的实例代码讲解,希望对大家有所帮助