ADO.NET連線資料庫

iDotNetSpace發表於2009-02-06

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace db_test
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection userConnection = new SqlConnection("server=ZLD-PC\\SQLEXPRESS;user id=sa;initial catalog=zld119;password=123");
            try{userConnection.Open();}

            catch{MessageBox.show("開啟失敗");}
            SqlCommand userCommand = new SqlCommand("getUserInfo",userConnection);//getUserInfo為自已寫的儲存過程
            SqlDataReader userDataReader = userCommand.ExecuteReader();
            while (userDataReader.Read())
            {
                Console.WriteLine(userDataReader["Sname"].ToString());
            }
            userDataReader.Close();
            userConnection.Close();
        }
    }
}

 

 

附:getUserInfo儲存過程

create procedure getUserInfo

as
 select Sname
 from student
 where Sdept='cs'

GO

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-545469/,如需轉載,請註明出處,否則將追究法律責任。

相關文章