C# 数据库操作

using System;using System.Text;using System.Data.SqlClient;namespace App{class MyClass{public static void Main(string[] args){string str=Data Source=.;Initial Catalog=mlh;Integrated Security=True;us...

using System;
using System.Text;
using System.Data.SqlClient;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            string str="Data Source=.;Initial Catalog=mlh;Integrated Security=True";
            using ( SqlConnection con = new SqlConnection(str)) {
                con.Open();
                string sql = "select * from student";
                using(SqlCommand cmd = new SqlCommand(sql,con)){
                    Console.WriteLine(cmd.ExecuteNonQuery());
                    Console.WriteLine(cmd.CommandText);
                    //Console.WriteLine(cmd.ExecuteReader());
                    SqlDataReader reader = cmd.ExecuteReader();
                    while(reader.Read()){
                        Console.WriteLine(reader["id"]);//读取id
                        Console.WriteLine(reader["name"]);//读取name字段
                    }
                }
            }
        }
    }
}

 

本文标题为:C# 数据库操作

基础教程推荐