需要从网上下载一个MySql.Data.dll,转载他人的 实验成功!中间出现权限不够问题,只要把数据库mysql里面user里面的host字段下面的localhost改成%,重启MYSQL就OK了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace classLogin
{
public class dbhelper
{
public string dodb()
{
MySqlConnection mysql = getMySqlCon();
//查询sql
String sqlSearch = "select user_name,user_id from pc_user";
//插入sql
String sqlInsert = "insert into student values (12,'张三',25,'大专')";
//修改sql
String sqlUpdate = "update student set name='李四' where id= 3";
//删除sql
String sqlDel = "delete from student where id = 12";
//打印SQL语句
//Console.WriteLine(sqlDel);
//四种语句对象
MySqlCommand mySqlCommand = getSqlCommand(sqlSearch, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlInsert, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlUpdate, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlDel, mysql);
mysql.Open();
string ab=getResultset(mySqlCommand);
//getInsert(mySqlCommand);
//getUpdate(mySqlCommand);
//getDel(mySqlCommand);
//记得关闭
mysql.Close();
String readLine = Console.ReadLine();
return ab;
}
/// <summary>
/// 建立mysql数据库链接
/// </summary>
/// <returns></returns>
public static MySqlConnection getMySqlCon()
{
String mysqlStr = "Database=school_pc;Data Source=192.168.0.149;User Id=root;Password=";
// String mySqlCon = ConfigurationManager.ConnectionStrings["MySqlCon"].ConnectionString;
MySqlConnection mysql = new MySqlConnection(mysqlStr);
return mysql;
}
/// <summary>
/// 建立执行命令语句对象
/// </summary>
/// <param name="sql"></param>
/// <param name="mysql"></param>
/// <returns></returns>
public static MySqlCommand getSqlCommand(String sql, MySqlConnection mysql)
{
MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
// MySqlCommand mySqlCommand = new MySqlCommand(sql);
// mySqlCommand.Connection = mysql;
return mySqlCommand;
}
/// <summary>
/// 查询并获得结果集并遍历
/// </summary>
/// <param name="mySqlCommand"></param>
public string getResultset(MySqlCommand mySqlCommand)
{
string a="";
MySqlDataReader reader = mySqlCommand.ExecuteReader();
try
{
while (reader.Read())
{
if (reader.HasRows)
{
a="编号:" + reader[1] + "|姓名:" + reader[0] ;
}
}
}
catch (Exception e)
{
a="查询失败了!"+e.Message;
}
finally
{
reader.Close();
}
return a;
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="mySqlCommand"></param>
public static void getInsert(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("插入数据失败了!" + message);
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="mySqlCommand"></param>
public static void getUpdate(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("修改数据失败了!" + message);
}
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="mySqlCommand"></param>
public static void getDel(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("删除数据失败了!" + message);
}
}
}
}
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。