本篇博客使用的是phpmyadmin图形化管理工具,利用已经建立好的phpstudy管理工具进行按照。
小皮面板(phpstudy)
using MySql.Data.MySqlClient;
类名 | Description |
---|---|
MySqlConnection | 数据库连接类 |
MySqlCommand | 对数据库进行执行语句的类 |
MySqlDataReader | 提供一种从MySQL数据库单向读取的方法 |
private static MySqlConnection conn;
private static MySqlCommand cmd;
private static MySqlDataReader reader;
MySqlConnection(server,database,user,password)
Parameter | Description |
---|---|
server | 服务器地址-ip,本地使用localhost就可以 |
database | 对应数据库 |
user | 进入数据库账号名 |
password | 进入数据库密码 |
charset | 可选,指定字符编码,遇到中文乱码可能需要修改 |
conn = new MySqlConnection(connstr);
通过这条语句只是实例化了一个MySqlConnection对象,还需要通过conn.open()
进行数据库的连接。
static void ContoSQL()
{
string connstr = "server=localhost;database=user;user=root;password=root;charset = uft8";
conn = new MySqlConnection(connstr);
conn.Open(); //建立连接,打开数据库
Console.WriteLine("打开数据库成功");
}
关闭与数据库的连接,但不清理缓存
conn.close();
conn.Dispose()
具体详见MySqlConnection Methods
属性 | 描述 |
---|---|
ServerVersionser | mysql服务器版本 |
State | 连接状态 |
static void ContoSQL()
{
string connectStr = "server=localhost;database=user;user=root;password=root;";
conn = new MySqlConnection(connectStr);
conn.Open(); //建立连接,打开数据库
Console.WriteLine("ServerVersion: " + conn.ServerVersionser +
"\nState: " + conn.State.ToString());
conn.Close();
}
cmd = new MySqlCommand(sql, conn);//利用sql语句和一个连接对象实例化MysqlDataCommand对象
cmd.ExecuteNonQuery();//执行SQL语句,并返回受影响的行数。一般用在增删改里面
reader=cmd.ExecuteReader();//执行查询语句并将信息传给MysqlDataReader类
string sql="insert into test(字段名1,字段名2) values('22','ChenChen')";
string sql = "insert into test(username,F1) values('22','ChenChen')";
static void InsertSQL(string sql)
{
try
{
ContoSQL();
cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery(); //执行sql语句
}
catch (Exception ex)//如执行出错执行内部语句,执行完后关闭连接
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); //关闭连接
}
}
string sql = "update 数据表名 set 字段名 = 'FaFa' where 字段名 = '2002' ";
string sql = "update test set F1 = 'FaFa' where username = '2002' ";
static void UpdateSQL(string sql)
{
try
{
ContoSQL(); //建立连接
cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery(); //执行sql语句
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); //关闭连接
}
}
string sql = "Delete from test where username = '22' ";
static void DeleteSQL(string sql)
{
try
{
ContoSQL();
cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
1.几种常见查询语句用法
string sql ="select * from 数据表名 "
string sql = "select * from test";//读取所有行
string sql = "select* from test where username = '2002'"; //读取指定行
string sql = "select* from test where 密码='2'and username=2002";//多个限制条件
string sql = "select username,密码,F1 from test where 密码='2'and username=2002"; //读取指定列
方法 | 说明 |
---|---|
reader["字段名"] |
关联数组,通过字段名获取数据返回object类型,需通过.Tostring()转换 |
reader[num] |
索引数组,通过读取字段对应的排序,从0开始 |
reader.GetString("字段名/num") |
同上作用 |
更多资料请见MySqlDataReader Class
Console.WriteLine(reader["username"].ToString() +' '+ reader["密码"].ToString()+' '+ reader["F1"].ToString());
Console.WriteLine(reader[0].ToString() +' '+ reader[1].ToString()+' '+ reader[2].ToString());
static void ReadSQL(string sql)
{
try
{
ContoSQL();
cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
while (reader.Read()) 逐行读取数据,当下一行没有数据返回false
{
Console.WriteLine(reader["username"].ToString() +' '+ reader["F1"].ToString()+' '+reader["F2"]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); //关闭连接
}
}
using MySql.Data.MySqlClient;//引用命名空间,添加到首行
private static MySqlConnection conn;
private static MySqlCommand cmd;
private static MySqlDataReader reader;
static void ContoSQL()
{
string connectStr = "server=localhost;database=user;user=root;password=root;";
conn = new MySqlConnection(connectStr);
conn.Open(); //建立连接,打开数据库
Console.WriteLine("打开数据库成功");
}
///
/// 读取数据库数据
///
/// 执行的sql语句
static void ReadSQL(string sql)
{
try
{
ContoSQL();
cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
while (reader.Read()) //遍历表中数据
{
Console.WriteLine(reader[0].ToString() +' '+ reader["密码"].ToString()+' '+ reader["F1"].ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); //关闭连接
}
}
static void InsertSQL(string sql)
{
try
{
ContoSQL();
cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); //关闭连接
}
}
static void UpdateSQL(string sql)
{
try
{
ContoSQL();
cmd = new MySqlCommand(sql, conn);
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); //关闭连接
}
}
static void DeleteSQL(string sql)
{
try
{
ContoSQL();
cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); //关闭连接
}
}
static void Main(string[] args)
{
string sql = "select username,密码,F1 from test where 密码='2'and username=2002";
ReadSQL(sql);
sql = "select * from test";
ReadSQL(sql);
}