Maison > Article > base de données > VB.net数据库编程(07):SQLserver中的通讯录
前面用过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中建立一个表,插入一个记录:
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','一个剑客')
所以第二条记录的标号就是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查看记录和修改记录的例子。
一,查询与非查询的操作。
二,存储过程的操作,参数是如何添加的。