Imports System

Module Program
    Sub Main(args As String())

        Dim a, b As Int32
        a = 1
        b = 2

        If a < 1 Then
            Console.WriteLine("结果1")
        Else
            Console.WriteLine("结果2")
        End If

        Console.WriteLine("========================")

        For index = 1 To 10
            Console.WriteLine(index)
        Next

        If True Then

        End If

        Console.WriteLine("Hello World!")

        Dim c(3) As Integer

        c(1) = 1
        c(2) = 1
        c(3) = 3

        For index = 1 To 3
            Console.WriteLine(c(index))
        Next

        Console.WriteLine("==========For Each========================")

        For Each item In func1()
            Console.WriteLine(item)
        Next

        Console.WriteLine("==========Sub函数========================")
        func2()

        //Public integerClass As New classHolder(Of Integer)

    End Sub



    Public Function func1() As Integer()
        Dim a(10) As Integer

        For index = 1 To 10
            a.Append(index)
        Next
        func1 = a
    End Function

    Public Sub func2()
        Console.WriteLine(“这是func2()”)
    End Sub


    Public Class Customer
        Public Sub New()
            Debug.WriteLine("setuped")

        End Sub

        '--------New(Byval)---------
        Public Sub New(ByVal sname As String)
            Name = sname
            Debug.WriteLine(Name & "setuped")
        End Sub

        Private m_Name As String
        Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal Value As String)
                m_Name = Value
            End Set
        End Property
        Private m_address As String
        Property Address() As String
            Get
                Return m_address
            End Get
            Set(ByVal Value As String)
                m_address = Value
            End Set
        End Property

        Public Function PlaceOrder() As String

        End Function

        Public Function CheckOrderStatus() As String

        End Function

        Public Overridable Function CalculateDiscount() As String
            Return "base class"
        End Function
    End Class

    Public Class SubPro : Inherits Customer

        Private m_zfjg As String
        Property Zfjg() As String
            Get
                Return Zfjg
            End Get
            Set(ByVal Value As String)
                m_zfjg = Value
            End Set
        End Property

        Private m_bsc As String
        Property Bsc() As String
            Get
                Return m_bsc
            End Get
            Set(ByVal Value As String)
                m_bsc = Value
            End Set
        End Property

        Public Overrides Function CalculateDiscount() As String
            Return "sub class"
        End Function
        Public Function SubProSelf() As String

        End Function
        Public Sub New()
            MyBase.New()
        End Sub
    End Class

End Module

结果:

结果2

1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 Hello World!
 1
 1
 3
For Each==============
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0
Sub函数==============
 这是func2()D:\Csharp\VBTest\VBTest\bin\Debug\net6.0\VBTest.exe (进程 1600)已退出,代码为 0


要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

一、插入语句
1. 普通插入
insert into tableA(字段1,字段2,字段3)values(字段值1,字段值2,字段值3)
例:

insert into tableA(
	userName,
	userSex,
	userPhone
)values(
	'张三',
	'男',
	'18812345678'
)

2. 表数据查询(复制)插入
insert into tableA(字段1,字段2,字段3)select (字段4,字段5,字段6) from tableB

insert into tableA(
	userName,
	userSex,
	userPhone
)select
	memberName,
	memberSex,
	memberPhone
 from tableB
 where ……

二、查询语句
1. 所有数据查询
select 字段1,字段2,字段3 from tableA where ……
例:

select
	userName,
	userSex,
	userPhone
from tableA
where ……

2. 根据某条件查询前多少条数据
select top n 字段1,字段2,字段3 from tableA where ……
(n为数据条数)
例(前10条数据):

select
	top 10
	userName,
	userSex,
	userPhone
from tableA
where ……
order by userName

三、更新语句
1. 单表数据更新
update tableA set 字段1=‘字段值1’,字段2=‘字段值2’,字段3=‘字段值3’ where……
例:

update tableA
set userName='李四',
userSex='男',
userPhone='13012345678'
where userName='张三'

2. 多表联合数据更新
update a set a.字段1=b.字段4,a.字段2=b.字段5,a.字段3=b.字段6
from tableA a left join tableB b on a.userId=b.userId
where……
例:

update a
set a.userName=b.userName,
a.userSex=b.userSex,
a.userPhone=b.userPhone
from tableA a 
left join tableB on a.userId=b.userId
where ……

四、删除语句
delete from tableA where ……
例:

delete from tableA where userName='张三'
1
五、case when……else
case when 条件1 then ……when 条件2 then……else……end
例:

select
	case when sexFlag=0 then '女'
	when sexFlag=1 then '男'
	else '未识别'
	end userSex
from tableA

六、left join(左关联查询)
结果集返回左表(tableA)所有数据,若左表存在、右表(tableB)无数据则右表数据返回null
例:

select
	a.userName,--tableA表中人员姓名
	b.userOrderId--tableB中人员订单号,若不存在则返回null
from tableA a
left join tableB b on a.userId=b.userId

七、right join(右关联查询)
结果集返回右表(tableB)所有数据,若右表存在、左表(tableA)无数据则左表数据返回null
例:

select
	a.userName,--tableA表中人员姓名,若不存在则返回null
	b.userOrderId--tableB中所有订单号
from tableA a
right join tableB b on a.userId=b.userId

八、inner join(内关联查询)
结果集返回左表(tableA)和右表(tableB)都存在的数据
例:

select
	a.userName,--tableA表中人员姓名
	b.userOrderId--tableB中所有订单号
from tableA a
inner join tableB b on a.userId=b.userId

九、like
模糊条件查询,查询某个字段值包含某一字符串的结果集
例(查询姓名中含有【国】字的所有人员信息):

select
……
from tableA
where userName like '%国%'

