.NET连接SQL Server

 

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

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

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

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

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main_(string[] args)
        {
            String sql = "select * from emp";
            string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=tt;User ID=sa;Password=tiger";
          
            SqlConnection conn = new SqlConnection(connString);
           
            SqlCommand comm = new SqlCommand(sql, conn);
          
            conn.Open();
        
            SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);

           
            while (reader.Read())
            {
                Console.WriteLine("id-----" + reader.GetInt32(0));
                Console.WriteLine("name--------" + reader.GetString(2));
                Console.WriteLine("age-------" + reader.GetInt32(1));
 
            }

            Console.ReadKey();
            reader.Close();

        }

 

}

}

posted @ 2010-01-25 20:50  释放  阅读(574)  评论(0)    收藏  举报