DOTNET零碎总结---VB.NET修改数据存在多个txtbox时,SQL语句的操作

2.DOTNET零碎总结---VB.NET修改数据存在多个txtbox时,SQL语句的操作
1. 一个Button1的text为查询和一个DataGridView1,点击查询按钮的代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strConn = "Data Source=192.168.1.110/orcl;User ID=test;Password=orcl"
        Dim dbcmd As OracleCommand
        Dim dt = New DataTable("ds")
        Dim dbConnection As OracleConnection
        Try
            dbConnection = New OracleConnection(strConn)
            dbConnection.Open()
            dbcmd = dbConnection.CreateCommand
            dbcmd.CommandText = "select * from student"
            Dim da As OracleDataAdapter = New OracleDataAdapter(dbcmd)
            da.Fill(dt)
            DataGridView1.DataSource = dt '这时候可以把dt的值直接赋值给DataGridview1控件
        Catch ex As Exception
            MsgBox("查询数据库出错")
        End Try
    End Sub


2.存在两个TextBox1.TextBox2,文本框和一个按钮, Button2,按钮text为修改,点击后的代码
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim username, password As String
        Dim temp, temp3 As String
        If Len(TextBox1.Text.Trim) <> 0 Then
            username = "sname=" + "'" + TextBox1.Text.Trim + "'" + " , "
        Else
            username = ""
        End If
        If Len(TextBox2.Text.Trim) <> 0 Then
            password = "spwd=" + "'" + TextBox2.Text.Trim + "'" + " , "
        Else
            password = ""
        End If
        If Len(username) <> 0 And Len(password) <> 0 Then
            temp = username + password
            MsgBox(temp.Length)
            temp = Mid(temp, 1, temp.Length - 3)
            MsgBox("temp" + temp)
            MsgBox("current" + DataGridView1.CurrentRow.Cells(1).Value)
        Else
  
         temp = ""
        End If
       
        Dim strConn = "Data Source=192.168.1.110/orcl;User ID=test;Password=orcl"
        Dim dbcmd As OracleCommand
        Dim dbConnection As OracleConnection
        'If username.Length = 0 And password = 0 Then
        Try
            dbConnection = New OracleConnection(strConn)
            dbConnection.Open()
            dbcmd = dbConnection.CreateCommand
            Dim temp2 As String
            temp2 = "update student set " + temp + "  where sname=" + "'" + DataGridView1.CurrentRow.Cells(1).Value + "'"
            dbcmd.CommandText = temp2
            MsgBox(temp2)
            'dbConnection.BeginTransaction()
            dbcmd.ExecuteNonQuery() '返回改动的row行数
        Catch ex As Exception
            MsgBox("修改失败")
            Exit Sub
        End Try
        ' Else
        '    MsgBox("请填入要修改的参数")
        'End If
    End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

添柴程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值