數據庫鏈接

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;  //引用命名空間

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string yu = @"Data Source=PC-20120701OAHI\SQLEXPRESS;Initial Catalog=mydb1;User ID=sa;password=shuyu+ast;";  //鏈接數據庫語句
            SqlConnection wei=new SqlConnection(yu);
            wei.Open();
            Console.WriteLine("打開");
            Console.ReadKey();

            string num1 = "select top(1) * from t_person1";
            SqlCommand Person = new SqlCommand(num1, wei);
            SqlDataReader count = Person.ExecuteReader();
            while (count.Read())  //判斷返回的內容是否有值
            {
                Console.WriteLine(string.Format("編號:{0},姓名:{1},年齡:{2},日期:{3}", count[0], count[1], count[2], count[3]));
            }
            Console.ReadKey();
        }
    }
}

發佈了32 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章