.NET访问SqlServer

最最基础的东西,长时间不写也会忘,一但要用的时候,回忆就是浪费时间!


在“数据”菜单上选择“添加新数据源”。

在“选择数据源类型”页面上选择“数据库 ”。

选择“新建连接”以创建新的数据连接。

在“选择数据源”对话框中,选择“Microsoft SQL Server 数据库文件”。单击“确定”。  


using System;
Wo#`C1j�vb _fA0 using System.Collections.Generic;ITPUB个人空间 |w@ ?5U{T0o
using System.Linq;
`cgt$mN }j ~?0 using System.Text;ITPUB个人空间)G^1V8Db{
using System.Data;ITPUB个人空间K:PN{h@ S
using System.Data.SqlClient;

namespace ConsoleApplication1
Q WuIL8~0 {
9fo0@R-QV G\h\0     class Program
ZM3`"C+U/Y{;Zz0     {
PIU%G7_&}?(^0         static void Main_(string[] args)ITPUB个人空间@ k�m0Ih,AQ
        {ITPUB个人空间(O {Mn(Z7h
            String sql = "select * from emp";   //sql查询语句 ITPUB个人空间1p_.]I7S;N3`Z it)[
            string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=tt;User ID=sa;Password=tiger"; //Sql数据库联接字符串
A5GY-dE'b g;|+K&s,H5A0           ITPUB个人空间kF8cR^ p^1f
            SqlConnection conn = new SqlConnection(connString);  //数据库联接对象 ITPUB个人空间}~ p` @1fl
           ITPUB个人空间3B3[ UEo!ex
            SqlCommand comm = new SqlCommand(sql, conn);     //命令对象 ITPUB个人空间9s ruz!oCQ} y"L1_
          ITPUB个人空间$GR4m_lP @`!Xe
            conn.Open();ITPUB个人空间x)r1Gj r.l
        ITPUB个人空间.N'sL`v:P
            SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);    //SqlDataReader对象,单步执行,速度快

           ITPUB个人空间"~5Z\ {q*U'mFG
            while (reader.Read())ITPUB个人空间1`/y7o,s2c&TFm
            {ITPUB个人空间#@B @$tGl*B/|
                Console.WriteLine("id-----" + reader.GetInt32(0));ITPUB个人空间`q g?r5W
                Console.WriteLine("name--------" + reader.GetString(2));
vn g5`/y0                 Console.WriteLine("age-------" + reader.GetInt32(1));
#H[D3nA Z0  
9UC1vDYx(G0             }

            Console.ReadKey();ITPUB个人空间b5R5t*Gi4k&|@2l
            reader.Close();

        }

}

}

posted @ 2011-01-18 18:25  稽首本然  阅读(2627)  评论(0编辑  收藏  举报