• DO.NET操作数据库


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

    namespace ConsoleApplication1
    {
    class Program1
    {
    static void Main1(string[] args)
    {
    //1.造一个连接字符串
    string connstring = "server=.;database=mydb;user=sa;pwd=123";
    //server指服务器:一般是IP地址,本机可以使用点;
    //database指数据库名称:要访问的数据库名称
    //user数据库的用户名:一般是sa
    //pwd数据库的密码:自己设置的

    //2.造一个连接对象(将程序和数据库之间搭建出一个通道)
    SqlConnection conn = new SqlConnection(connstring);

    //3.在此连接的基础上造一个命令对象
    SqlCommand cmd = conn.CreateCommand();

    //4.给命令对象一个SQL语句
    cmd.CommandText = "select top 1 * from Nation";

    //*****打开连接
    conn.Open();

    //5.执行SQL语句(命令)返回读取器对象
    SqlDataReader dr = cmd.ExecuteReader();

    //6.通过读取器来读取数据
    if (dr.HasRows)
    {
    dr.Read(); //读取数据的方法(读当前指针指向的一条数据,执行完该方法会将指针向下调一个)

    Console.WriteLine(dr[0]);
    Console.WriteLine(dr[1]);
    Console.ReadLine();
    }

    //******关闭连接
    conn.Close();
    }
    }
    }

    给一个条件,查询Info 表的数据

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

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("输入内容");
    string aa = Console.ReadLine();
    string connstring = "server=.;database=mydb;user=sa;pwd=123";
    SqlConnection conn = new SqlConnection(connstring);
    SqlCommand a = conn.CreateCommand();
    a.CommandText="select * from Info where name like'%"+aa+"%'";
    conn.Open();
    SqlDataReader b = a.ExecuteReader();
    if (b.HasRows)
    {
    while (b.Read())
    {
    Console.WriteLine(b[0] + " " + b[1] + " " + b[2] + " " + b[3] + " " + b[4]);
    }
    }
    else
    {
    Console.WriteLine("无");
    }
    Console.ReadLine();
    conn.Close();
    }
    }
    }

  • 相关阅读:
    STL中关于map和set的四个问题?
    PHP之Zip扩展,解压缩文件,ZipArchive类
    PHP之音乐ID3扩展
    关于PHP执行超时的问题
    PHP中GD库安装
    PHP之输出控制 ob_start(),ob_get_contents(),ob_end_clean()
    PHP之xdebug详解
    PHP上传文件详解
    php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证及缺点
    深入研究php://input与php://output
  • 原文地址:https://www.cnblogs.com/shi2172843/p/5774991.html
Copyright © 2020-2023  润新知