ADO.NET查看

來源:互聯網
上載者:User

標籤:--   generic   pwd   arch   static   and   執行   ace   中括弧   

資料庫:

create database Date0504go use Date0504gocreate table Users(    Ids int primary key identity(1,1),    --設定一個int類型的主鍵從1開始一次遞增--    Ucode nvarchar(200),    UserName nvarchar(200),    [passWord] nvarchar(200),     --關鍵詞要用中括弧括起來--    NickName nvarchar(200),    Sex bit,    Birthday datetime,    Nation nvarchar(200),)insert into Users values(‘U001‘,‘zhangsan‘,‘123‘,‘張三‘,1,‘2001-1-1‘,‘N001‘)insert into Users values(‘U002‘,‘lisi‘,‘123‘,‘李四‘,1,‘2002-2-2‘,‘N002‘)insert into Users values(‘U003‘,‘wagnwu‘,‘123‘,‘王五‘,1,‘2003-3-3‘,‘N003‘)insert into Users values(‘U0014‘,‘xiaohua‘,‘123‘,‘小花‘,1,‘2004-4-4‘,‘N004‘)create table UserNation(    NationCode nvarchar(200) primary key,    NationName nvarchar(200))insert into UserNation values(‘N001‘,‘漢族‘)insert into UserNation values(‘N002‘,‘滿族‘)insert into UserNation values(‘N003‘,‘藏族‘)insert into UserNation values(‘N004‘,‘彝族‘)select * from UserNationselect *from Users

vs:串連資料庫查看內容

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;//1、第一步引用此命名空間namespace ado.net{    class Program    {        static void Main(string[] args)        {            //編寫連接字串      一定要寫仔細不能寫錯    一個字串真能串連一個表            string sql = "server =.;database=Date0504;user=sa;pwd=y920106k";//伺服器名稱 ;串連資料庫(只能練一個資料庫但能操作所有的表);使用者名稱 ;密碼                       //建立資料庫連接對象            SqlConnection conn = new SqlConnection(sql);    // SqlConnection  串連                       //建立資料庫操作對象            SqlCommand cmd = conn.CreateCommand();   //SqlCommand 命令    編寫命令             //執行操作            cmd.CommandText = "select * from users";    //查詢users表            conn.Open();                       //開啟資料庫連接最後別忘掉關掉串連            SqlDataReader dr = cmd.ExecuteReader();    //將語句執行,結果查詢到dr對象中                        Console.WriteLine("id     編號    使用者名稱    密碼    暱稱    性別     生日       民族");            dr.Read();   //指標往下走一行            while (dr.Read())//查看全部內容            {                Console.WriteLine(dr["Ids"] + " | " + dr["Ucode"] + " | " + dr["UserName"] + " | " + dr["PassWord"] + " | " + dr["NickName"] + " | " + (Convert.ToBoolean(dr["Sex"]) ? "男" : "女") + " | " + Convert.ToDateTime(dr["Birthday"]).ToString("yyyy年MM月dd日") + " | " + dr["Nation"]);            }            Console.ReadLine();            conn.Close();                     //關掉資料庫連接和conn。open對應            Console.ReadLine();        }    }}

 

ADO.NET查看

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.