C#MysqlHelper

C基础算法 cyanprobe 9年前 (2015-11-22) 4248次浏览 已收录 2个评论

前言:

只是想低俗暴力的完成C#的结束小项目,so,暴力的低俗的写了这个SQLhelper,连个带参查询都没有,必定能注入….
大神勿喷,只是做下纪律,证明我也学过C#…..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace myapp
{
    abstract class mysql
    {
        public static readonly string conmessage = "server=localhost;database=admin_my;user=root;password=asd123;port=3306;Charset=utf8;";
        #region //管理员初始化
        public static int check()
        {
            try
            {
                using (MySqlConnection mysqlcon = new MySqlConnection(conmessage))
                {
                    mysqlcon.Open();
                        return 1;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        } //对库的创建
        public static int  connection()
        {
            {
                try
                {
                    string d = "server=localhost;database=admin_my;user=root;password=asd123;port=3306;Charset=utf8;";
                    using (MySqlConnection mysqlcon = new MySqlConnection(d))
                    {
                        mysqlcon.Open();
                        /*SQL语句执行建表*/
                        string com = "alter database admin_my character set utf8; create table `students`(ID int(100) auto_increment not null primary key,name varchar(14),user_name varchar(14),password varchar(14),score int(3),SID varchar(55),time_end Datetime,response varchar(44));create table `question` (QID int(3)    auto_increment not null primary key,question varchar(900),option_a char(45),option_b char(45),option_c char(45),option_d char(45),answer char(2));create table `bquestion` (BQID int(3) auto_increment not null primary key,bquestion varchar(20000)); create table `banswer`( stname varchar(5),stanswer varchar(2000),stanswer1 varchar(2000),stanswer2 varchar(2000))";
                        using (MySqlCommand mysqlcom = new MySqlCommand(com, mysqlcon))
                        {
                            mysqlcom.ExecuteNonQuery();
                            return 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        } //实现完整表的创建
        #endregion//
        #region//mysqlhelper
        public static string getconmessage()
        {
            return conmessage;
        }
        public static int atmysqlExecuteNonquery(string sql)
        {
            try
            {
                using (MySqlConnection con = new MySqlConnection(getconmessage()))
                {
                    con.Open();
                    using (MySqlCommand com = new MySqlCommand(sql, con))
                    {
                        return com.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
        }
        public static object atmysqlExcuteScalar(string sql)
        {
            try
            {
                using (MySqlConnection con = new MySqlConnection(getconmessage()))
                {
                    con.Open();
                    using (MySqlCommand com = new MySqlCommand(sql, con))
                    {
                        return com.ExecuteScalar();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return -1;
            }
        }
        public static MySqlDataReader atmysqlExcuteReader(string sql)
        {
            try {
            MySqlConnection con = new MySqlConnection(getconmessage());
            con.Open();
            using (MySqlCommand com = new MySqlCommand(sql, con))
            {
                return com.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
    }
    }
    #endregion
}

CyanProbe , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#MysqlHelper
喜欢 (1)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. 我能说高大上吗?很厉害的博主哦~
    姜辰2015-11-22 23:19 回复