前言:
只是想低俗暴力的完成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
}
