代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
VB.Net
】
vb实现数据库的连接,修改,删除,插入(ADO.Net)
作者:
Dezai.CN
/ 发布于
2013/6/18
/
1436
Public Class 全部学生信息 Dim conn As New SqlConnection Dim rs As New SqlDataAdapter 'Dim dt As New DataTable Dim ds As New DataSet Public Function connect_db() As SqlConnection Dim constr As String constr = "Server=PC-201105311336\SQLEXPRESS;uid=sa;pwd=123456;database=jiaxiaoai" 'rs.Fill(dataset, "student") conn = New SqlConnection(constr) conn.Open() connect_db = conn End Function Private Sub 全部学生信息_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load '窗口初始化函数 Dim sql As String Dim dt As New DataTable sql = "select * from student" conn = connect_db() rs = New SqlDataAdapter(sql, conn) conn.Close() rs.Fill(dt) '填充表 DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据 DataGridView1.Columns(0).HeaderText = "学号" '设置列名 DataGridView1.Columns(1).HeaderText = "姓名" DataGridView1.Columns(2).HeaderText = "年龄" DataGridView1.Columns(3).HeaderText = "成绩" rownum.Text = Str(DataGridView1.RowCount) '获取行数 xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值 nametext.Text = DataGridView1.Rows(0).Cells(1).Value If (DataGridView1.Rows(0).Cells(2).Value = 0) Then agetext.Text = "" Else agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value) End If If (DataGridView1.Rows(0).Cells(3).Value = 0) Then scoretext.Text = "" Else scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value) End If End Sub Private Sub selectbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles selectbtn.Click Dim sql As String Dim dt As New DataTable sql = "select * from student where name like 'l%'" conn = connect_db() rs = New SqlDataAdapter(sql, conn) conn.Close() rs.Fill(dt) '填充表 DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据 DataGridView1.Columns(0).HeaderText = "学号" '设置列名 DataGridView1.Columns(1).HeaderText = "姓名" DataGridView1.Columns(2).HeaderText = "年龄" DataGridView1.Columns(3).HeaderText = "成绩" rownum.Text = Str(DataGridView1.RowCount) '获取行数 xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值 nametext.Text = DataGridView1.Rows(0).Cells(1).Value If (DataGridView1.Rows(0).Cells(2).Value = 0) Then agetext.Text = "" Else agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value) End If If (DataGridView1.Rows(0).Cells(3).Value = 0) Then scoretext.Text = "" Else scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value) End If End Sub Private Sub DataGridView1_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick '鼠标点击单元格触发事件 'DataGridView1.CurrentRow.Index获取当前单元格的行号 xuehaotext.Text = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value '获取datagridview中的某个值 nametext.Text = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value If (DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value = 0) Then agetext.Text = "" Else agetext.Text = Str(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value) End If If (DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = 0) Then scoretext.Text = "" Else scoretext.Text = Str(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value) End If End Sub Private Sub updatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updatebtn.Click Dim sql As String Dim row As String Dim dc As New SqlCommand Dim age If agetext.Text = "" Then age = 0 Else age = Val(agetext.Text) End If sql = "update student set sid='" & xuehaotext.Text & "', name='" & nametext.Text & "',age='" & age & "',score='" & scoretext.Text & "' where sid='" & DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value & "'" conn = connect_db() dc = New SqlCommand(sql, conn) row = dc.ExecuteNonQuery() conn.Close() If (row > 0) Then MsgBox("修改成功") Else MsgBox("修改失败") End If DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value = xuehaotext.Text DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = nametext.Text DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value = agetext.Text DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = scoretext.Text End Sub Private Sub insertbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles insertbtn.Click Dim sql As String Dim row As String Dim dc As New SqlCommand Dim age If agetext.Text = "" Then age = 0 Else age = Val(agetext.Text) End If sql = "insert into student values('" & xuehaotext.Text & "', '" & nametext.Text & "','" & age & "','" & scoretext.Text & "')" conn = connect_db() dc = New SqlCommand(sql, conn) row = dc.ExecuteNonQuery() 'conn.Close() If (row > 0) Then MsgBox("插入成功") Else MsgBox("插入失败") End If Dim dt As New DataTable sql = "select * from student" 'conn = connect_db() rs = New SqlDataAdapter(sql, conn) conn.Close() rs.Fill(dt) '填充表 DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据 DataGridView1.Columns(0).HeaderText = "学号" '设置列名 DataGridView1.Columns(1).HeaderText = "姓名" DataGridView1.Columns(2).HeaderText = "年龄" DataGridView1.Columns(3).HeaderText = "成绩" rownum.Text = Str(DataGridView1.RowCount) '获取行数 xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值 nametext.Text = DataGridView1.Rows(0).Cells(1).Value If (DataGridView1.Rows(0).Cells(2).Value = 0) Then agetext.Text = "" Else agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value) End If If (DataGridView1.Rows(0).Cells(3).Value = 0) Then scoretext.Text = "" Else scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value) End If End Sub Private Sub deletebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deletebtn.Click Dim sql As String Dim row As String Dim dc As New SqlCommand Dim age If agetext.Text = "" Then age = 0 Else age = Val(agetext.Text) End If sql = "delete student where sid='" & DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value & "'and name='" & DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value & "'" conn = connect_db() dc = New SqlCommand(sql, conn) row = dc.ExecuteNonQuery() 'conn.Close() If (row > 0) Then MsgBox("删除成功") Else MsgBox("删除失败") End If Dim dt As New DataTable sql = "select * from student" conn = connect_db() rs = New SqlDataAdapter(sql, conn) conn.Close() rs.Fill(dt) '填充表 DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据 DataGridView1.Columns(0).HeaderText = "学号" '设置列名 DataGridView1.Columns(1).HeaderText = "姓名" DataGridView1.Columns(2).HeaderText = "年龄" DataGridView1.Columns(3).HeaderText = "成绩" rownum.Text = Str(DataGridView1.RowCount) '获取行数 xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值 nametext.Text = DataGridView1.Rows(0).Cells(1).Value If (DataGridView1.Rows(0).Cells(2).Value = 0) Then agetext.Text = "" Else agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value) End If If (DataGridView1.Rows(0).Cells(3).Value = 0) Then scoretext.Text = "" Else scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value) End If End Sub End Class
试试其它关键字
数据库的连接
同语言下
.
根据User Agent来判定操作系统与浏览器类型
.
将PPT内容导出为JPG图片
.
Java屏幕截取
.
发送邮件组件
.
子窗体在父窗体指定的控件中显示
.
取文件的大小
.
从某个目录中筛选文件
.
VB脚本调用exe应用程序并传递参数
.
图像格式转换工具
.
洗牌算法
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3