当前位置:Gxlcms > mysql > VB.net数据库编程(07):SQLserver中的通讯录

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

时间:2021-07-01 10:21:17 帮助过:61人阅读

前面用过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. use Sales
  2. go
  3. create table txl
  4. ( tID int identity(10000,1) primary key,
  5. tName varchar(50) not null,
  6. tEmail varchar(50),
  7. tMobile char(11),
  8. tHomePhone varchar(12),
  9. tMemo varchar(2000) --备注
  10. )
  11. insert into txl values('令狐冲','yms@sohu.com','13681246528','62349087','一个剑客')

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

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


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

注意:SQL语句中的单引号


  1. Imports System.Data.SqlClient
  2. Public Class Form1
  3. Dim cn As SqlConnection
  4. Dim cmd As SqlCommand
  5. Dim da As SqlDataAdapter
  6. Dim ds As DataSet
  7. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  8. Try
  9. cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")
  10. cn.Open()
  11. cmd = New SqlCommand("insert into txl values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')", cn)
  12. cmd.ExecuteNonQuery()
  13. cn.Close()
  14. MessageBox.Show("插入成功")
  15. Catch ex As Exception
  16. MessageBox.Show(ex.ToString & cmd.CommandText)
  17. End Try
  18. End Sub
  19. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  20. cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")
  21. da = New SqlDataAdapter("select * from txl", cn)
  22. ds = New DataSet()
  23. da.Fill(ds, "txl")
  24. DataGridView1.DataSource = ds.Tables("txl")
  25. cn.Close()
  26. End Sub
  27. Private Sub Button2_Click(sender As Object, e As EventArgs)
  28. Try
  29. cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")
  30. cn.Open()
  31. cmd = New SqlCommand("insert into txl values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')", cn)
  32. cmd.ExecuteNonQuery()
  33. cn.Close()
  34. MessageBox.Show("插入成功")
  35. Catch ex As Exception
  36. MessageBox.Show(ex.ToString & cmd.CommandText)
  37. End Try
  38. End Sub
  39. End Class




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

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

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

人气教程排行