C#中SQL Server学习

1、数据库视图学习
    视图其实就是一个表查询,一般不存实际的表,每次使用的时候执行一次查询;
    视图也可以增删改查,修改对应数据,对应表中数据同样会被修改;
    视图创建语句:
use StudentManagerDB
go
if(exists (select * from sys.objects where name='VTest'))
drop view VTest
go

--创建视图
create view VTest
as
select TOP (100) PERCENT A.StudentId,A.StudentName,A.ClassId,B.ClassName,C.CSharp,C.SQLServer 
from Students as A
inner join StudentClass as B on A.ClassId=B.ClassId
left join ScoreList as C on A.StudentId=C.StudentId
order by A.StudentId
go
    TOP (100) PERCENT:因为SQL规则规定,如果子查询里,有order,就必须有TOP,所以就用SELECT TOP (100) percent来限定,top 100 percent是为了保证筛选出所有符合条件的数据条目(百分百)。
    sys.objests存储的是本数据库中的信息,不仅存储表名,还有存储过程名称、视图名、触发器等;
 
2、数据库存储过程学习
        使用存储过程的优点:
        (1)降低网络传输数据:执行语句预先定义为了存储过程,代码在调用时仅需传递存储过程名和参数,无需传递整条语句,降低了网络负载;
        (2)执行效率高:SQL Server只需要在存储过程第一次执行的时候编译成可执行的二进制代码,再次使用不需要重新编译;
        (3)封装性:将实现某种功能的多条SQL语句封装到一个对象中,可多次重复调用,可移植性强;
        (4)安全:如果所有用户都使用存储过程来访问数据,就可以禁止用户对表的直接访问,并控制所有对数据的访问;
3、创建存储过程 、执行存储过程
[1]创建不带输入、输出的存储过程
use StudentManagerDB
go
if(exists(select * from  sys.objects  where name='pro_testNo'))
drop procedure pro_testNo
go
create procedure pro_testNo
as
begin
select * from Students 
end
go
--执行存储过程
exec dbo.pro_testNo

[2]创建带输入、不带输出参数的存储过程

use StudentManagerDB
go
if(exists(select * from sys.objects where name='pro_Students'))
drop proc pro_Students
go
create procedure pro_Students
    @StudentId int
as
begin
    select * from Students where StudentId=@StudentId
end
go
--执行存储过程
exec pro_Students 10002
--或者声明变量后再执行
declare @StudentId int = 10002
exec pro_Students @StudentId 
declare/dɪˈkler/ v.声明
[3]创建带输出参数、不带输入参数的存储过程
use StudentManagerDB
go
if(exists(select * from sys.objects where name='pro_testOut'))
drop procedure pro_testOut
go
create procedure dbo.pro_testOut
@StudentName varchar(50) output
as
begin
select @StudentName=StudentName from dbo.Students where StudentID=10002
end
go
--执行存储过程
declare @StudentName varchar(50)--声明变量
exec pro_testOut @StudentName output
print @StudentName
--另一种输出方法
select @StudentName

[4]创建带输入、输出参数的存储过程

use StudentManagerDB
go
if(exists (select * from sys.objects where name='pro_testAll'))
drop procedure pro_testAll
go
create procedure pro_testAll
@StudentId int,
@StudentName varchar(50) output
as
begin
select @StudentName=StudentName from Students where StudentId=@StudentId
end
go
--调用存储过程
declare @StudentName varchar(50) 
declare @StudentId int = 10002
exec pro_testAll @StudentId,@StudentName output
print @StudentName

select @StudentName 
storedprocedure 存储过程
store/stɔːr/ n.存储
procedure /prəˈsiːdʒər/ n.过程
parameter /pəˈræmɪtər/ n.参数
direction/dəˈrekʃn/ n.方向
 
4、ADO.NET调用存储过程
[1]调用没有输入和输出参数的存储过程,而且不返回查询结果
using System.Data.SqlClient;
using System.Data;
//调用没有输入和输出参数的存储过程,而且不返回查询结果
class Store
    {
        private string conn = "server=MI-NTCOMPUTER;database=StudentManageDB;Trusted_Connection=sspi";
    
        public void GetProcedure()
        {
            SqlConnection con = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand("dbo.pro_testNo",con);//("存储过程名",con)
            cmd.CommandType =CommandType.StoredProcedure;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

[2]调用带有输入参数的存储过程

using System.Data.SqlClient;
using System.Data;
//调用有输入参数的的存储过程,不返回查询结果
class Store
    {
        private string conn = "server=MI-NTCOMPUTER;database=StudentManageDB;Trusted_Connection=sspi";

        public void GetProcedure()
        {
            SqlConnection con = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand("dbo.pro_testInt",con);//("存储过程名",con)
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@StudentId",SqlDbType.Int));//("存储过程参数变量名",数据类型)
            //把具体的值传给输入参数
            cmd.Parameters["@StudentID"].Value = 100001;
            con.Open();
            //执行存储过程
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

 

[3]调用带有输出参数、没有输入参数的存储过程
 
 
 
 
 
 
 
 
5、连接字符串--SQL身份验证的连接字符串   和  Winodws身份验证的连接字符串
    (1)SQL身份验证的连接字符串
            private string  con ="server=服务器名称;database=数据库名称;uid=登录名;pwd=登录密码";
    (2)Windows身份验证的连接字符串
            private string con="server=服务器名称;database=数据库名称;Trusted_Connection=SSPI";
trust /trʌst/ n.信任
       (3)Trusted_Connection
            当为false时,将在连接中指定用户的ID和密码;
            当为true时,将使用当前的Windows账户凭据进行身份验证;
            可识别的值为true、false、yes、no以及与true等效的sspi(推荐使用);      
posted @ 2022-03-18 11:27  Jack_Yao  阅读(373)  评论(0)    收藏  举报