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

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


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

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','一个剑客')

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

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


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

注意:SQL语句中的单引号


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




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

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

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




  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值