資料庫的基本操作及資料庫與c 的連線

2021-06-27 22:54:21 字數 2044 閱讀 7487

1、資料庫的刪除

1)刪除一條資料 delete from users where name='王五'

2)刪除資料庫 drop database aa

3)刪除表 (區別第二種刪除資料不可恢復)

drop table score \truncate table users

2、資料庫的查詢

1)查詢所有資料

select * from 表名

如:select * from users

2)查詢部分資料

分為兩種,一種為「=」,查詢等於欄位的所有資料;另一種為「<>」,查詢不等於欄位的所有資料

select id,name from users where id=103

select id,name from users where id<>103

3)合併查詢(合併兩表中相同的字段,如果欄位不

相同則,相同字段進行合併)

select * from users union select * from score

select  id from users union select id from score

4)中文命名列(兩種表現形式)

select id as 編號,name as 姓名 from users

select 編號= id ,姓名=name  from users

5)查詢空行和非空行

select id,name from users where age is null

select id,name from users where name is  not null

6)限制條數 (一種是按前幾個查詢,一種是按百分比進行查詢)

select top 3 id,name from users

select top 37 percent id,name from users

7)排序(分為公升序(asc)\降序(desc)\預設為公升序)

select id,name from users order by id

select id,name from users order by id asc

select id,name from users order by id desc

select id,name from users order by name desc

3、資料庫的連線

開始---->引入命名空間---->

建立乙個 sqlconnection 物件---->開啟連線

---->

建立乙個sqlcommand物件

---->

獲取sqldatareader物件

---->

關閉sqldatareader物件

---->

關閉連線---->

結 束

具體實現如下**:

using

system;

using

system.collections.generic;

using

system.linq;

using

system.text;

using

system.threading.tasks;

using

system.data;

using

system.data.sqlclient;

namespace

message

reader.close();

//關閉流

con.close();

//關閉資料庫 }

static

void

main(

string

args)  }

}

C 資料庫 資料庫基本操作

executenonquery方法 executescalar方法 executereader方法的區別 executenonquery 執行命令物件的sql語句,返回乙個int型別變數,如果sql語句是對資料庫的記錄進行操作 如記錄的增加 刪除和更新 那麼方法將返回操作所影響的記錄條數。execu...

資料庫 資料庫基本操作

操作練習 修改表結構 表資料的操作 實現 1 建立表 create table student stu no char 12 not null primary key,stu name varchar 20 not null gender tinyint 1 default1,age tinyint...

資料庫的基本操作

sql server 2000 是一種採用 t sql 語言的大型關係型資料庫管理系統。資料訓的資料按不同的形式組織在一起,構成了不同的資料物件。以二維表的形式組織在一起的資料就構成了資料庫的表物件,資料庫是資料庫物件的容器。資料庫物件沒有對應的磁碟檔案,整個資料庫對應磁碟上的檔案與檔案組。一 sq...