十、concat
字符换拼接组合
例(姓名/性别/手机号组合为一个字段):

select
--返回:张三-男-18812345678
CONCAT(userName,'-',userSex,'-',userPhone) as userInfo
from tableA
where ……

十一、charindex
判断某字段值是否包含某一字符串,若包含则返回字符串的位置(大于0),若不存在则返回0,一般用于筛选条件
例(查询姓名中包含【国】字的所有人员信息):

select
……
from tableA
where charindex('国',userName)>0
1
2
3
4
十二、substring
截取字符串
例:

select
--手机号第4位开始截取,共截取4位
substring(userPhone,4,4)
from tableA


一、数据库

1 修改数据库名&删除数据库
alter database tablename_old modify name = tablename_new;

drop database tablename;

二、数据类型认识

2.1 数字类型

 

2.2 日期类型

 

2.3 字符串类型

 

三、修改命令——alter

3.1 修改字段类型

alter table tableA alter column name varchar(10);

alter table tableA alter column age float not null;

3.1 新增&删除主键

alter table tableA add constraint KID(主键约束名)  primary key(ID);

alter table tableA drop constraint KID;

3.1 更改&添加字段名

exec sp_rename 'tableA .age','userage','column';

alter table tableA add grade varchar(10) not null;

四、主键&外键(略)

五、插入新增记录——insert

5.1 插入新增行

insert into tableA ('column1','column2','column3','column4') values ('value1','value2','value3','value4'),('value1','value2','value3','value4'),...('value1','value2','value3','value4');

insert into tableA ('column1','column2','column3','column4')  select column1,column2,column3,column4 from tableB ;

六、修改记录——updata

6.1 updata tableA set  字段 = 值  where  字段=值(限制条件)

 七、删除记录——delete

7.1 delete from  tableA where 字段=值(限制条件)

八、条件限制——where

8.1 精确条件

where 字段 = '值'

8.2 模糊条件

where 字段  like '%值%'

九、between¬ between语法

select * from tableA where ID between 10 and 20;

select * from tableA where ID  not  between 10 and 20;

十、子查询in¬ in语法

select * from tableA where name in  ('zhangsan',''lisi) ;

select * from tableA where name not  in  ('zhangsan',''lisi);

select * from tableA where name   in  (select  name  from tableB );

select * from tableA where name  not  in  (select  name  from tableB );

十一、返回结果排序——order by

select * from  tableA  order by ID desc; #按照ID降序

select * from  tableA  order by ID asc ;#按照ID升序

select * from  tableA  order by ID asc ,age desc ;#先按照ID升序,再在ID升序的基础上按照age降序;

十二、 关联查询

12.1 inner join——内连接

select * from tableA inner join tableB on tableA.ID = tableB.ID;

12.2 left join——左连接

select * from tableA left join tableB on tableA.ID = tableB.ID;

12.3 right join——右连接

select * from tableA right join tableB on tableA.ID = tableB.ID;

12.4 full join——全连接

select * from tableA full join tableB on tableA.ID = tableB.ID;

十二、 函数

12.1 返回字符串字符数——len()

12.2 返回当前时间——getdate()、getutcdata()

12.3 转换日期格式——convert(datetype,date_to_be_converted,style)







convert(verchar(10),getdate(),110)

12.4 计算时间差——datediff(datepart,startdate,enddate)

#datepart常取值:day,month,year,hour,minute

select datediff(day,'2020-05-01','2020-05-03') ; #结果2

select datediff(month,'2020-05-01','2020-06-03') ; #结果1

12.5 增加日期——dateadd(datepart,number,date)

select dateadd(day,3,'2020-05-01') ; #结果2020-05-04

select dateadd(day,-3,'2020-05-01') ; #结果2020-04-28

12.6 获取日期单独部分——datepart(datepart,number,date)

select datepart(day,getdate());#select datepart(dd,getdate());返回类型为int整型

select datename(day,getdate());#select datename(dd,getdate());返回类型为varchar型

select day(getdate();select month(getdate();select year(getdate();

12.7 返回字符在另一个字符串中的位置——charindex()、patindex()



select charindex('cc','aabbccdd');#返回结果5;

select patindex('%dd','aabbccdd');#返回结果7(%dd:是否以dd结尾);

12.8 删除&替换指定长度的字符——stuff(字符串,开始位置,长度,替代字符串)

select stuff('aabbccdd',5,2,'ee');#返回结果aabbeedd;

12.9 截取指定长度字符串——substring(字符串,开始位置,长度)

select substring('aabbccdd',5,2);#返回结果cc;

12.10 截取字符串——left()、right()

select left('aabbccdd',5);#返回结果aabbc;

select right('aabbccdd',5);#返回结果bccdd;

12.10 字符串除空格——ltrim()、rtrim()



select ltrim('    aabbccdd    ');#返回结果'aabbccdd    ';

select rtrim('    aabbccdd    ');#返回结果'    aabbccdd ';

12.11 字符串大小写转换——lower()、upper()

select lower('Abc');#返回结果abc

select upper('Abc');#返回结果ABC

12.12 字符串替换——replace()

select replace('aabbccddcc','cc','ee');#返回结果aabbeeddee;

12.13 字符串重复——replicate() 、space()

select replicate('aab',3);#返回结果aabaabaab;

select 'a'+space(5)+'a';#返回结果'a     a';

12.14 倒置字符串位置——reverse()

 select reverse('aab');#返回结果baa;

12.15 改变字段类型——cast()

 select cast(123 as varchar(10));#整型转化成字符型;

 select cast(12.5 as decimal(18,2));#返回结果12.50;

12.16 条件判断转换函数——case()

select case when sex='1' then '女' else '男' end as sex from tableA;