点击(此处)折叠或打开

  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using MySql.Data.MySqlClient;
  12. //☆qau 555 2015.11 博盈杯
  13. namespace DB_
  14. {
  15.     public class DB
  16.     {

  17.         public DB()
  18.         {
  19.             // 在此处添加构造函数逻辑
  20.         }

  21.         public MySqlConnection conntent()
  22.         {
  23.             string constr = "Data Source=127.0.0.1;port=3306;Database=test;User Id=root;Password=root;CharSet=utf8;allow zero datetime=true";
  24.             MySqlConnection con = new MySqlConnection(constr);
  25.             try
  26.             {
  27.                 con.Open();
  28.                 return con;// 成功
  29.             }
  30.             catch (Exception ex)
  31.             {
  32.                 return null;//连接打开出现异常
  33.             }
  34.         }//连接字符串,返回MySqlConnection 链接对象

  35.         public DataSet Select(string sqlstr)//返回一个DataSet数据集或NULL
  36.         {
  37.             MySqlDataAdapter myadapter = new MySqlDataAdapter(sqlstr, conntent());
  38.              DataSet select = new DataSet();
  39.             int suc_count = myadapter.Fill(select);
  40.            if (select != null) { return select; }
  41.             else { return null; }
  42.         }

  43.         public MySqlDataReader Select1(string sqlstr)//返回一个MySqlDataReader对象,或null
  44.         {
  45.             MySqlCommand mycmd = new MySqlCommand(sqlstr,conntent());
  46.             try
  47.             {
  48.                 MySqlDataReader select1 = mycmd.ExecuteReader();
  49.                 if (select1 != null){ return select1; }
  50.                 else { return null; }
  51.             }
  52.             catch (Exception ex)
  53.             {
  54.                 return null;
  55.             }
  56.         }

  57.         public bool ZSG(string sqlstr)//根据传人的sqlstr 和 连接对象,执行操作
  58.         {//调用ZSG()函数进行增删改的操作,成功TRUE,失败FALSE
  59.                 MySqlCommand mycmd = new MySqlCommand(sqlstr, conntent());
  60.                 try
  61.                 {
  62.                     if (mycmd.ExecuteNonQuery() > 0)
  63.                     { return true;//成功 }
  64.                     else
  65.                     { return false; }
  66.                 }
  67.                 catch{ return false; }
  68.         }
  69.     }
  70. }
Asp.net-Mysql数据库的增删改查-LMLPHP
09-17 15:29