Rumah > pangkalan data > tutorial mysql > VB.net数据库编程(07):SQLserver中的通讯录

VB.net数据库编程(07):SQLserver中的通讯录

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-06-07 15:45:35
asal
1485 orang telah melayarinya

前面用过Access的通讯录,下面用一个SQLserver的通讯录。 首先,在SQL2012中建立一个表,插入一个记录: use Salesgocreate table txl( tID int identity(10000,1) primary key, tName varchar(50) not null, tEmail varchar(50), tMobile char(11), tHomePh

前面用过Access的通讯录,下面用一个SQLserver的通讯录。


首先,在SQL2012中建立一个表,插入一个记录:

1

2

3

4

5

6

7

8

9

10

11

use Sales

go

create table txl

( tID int identity(10000,1) primary key,

  tName varchar(50) not null,

  tEmail varchar(50),

  tMobile char(11),

  tHomePhone varchar(12),

  tMemo varchar(2000) --备注

)

insert into txl values('令狐冲','yms@sohu.com','13681246528','62349087','一个剑客')

Salin selepas log masuk

注:identity(seed,incre)第一个表示起点,第二表示增量。10000,1表示记录从10000开始,每一个记录数据增加加1.

        所以第二条记录的标号就是10001.


下面用VB.net对其插入记录,并显示。

注意:SQL语句中的单引号


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

Imports System.Data.SqlClient

 

Public Class Form1

    Dim cn As SqlConnection

    Dim cmd As SqlCommand

    Dim da As SqlDataAdapter

    Dim ds As DataSet

 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

 

        Try

            cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")

            cn.Open()

            cmd = New SqlCommand("insert into txl values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')", cn)

            cmd.ExecuteNonQuery()

            cn.Close()

            MessageBox.Show("插入成功")

        Catch ex As Exception

            MessageBox.Show(ex.ToString & cmd.CommandText)

        End Try

    End Sub

 

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")

        da = New SqlDataAdapter("select * from txl", cn)

        ds = New DataSet()

        da.Fill(ds, "txl")

        DataGridView1.DataSource = ds.Tables("txl")

        cn.Close()

    End Sub

 

    Private Sub Button2_Click(sender As Object, e As EventArgs)

        Try

            cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")

            cn.Open()

            cmd = New SqlCommand("insert into txl values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')", cn)

            cmd.ExecuteNonQuery()

            cn.Close()

            MessageBox.Show("插入成功")

        Catch ex As Exception

            MessageBox.Show(ex.ToString & cmd.CommandText)

        End Try

    End Sub

End Class

Salin selepas log masuk

VB.net数据库编程(07):SQLserver中的通讯录



由引我们学习了Access和SQlserver查看记录和修改记录的例子。

一,查询与非查询的操作。

二,存储过程的操作,参数是如何添加的。




Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan