• C#对于sql server数据库的简单操作


    1.在用windows模式登陆sql server 数据库 简历一个student的数据库,然后新建查询:

    create table student
    (
     id     int  auto_increment  primary key,
     name char(10) not null,
     sex    char(10) not null,
     age   char(10) not null,   
    )

    2.在vs中新建一个项目,输入一下代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string connSting;
                connSting = "server=localhost;database=student;Integrated Security=True ";
                SqlConnection sConn = new SqlConnection(connSting);
                try
                {
                    sConn.Open();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("链接错误:" + ex.Message);
                }
                string sqlMessage=null;
                sqlMessage = "select * from student";
                SqlCommand sCmd = new SqlCommand(sqlMessage, sConn);
    
                SqlDataReader sdr = null;
                try
                {
                    sdr = sCmd.ExecuteReader();
                
                    Console.WriteLine(" 姓名   性别   年龄 ");
                    while (sdr.Read())
                    {
                        Console.WriteLine(sdr["name"] +""+ sdr["sex"]+"" + sdr["age"]);
                    }
                    sConn.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
        }
    }

    3.运行结果将会显示数据库中的数据

  • 相关阅读:
    JavaWeb_Tomcat_Maven!
    java异常!
    JavaWeb获取web.xml初始参数!getInitParameter
    JavaWeb文件下载!
    Java抽象接口!
    JavaWeb初识Servlet!
    关于HashMap以对象作为Key的实现及踩坑
    浅析.Net下的多线程编程(2)
    获取浏览器分辨率
    浅析.Net下的多线程编程(1)
  • 原文地址:https://www.cnblogs.com/izxcheng/p/3692049.html
Copyright © 2020-2023  润新知