ITPub博客

首页 > Linux操作系统 > Linux操作系统 > .NET连接SQL Server

.NET连接SQL Server

原创 Linux操作系统 作者:iDotNetSpace 时间:2010-01-26 11:14:07 0 删除 编辑

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

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

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

在“选择数据源”对话框中,选择“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();

        }

 

}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-626019/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5765978