SQL Server使用笔记

1、连接字符串

  SQL Server 身份验证,如:"server=yqzhu-peter;database=WindWMNew1_DB;uid=sa;pwd=ABcd1234;Connect Timeout=1000"

2、sa(system administrator),dbo(database owner)

3、SQL 不区分大小写,包括sql语句,数据库名,表名,字段名,变量名,函数名,以及字段中实际值的比较

use wfc_db
go

create table TestCase(
Id int,
CaseName varchar(20),
UpdateDate datetime
)

alter table testcase
add [from] varchar(20)

insert into Dbo.TestCase
values(1, '5555', Getdate(), 'shanghai')
insert into Dbo.TestCase
values(2, '66666', Getdate(), 'NANJING')

select * from Dbo.TestCase

declare @aa varchar(20)
select @aa = 'SHANGHAI'
print @AA
if @AA = (select [from] from testcase)
    print 'yes'
else
    print 'no'

    
select [from] from testcase
DELETE FROM TESTCASE WHERE ID = 2
View Code

  1、当对象名为sql中的关键字时,需要用[]包括

  2、创建表时,字段默认是可空的

4、in的两种基本使用方式:

  1、简单的方式:select * from testcase where [from] in ('shanghai')

  2、组合方式: select * from testcase where [from] in (select [from] from testcase where datediff(day, updatedate, getdate()) = 4)

   注意该方式的select子句返回必须为一维表,否则会报错

5、 批量插入:  insert into persons(name, classId)  select name, 1 from students  

      或       insert into persons(name, classId)  values ('cheng', 1), ('cheng2', 2), ('cheng3', 3),     

6、可以定义表变量: 

    DECLARE @temptb TABLE ( 

      [id] int identity(1,1),
      UserID varchar(50)
    )

  该变量常用于字符串的处理

7、自定义函数:

  有三种类型: 1、标量函数 2、内联表值函数 3、多语句表值函数

  详细可参考: 1、SQL Function 自定义函数

         2、SQLServer:FUNCTION/CURSOR/PROCEDURE/TRIGGER

         3、SQL Server游标的使用【转】

 8、查询重复记录

  distinct关键字只能去除重复字段,而不能用于多个字段,如select distinct name from tb_student可行,但select distinct name, age from tb_student会报错

  查询重复记录方式是使用group by及having子句,如

select wid, count(*) from TB_CustLotteryInfo
where [datetime] > '20160112'
group by wid
having count(*) > 3
View Code

 9、存储过程返回数据的三种方式:

  a、通过select,返回标量值或table

    对应标量值,sql:select 1,c#:dt.Rows[0][0].ToString()

  b、output关键字,可以参考SQLServer:FUNCTION/CURSOR/PROCEDURE/TRIGGER

    这种方式应用在sql调用存储过程

  c、影响的行数

  sql中return的作用是使存储过程立即返回,与其他编程语言的return不同

 10、游标的使用,详细可参考: SQL Server游标的使用【转】

使用C#定义一个DBHelper,并兼容多种类型的数据库

实例源码DBHelperTest.rar

说明:

1、当记录的某列为可空时,应当判断是否为DBNull.Value再进行类型转换,如:

  FirstName = reader["FirstName"] == DBNull.Value ?  "" : (string)reader["FirstName"];

2、表Employees的模式

create table Employee(
    id int IDENTITY(1,1) primary key,
    FirstName varchar(255),
    SecondName varchar(255),
    Salary decimal(10, 2)
)
View Code

  存储过程SP_DeleteEmployeeById

ALTER PROCEDURE [dbo].[SP_DeleteEmployeeById](
    @ids varchar(100)
)
AS
BEGIN
    declare @temptb table(
        id int
    )
    declare @index int
    declare @str varchar(10)
    select @str = @ids
    select @index = charindex(',', @str)
    while @index > 1
    begin
        insert into @temptb values(cast(left(@str, @index - 1) as int)) 
        select @str = right(@str, len(@str) - @index)
        select @index = charindex(',', @str)
    end
    insert into @temptb values(cast(@str as int)) 

    delete from dbo.Employees where id in (select id from @temptb)
END
View Code

  存储过程SP_GetEmployees

ALTER PROCEDURE [dbo].[SP_GetEmployees](
    @id int
)
AS
BEGIN
    if @id = 0
        select * from dbo.Employees
    else
        select * from dbo.Employees where id = @id
END
View Code

参考:1、数据库访问类DBHelper  2、如何做个好用的数据库访问类

原文地址:https://www.cnblogs.com/MattCheng/p/4671167.html