c#连接mysql数据库查询语句_C#连接MySQL数据库(增删改查)

using System;

using MySql.Data.MySqlClient;

namespace CSharp直接连接MySQL

{

class Program

{

static void Main(string[] args)

{

//Read();

//Insert();

//Update();

//Delete();

//ExcuteSccalar();//查询一个值更方便

Console.WriteLine(VerifyUser("w", "2"));//验证库中是否有此数据

Console.ReadKey();

}

static bool VerifyUser(string username, string password)

{

string connectStr = "server=127.0.0.1;port=3306;database=zxw;user=root;password=123456";//本机配置

MySqlConnection conn = new MySqlConnection(connectStr);

try

{

conn.Open();//开启数据库

Console.WriteLine("已连接。。");

//string sql = "select * from users where username='" + username + "' and password='" + password + "'";//按照自定义查询规则组拼sql

string sql = "select * from users where [email protected] and [email protected]";//按照自定义查询规则组拼sql @符号等于临时定义一个数据

MySqlCommand cmd = new MySqlCommand(sql, conn);

cmd.Parameters.AddWithValue("username", username);[email protected]

cmd.Parameters.AddWithValue("password", password);[email protected]

MySqlDataReader reader = cmd.ExecuteReader();//查询

if (reader.Read()) return true;

else return false;

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

conn.Close();//关闭连接

return false;

}

static void Read()

{

string connectStr = "server=127.0.0.1;port=3306;database=zxw;user=root;password=123456";//本机配置

MySqlConnection conn = new MySqlConnection(connectStr);

try

{

conn.Open();//开启数据库

Console.WriteLine("已连接。。");

string sql = "select * from users";

MySqlCommand cmd = new MySqlCommand(sql, conn);

MySqlDataReader reader = cmd.ExecuteReader();//查询

while (reader.Read())//Read()返回值bool

{

//Console.WriteLine(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString());//通过read数组取值

//Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetInt16(2));//通过get方法读取

Console.WriteLine(reader.GetString("username") + " " + reader.GetString("password") + " " + reader.GetInt16("id"));//通过列名索引读取

}

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

conn.Close();//关闭连接

Console.ReadKey();

}

static void Insert()

{

string connectStr = "server=127.0.0.1;port=3306;database=zxw;user=root;password=123456";//本机配置

MySqlConnection conn = new MySqlConnection(connectStr);

try

{

conn.Open();//开启数据库

Console.WriteLine("已连接。。");

string sql = "insert into users(username,password) values('" + DateTime.Now + "','zxw')";//当前时间

MySqlCommand cmd = new MySqlCommand(sql, conn);

int result = cmd.ExecuteNonQuery();//返回值受数据库中数据的行数影响

Console.WriteLine("插入完成");

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

conn.Close();//关闭连接

Console.ReadKey();

}

static void Update()

{

string connectStr = "server=127.0.0.1;port=3306;database=zxw;user=root;password=123456";//本机配置

MySqlConnection conn = new MySqlConnection(connectStr);

try

{

conn.Open();//开启数据库

Console.WriteLine("已连接。。");

string sql = "update users set username='oo',password='oo' where id=1";

MySqlCommand cmd = new MySqlCommand(sql, conn);

int result = cmd.ExecuteNonQuery();//返回值受数据库中数据的行数影响

Console.WriteLine("更新完成");

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

conn.Close();//关闭连接

Console.ReadKey();

}

static void Delete()

{

string connectStr = "server=127.0.0.1;port=3306;database=zxw;user=root;password=123456";//本机配置

MySqlConnection conn = new MySqlConnection(connectStr);

try

{

conn.Open();//开启数据库

Console.WriteLine("已连接。。");

string sql = "delete from users where id=1";

MySqlCommand cmd = new MySqlCommand(sql, conn);

int result = cmd.ExecuteNonQuery();//返回值受数据库中数据的行数影响

Console.WriteLine("删除完成");

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

conn.Close();//关闭连接

Console.ReadKey();

}

static void ExcuteSccalar()

{

string connectStr = "server=127.0.0.1;port=3306;database=zxw;user=root;password=123456";//本机配置

MySqlConnection conn = new MySqlConnection(connectStr);

try

{

conn.Open();//开启数据库

Console.WriteLine("已连接。。");

string sql = "select count(*) from users";

MySqlCommand cmd = new MySqlCommand(sql, conn);

object o = cmd.ExecuteScalar();//返回值即为查询结果

int count = Convert.ToInt32(o.ToString());

Console.WriteLine(count);

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

conn.Close();//关闭连接

Console.ReadKey();

}

}

}

private SqlConnection con; //数据库连接、关闭、释放资源 #region 数据库连接、关闭、释放资源 private void Open() { if (con == null) { //根据Web.Config文件中数据库连接串,建立数据连接 con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString ()); } try { //判断连接对象状态,如果是关闭,将其打开 if (con.State == System.Data.ConnectionState.Closed) con.Open(); } catch (System.Data.SqlClient.SqlException E) { //如果出现错误,关闭数据连接,并抛出错误信息 this.Close(); throw new Exception(E.Message); } } public void Close() { if (con != null) { con.Close();//关闭连接 } } public void Dispose() { if (con != null) { con.Dispose();//释放连接资源 con = null; } } #endregion #region 参数处理 public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) //参数处理 { SqlParameter param; if (Size > 0) { param = new SqlParameter(ParamName, DbType, Size); } else { param = new SqlParameter(ParamName, DbType); } param.Direction = ParameterDirection.Input; if (Value != null) { param.Value = Value; } return param; } #endregion #region 将参数添加到SqlCommand当中 private SqlCommand CreateCommand(string procName, SqlParameter[] prams) //将参数添加到SqlCommand当中 { this.Open();//确认打开连接 SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.Text; //执行类型是命令文本。上面的参数procName即为SQL语句 //cmd.CommandType = CommandType.StoredProcedure; //执行类型是存储过程。上面的参数procName即为存储过程名称 //下面依次把参数传入命令文本 if (prams != null) { foreach (SqlParameter parameter in prams) cmd.Parameters.Add(parameter); } return cmd; } #endregion #region 将参数添加到SqlDataAdapter当中 private SqlDataAdapter CreateDataAdapter(string procName, SqlParameter[] prams) //将参数添加到SqlDataAdapter当中 { this.Open(); SqlDataAdapter dap = new SqlDataAdapter(procName, con); dap.SelectCommand.CommandType = CommandType.Text;//执行类型:命令文本 //dap.SelectCommand.CommandType = CommandType.StoredProcedure;//执行类型:存储过程 if (prams != null) { foreach (SqlParameter parameter in prams) { dap.SelectCommand.Parameters.Add(parameter); } } return dap; } #endregion #region 针对查询(带参) public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName) //针对查询(带参) { SqlDataAdapter dap = CreateDataAdapter(procName, prams); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对查询(不带参) public DataSet RunProcReturn(string procName, string tbName) //针对查询(不带参) { SqlDataAdapter dap = CreateDataAdapter(procName, null); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对增删改 public int RunProc(string procName, SqlParameter[] prams) //针对增删改 { SqlCommand cmd = CreateCommand(procName, prams); int i = cmd.ExecuteNonQuery(); this.Close(); return i; } #endregion }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值