Mander-ZH01

DateGridView控件与mysql交互

private void dgv()
{
  //Ip+端口+数据库名+用户名+密码
  string connectStr = "server=127.0.0.1;port=3306;database=Test;user=root;password=123456;SslMode=none;";
  MySqlConnection conn = new MySqlConnection(connectStr);
  try
  {
    conn.Open();//跟数据库建立连接,并打开连接
    string sql = "select Id,UserId,UserName,UserBirth,UserConnect,UserOK,CreateOn,UpdateOn from CU_User";//MySql语句,查询列表内容
    MySqlCommand cmd = new MySqlCommand(sql, conn);
    MySqlDataReader reader = cmd.ExecuteReader();//执行一些查询
    //cmd.ExecuteScalar();//执行一些查询,返回一个单个的值
    //读取第一次Read(),ke输出读取第一列数据,如果再Read()一次,可输出读取第二列数据,但是只能读取第二列数据
    //reader.Read();//读取一列数据如果读取(有数据)成功,返回True,如果没有(数据),读取失败的话返回false
    while (reader.Read())//使用while循环可读取所有user列表里的数据
    {
      // Console.WriteLine(reader.GetInt32("id") + " " + reader.GetString("username") + " " + reader.GetString("password"));
      dataGridView.Rows.Add(
      reader.GetString("Id")
      ,reader.GetString("UserId")
      ,reader.GetString("UserName")
      , reader.GetString("UserBirth")
      ,reader.GetString("UserConnect")
      , reader.GetString("UserOK")
      , reader.GetString("CreateOn")
      , reader.GetString("UpdateOn")
        );
    }
  }
  finally
  {
    conn.Clone();
  }
}

分类:

技术点:

相关文章:

  • 2022-01-07
  • 2022-12-23
  • 2022-01-10
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-07-10
猜你喜欢
  • 2021-06-24
  • 2021-10-29
  • 2021-06-03
  • 2022-12-23
相关资源
相似解决方案