zoukankan      html  css  js  c++  java
  • SqlDataReader的使用

    1.建立数据库连接;

    2.设置数据库指令;

    3.数据拾取器接收输出的数据;

    4.遍历打印数据;

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Data;
     4 using System.Data.SqlClient;
     5 using System.Linq;
     6 using System.Text;
     7 
     8 namespace test2
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             SqlConnection conn = new SqlConnection();
    15             conn.ConnectionString = "Data Source=HZ-PC;Initial Catalog=mydb;Persist Security Info=True;User ID=sa;Password=123";//设置连接属性
    16             conn.Open();//连接数据库
    17             SqlCommand cmd = conn.CreateCommand();//从所建立的数据库连接中新建数据库命令变量
    18             cmd.CommandText = "select * from db_line";   //赋值
    19             SqlDataReader dr = cmd.ExecuteReader();//建立数据拾取器,接收数据
    20             Console.WriteLine("ID     姓名    编号    票");
    21             while(dr.Read())//循环依次打印数据
    22                 Console.WriteLine("{0}    {1}    {2}    {3} ",dr[0],dr[1],dr[2],dr[3]);
    23             dr.Close();
    24             conn.Close();
    25         }
    26     }
    27 }
  • 相关阅读:
    phpdocumentor生成代码注释文档(linux)
    phpstorm扩展
    es教程
    康威定律
    k8s
    tidb调研
    netty 在线教程
    McQueenRPC源码阅读
    DIY一些基于netty的开源框架
    性能测试
  • 原文地址:https://www.cnblogs.com/Failbs/p/4099237.html
Copyright © 2011-2022 走看看