using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; //引用命名空间 namespace Ado.net数据库增删改查 { class Program { static void Main(string[] args) { //1、数据库连接 SqlConnection conn = new SqlConnection("server=.;database=ADO;user=sa;pwd=ray;"); //2、数据库操作 SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from Student"; //3、数据库连接通道开启 conn.Open(); //4、数据库执行 SqlDataReader drc = cmd.ExecuteReader(); if (drc.HasRows) { while (drc.Read()) { string gender = ((bool)drc[2]) ? "男" : "女"; int age=DateTime.Now.Year-Convert.ToDateTime(drc[3]).Year; string birthday=Convert.ToDateTime(drc[3]).ToString("yyyy年MM月dd日"); string score=Convert.ToDecimal(drc[4]).ToString("#.##"); Console.WriteLine ("学号:{0},姓名:{1},性别:{2},年龄:{3},生日:{4},分数:{5}" ,drc[0],drc[1],gender,age,birthday,score); } } //5、数据库连接通道关闭 conn.Close(); Console.ReadKey(); } } }
时间: 2024-12-28 21:36:07