zoukankan      html  css  js  c++  java
  • c#读取MySQL数据表中的内容

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    //引入MySQL
    using MySql.Data.MySqlClient;
    using System.Data.SqlClient;
    using System.Data.OleDb;
    
    namespace c_01_连接查询MySQL数据库
    {
        public partial class frmQuery : Form
        {
            public frmQuery()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MySqlConnection sqlCnn = new MySqlConnection();
                sqlCnn.ConnectionString = "server = '127.0.0.1'; uid = 'root'; pwd = 'root'; database = 'ajax_stydy';";//连接字符串
                MySqlCommand sqlCmd = new MySqlCommand();
                sqlCmd.Connection = sqlCnn;
                sqlCmd.CommandText = "select * from user";
                try
                {
                    sqlCnn.Open();
                    MySqlDataReader rec = sqlCmd.ExecuteReader();
                    textBox2.AppendText("数据库连接成功!
    ");
                    textBox1.AppendText(string.Format("ID	 用户名	 密码	
    "));
                    while (rec.Read())
                    {
                        textBox1.AppendText(string.Format("{0}	 {1}	 {2}	
    ", rec.GetInt32(0), rec.GetString(1), rec.GetString(2)));   
                    }
                    textBox2.AppendText("读取数据完成!
    ");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error");
                }
                finally
                {
                    textBox2.AppendText("数据库连接关闭!");
                    sqlCnn.Close();
                }
            }
        }
    }
    

     数据库字段:

  • 相关阅读:
    浮动float 摆放位置
    边框(border)宽度样式颜色 和基本属性
    调用css文件,进行调色
    deque_01
    iterator_教程中的讲解
    vector_01
    VS2013_CodeLens
    Qt for Embedded Linux
    jsjl_for_ubuntu12.04
    VC6_导入lib库
  • 原文地址:https://www.cnblogs.com/soulsjie/p/7976515.html
Copyright © 2011-2022 走看看