.net 調(diào)用sqlserver視圖

數(shù)據(jù)庫代碼:
--表和數(shù)據(jù)
create table studentTable(
??id int identity(1,1)primary key,
??name varchar(20),
??gender char(2),
??age int,
)
insert into studentTable (name,gender,age)
values
??('劉備','男',28),
??('張飛','男',24),
??('關(guān)羽','男',26);?
???
???
--創(chuàng)建視圖 student_view
if (exists (select * from sys.objects where name = 'student_view'))
??drop view student_view
go
--student_view()不使用參數(shù),默認(rèn)為基礎(chǔ)表中的列名稱
create view student_view?
as
select name,age from studentTable where age>24;
?
?
?
--創(chuàng)建視圖 stu_insert_view
create view stu_insert_view(編號,姓名,性別,年齡)
as
select id,name,gender,age from studentTable;
go
?
??
--查看student_view視圖數(shù)據(jù)?
select * from student_view;
--查看表數(shù)據(jù)
select * from studentTable;
使用視圖修改基本表數(shù)據(jù)
(1).向視圖插入數(shù)據(jù),基本表也會同步插入數(shù)據(jù)。
create view stu_insert_view(編號,姓名,性別,年齡)
as
select id,name,gender,age from studentTable;
go
select * from studentTable;
---插入一條數(shù)據(jù)
insert into stu_insert_view values('曹操','男',40);
----查看插入記錄之后表中的內(nèi)容。
select * from studentTable;
?
(2).修改視圖數(shù)據(jù),基本表也會同步修改數(shù)據(jù)。
--查看修改之前的數(shù)據(jù)
select * from studentTable;
--修改數(shù)據(jù)
update student_view set age=30
where name='劉備';
--查看修改后的數(shù)據(jù)
select * from studentTable;
?
(3).刪除視圖數(shù)據(jù)。
--語法
delete view_name where condition;
--例子
delete student_view where name ='張飛';
select * from student_view;
select * from studentTable;
后端代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace ConsoleApp1
{
??class Program
??{
????private static string GetConnectionStringsConfig(string connectionName)
????{
??????string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();
??????Console.WriteLine(connectionString);
??????return connectionString;
????}
????static void Main(string[] args)
????{
??????string source = GetConnectionStringsConfig("Northwind");
??????try
??????{
????????#region 測試3:.net調(diào)用視圖
????????using (SqlConnection cn = new SqlConnection(source))
????????{
??????????cn.Open();
??????????SqlCommand cmd = new SqlCommand();
??????????cmd.Connection = cn;
??????????//調(diào)用視圖,和表的語法是一樣的。
??????????cmd.CommandText = @"select * from student_view;";
??????????//4.2.使用視圖修改基本表數(shù)據(jù)
??????????//?(1).向視圖插入數(shù)據(jù),基本表也會同步插入數(shù)據(jù)。
??????????cmd.CommandText = @"insert into stu_insert_view values('曹操','男',40);";
??????????//?(2).修改視圖數(shù)據(jù),基本表也會同步修改數(shù)據(jù)。
??????????cmd.CommandText = @"update student_view set age=30 where name='劉備';";
??????????//?(3).刪除視圖數(shù)據(jù)。
??????????cmd.CommandText = @"delete student_view where name ='張飛';";
??????????DataTable dt = new DataTable();
??????????dt.Load(cmd.ExecuteReader());
??????????Console.WriteLine(dt);?
????????}
????????#endregion
??????}
??????catch (SqlException ex)
??????{
????????Console.WriteLine(ex.Message);
??????}
??????Console.ReadKey();
????}
??}
}?
視圖的優(yōu)點和作用
與直接從表中讀取數(shù)據(jù)相比,視圖具有以下優(yōu)點:
3.1.簡單化
看到的就是需要的,視圖不僅可以簡化用戶對數(shù)據(jù)的理解,也可以簡化對它們的操作,那些經(jīng)常使用的查詢可以被定義為視圖,從而使得用戶不必為以后的每次操作指定全部的條件。
3.2.安全性
視圖可以作為一種安全機(jī)制。通過視圖用戶只能查看和修改他們所能看到的數(shù)據(jù)。其它或表既不可見也不可以訪問。如果某一用戶想要訪問視圖的,必須授予其訪問權(quán)限。視圖所引用表的訪問權(quán)限與視圖權(quán)限的設(shè)置互不影響。
3.3.邏輯數(shù)據(jù)獨立性
視圖可以幫助用戶屏蔽真實表結(jié)構(gòu)變化帶來的影響。