ADO.NET(資料訪問技術)

來源:互聯網
上載者:User

標籤:

一、ADO.NET

就是將c#和sql串連起來的一個紐帶

可以通過ADO.NET將記憶體中的臨時資料寫入到資料庫中也可以將資料庫中的資料提取到記憶體中供程式調用

二、串連資料庫的基本格式

需要兩個類

1、資料庫連接類  SqlConnection

2、資料庫操作類  SqlCommand

例:

在sql中建立資料庫

create database DATA0720
go
use DATA0720
go
create table xinxi
(
code nvarchar(200) primary key,
[password] nvarchar(200),
name nvarchar(200),
birthday datetime,
sex bit,
nation varchar(200),
ncode nvarchar(200)
)
go
insert into xinxi values(‘zhaoliu‘,‘xtq0313‘,‘趙六‘,1992-03-13,1,‘漢族‘,‘N001‘)
insert into xinxi values(‘zhangsan‘,‘xtq0313‘,‘張三‘,1991-05-11,0,‘回族‘,‘N002‘)
insert into xinxi values(‘lisi‘,‘xtq0313‘,‘李四‘,1995-08-08,1,‘滿足‘,‘N003‘)
insert into xinxi values(‘wangwu‘,‘xtq0313‘,‘王五‘,1994-11-25,0,‘彝族‘,‘N004‘)
select *from xinxi

create table Nxinxi
(
ncd varchar(200)primary key,
nplace varchar(200)
)
insert into Nxinxi values(‘N001‘,‘中國‘)
insert into Nxinxi values(‘N002‘,‘四川‘)
insert into Nxinxi values(‘N003‘,‘東北‘)
insert into Nxinxi values(‘N004‘,‘廣西‘)
select *from Nxinxi

在c#中建立關係

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;    //必須引用此命名空間
namespace ADO.NET基礎及增刪
{
class Program
{
static void Main(string[] args)
{
Console.Write("請輸入要刪除的姓名:");
string s = Console.ReadLine();
//1、資料庫的串連

//寫連接字串,立馬就要想到有4點要寫完,1、串連哪台伺服器,2、串連哪個資料庫,3、串連使用者名稱,4、密碼
string sql=("server=.;database=DATA0720;user=sa;pwd=123");

//執行個體化資料連線類,將連接字串寫到建構函式中,讓這個類構造完畢後就串連到指定的伺服器和資料庫上
SqlConnection conn = new SqlConnection(sql);

//2、設定的操作

//通過已經串連的資料庫建立一個對此庫的操作類
SqlCommand cmd = conn.CreateCommand();

//編寫TSQL語句
cmd.CommandText=("delete from xinxi where name=‘"+s+"‘");

//3、執行操作並關閉串連
conn.Open();    //資料庫連接開啟
cmd.ExecuteNonQuery();    //資料庫操作執行
conn.Close();    //資料庫連接關閉

//提示
Console.WriteLine("您已成功刪除姓名為"+s+"的人。");

Console.ReadKey();

 

這樣就可以在c#中刪除sql中的資料了。

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.