mysqlclassc#_操作mysql的C#类详解
需要mysql.data.dll
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
///
using MySql.Data.MySqlClient;
namespace NameSpace
{
webstorm中文版class MysqlConnection
{
MySqlConnection mysqlConnection;
DataSet dataSet;
string IP = null;
string UserName = "root";
string Password = "root";
string Database = null;
public MysqlConnection()
{
try
{
mysqlConnection = new
MySqlConnection("datasource=20.0.0.20;username=root;password=root;database=sysinfo;charset=gb2312"); }
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
public MysqlConnection(string IP,string UserName,string Password,string Database)
{
string connectionString = "datasource=" + IP + ";username="+UserName+";password="+Password+";database=" + Database+ ";charset=gb2312" ;
mysqlConnection = new MySqlConnection(connectionString);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
public string MysqlInfo()
{
string mysqlInfo = null;
try
{
mysqlConnection.Open();
mysqlInfo += "Connection Opened." + Environment.NewLine;
mysqlInfo += "Connection String:" + mysqlConnection.ConnectionString.ToString() + Environment.NewLine;
mysqlInfo += "Database:" + mysqlConnection.Database.ToString() + Environment.NewLine;
mysqlInfo += "Connection ServerVersion:" + mysqlConnection.ServerVersion.ToString() + Environment.NewLine;
mysqlInfo += "Connection State:" + mysqlConnection.State.ToString() + Environment.NewLine;
}
catch (MySqlException ex)
{
Console.WriteLine("MySqlException Error:" + ex.ToString());
}
finally
{
mysqlConnection.Close();
}
return mysqlInfo;
}
public int MysqlCommand(string MysqlCommand)
{
mysqlConnection.Open();
Console.WriteLine("MysqlConnection Opened.");
convert命令的作用MySqlCommand mysqlCommand = new MySqlCommand(MysqlCommand, mysqlConnection);
return mysqlCommand.ExecuteNonQuery();
}
catch (MySqlException ex)
{
Console.WriteLine("MySqlException Error:" + ex.ToString());
if (Regex.IsMatch(ex.ToString(), ""))
{
MessageBox.Show("数据库已经存在唯⼀键值");
}
}
finally
{
mysqlConnection.Close();
}
return -1;
}
mysql面试题详解
//
public DataView MysqlDataAdapter(string table)
{
DataView dataView = new DataView();
try
{
单引号被过滤sql注入mysqlConnection.Open();
MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter("Select * from " + table, mysqlConnection); dataSet = new DataSet();
mysqlDataAdapter.Fill(dataSet, table);
dataView = dataSet.Tables[table].DefaultView;nike react科技
}
catch (MySqlException ex)
{
手机编程新手教学MessageBox.Show(ex.Message); }
finally
{
mysqlConnection.Close();
}
return dataView;
}
}//end class
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。