set names utf8;
use test;
CREATE TABLE IF NOT EXISTS wtc_student(
id INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
profession VARCHAR(45) DEFAULT NULL,
sex VARCHAR(3) NOT NULL,
birthdate DATE,
PRIMARY KEY ( id )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO wtc_student(id, name, profession, sex, birthdate) VALUES (1, '陈', '老师', '男', '1980-01-05');
INSERT INTO wtc_student(id, name, profession, sex, birthdate) VALUES (2, '陈', '学生', '男', '1981-02-06');
INSERT INTO wtc_student(id, name, profession, sex, birthdate) VALUES (3, '刘', '学生', '男', '1982-03-07');
INSERT INTO wtc_student(id, name, profession, sex, birthdate) VALUES (4, '黄', '学生', '男', '1983-04-08');
INSERT INTO wtc_student(id, name, profession, sex, birthdate) VALUES (5, '朱', '学生', '男', '2083-04-08');
INSERT INTO wtc_student(id, name, profession, sex, birthdate) VALUES (6, '吴', '学生', '男', '2083-04-08');
using MySql.Data.MySqlClient;//调用MySQL动态库
string connString = "server=localhost;database=test;uid=root;pwd=123456";
MySqlConnection conn = new MySqlConnection(connString);
try
{
conn.Open();//开启连接
Console.WriteLine("连接成功!", "测试结果");
try
{
MySqlCommand mycmd = new MySqlCommand("select id,name,profession,sex,birthdate from wtc_student", conn);
mycmd.Connection = conn;
var myData = mycmd.ExecuteReader();
while (myData.Read())
{
Console.WriteLine("{0} {1} {2} {3} {4}", myData["id"], myData["name"], myData["profession"], myData["sex"], myData["birthdate"]);
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("数据库连接异常!");
}
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//关闭连接
conn.Close();
}
Console.ReadLine();