mysql数据库学习(1)

来源:互联网 发布:2016淘宝日刷千单 编辑:程序博客网 时间:2024/05/22 00:32
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using MySql.Data.MySqlClient;namespace CSharpContentMySql{    class Program    {        static void Main(string[] args)        {            string contentStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=123456;";            MySqlConnection con = new MySqlConnection(contentStr);//数据库链接对象            try            {                con.Open();//开始链接                string sql = "select * from users";//sql语句                MySqlCommand comd = new MySqlCommand(sql, con);//数据库命令行对象                /*                 * 命令行sql语句介绍                comd.ExecuteReader();//命令行查询语句执行                comd.ExecuteNonQuery();//命令行插入,删除语句执行                comd.ExecuteScal                ar();//命令行执行查询语句,返回查询结果的单一数值                 */                MySqlDataReader reader= comd.ExecuteReader();//返回data数据读取流                /*                 * 信息读取测试                reader.Read();//从表的第一行开始,仅读取当前行的数据信息,每多执行一次该方法,读取的行数信息加1,返回值为判断当前行信息是否为空的Bool值。                Console.WriteLine(reader[0].ToString()+reader[1].ToString());                */                while (reader.Read()) {                    Console.WriteLine("id:"+reader[0].ToString()+" username:" + reader[1].ToString()+" password:"+reader[2].ToString());                }                Console.Write("已进行链接");            }            catch (Exception e) {                Console.WriteLine(e.ToString());            }            con.Close();            Console.ReadKey();        }    }}


原创粉丝点击