c#+mysql+读取+数据库文件大小_C#从数据库mysql读取数据

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;namespace电子商务

{public partial classForm1 : Form

{publicForm1()

{

InitializeComponent();

}private void Form1_Load(objectsender, EventArgs e)

{

}private void button1_Click(objectsender, EventArgs e)

{string str = "Host=localhost;User ID=root;Password=;Port = 3306;DataBase=animals;Charset=utf8;";

MySqlConnection conn= newMySqlConnection(str);

conn.Open();// 拿到数据库并打开连接

//string sql = "select *from student";//MySqlCommand cmd = new MySqlCommand(sql, conn);//MySqlDataAdapter mda = new MySqlDataAdapter(cmd);//DataSet ds = new DataSet();//mda.Fill(ds);//dataGridView1.DataSource = ds.Tables[0];

MySqlDataReader dr = cmd.ExecuteReader();

//conn.Close();

/*1.访问数据库的五大步骤

2.拿到要发送的sql语句

3.执行 sql 语句

4.拿到执行后的聚过

5.关闭连接 切记关闭连接*/

1 拿到数据库的连接 并打开连接

//string url = "User ID=root;Password=;Host=localhost;Port=3306;Database=xx;charset=xx;";//MySqlConnection con = new MySqlConnection(url);//con.Open();

2.要拿到要发送的sql语句

//string sql = "select id from student where name=‘kobe‘";//MySqlCommand com = new MySqlCommand(sql, con);

3 执行 sql 语句

//MySqlDataReader reader = com.ExecuteReader();//reader.Read();//Read() 每次调用 都会从结果集中返回一行数据//reader.GetInt32(0);

/*1.访问数据库的五大步骤

2.拿到要发送的sql语句

3.执行 sql 语句

4.拿到执行后的聚过

5.关闭连接 切记关闭连接*/

//1 拿到数据库的连接 并打开连接

string url = "User ID=root;Password=;Host=localhost;Port=3306;Database=students;";

MySqlConnection con= newMySqlConnection(url);

con.Open();//2.要拿到要发送的sql语句

string sql = "select *from student where name=‘kobe‘";

MySqlCommand com= newMySqlCommand(sql, con);//3 执行 sql 语句

MySqlDataReader reader =com.ExecuteReader();

reader.Read();//Read() 每次调用 都会从结果集中返回一行数据//reader.GetInt32(0);

Console.WriteLine(reader.GetInt32(0));

con.Close();对数据库进行 增 删 改 查1 拿到数据库的连接 并打开连接

//string url = "User ID=root;Password=root;Host=localhost;Port=3306;Database=students;";//MySqlConnection con = new MySqlConnection(url);//con.Open();//string sql = "insert into student values(11,‘hahahaha‘,‘man‘,6)";//添加信息//MySqlCommand cmd = new MySqlCommand(sql, con);//int result = cmd.ExecuteNonQuery();//Console.WriteLine("数据库打开结果:"+result);//con.Close();//FindStudent("kobe");

}public static bool FindStudent(stringname)

{bool b = false;string url = "User ID=root;Password=root;Host=localhost;Port=3306;Database=students;";

MySqlConnection con= newMySqlConnection(url);

con.Open();string sql = "select *from studnet where name=‘{0}‘";

sql= string.Format(sql, name);//Console.WriteLine(sql);

MySqlCommand cmd = newMySqlCommand(sql, con);

MySqlDataReader reader=cmd.ExecuteReader();

b=reader.Read();returnb;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值