回 帖 发 新 帖 刷新版面

主题:如何添加/删除数据库中表的内容

vb.net 连接SQL server:

创建一个窗体,上面设置几个textbox控件,和一个button控件

以Button_Click事件能够实现,textbox控件的内容添加到数据库的表内

请大师指点,提供代码,急着使用咯。谢谢![em10][em10]

回复列表 (共3个回复)

沙发

imports system.data
imports system.data.sqlclient

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim connstr ,inscmd as string
connstr="server=服务器名;database=数据库名;uid=用户名;pwd=密码"
inscmd="insert into 表 values('"& textbox1.text &"','"& textbox1.text &"')"
Dim conn As SqlConnection, cmd As SqlCommand
conn = New SqlConnection(connstr)
conn.Open()
cmd = New SqlCommand(inscmd, conn)
cmd.ExecuteNonQuery()
conn.Close()
End Sub
这样应该就可以了

板凳

先做一个数据库处理层来连接数据库
在事务层中写
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim xuehao As String = Trim(Me.TextBox4.Text)
        Dim xingming As String = Trim(Me.TextBox5.Text)
        Dim yingyu As String = Trim(Me.TextBox6.Text)
        Dim dianzhi As String = Trim(Me.TextBox7.Text)
        Dim vc As String = Trim(Me.TextBox8.Text)
        Dim shuju As String = Trim(Me.TextBox9.Text)
        'Dim shuju As Ineger = Val(Me.TextBox9.Text)'''是数字就用Val()
        Select Case cur

            Case 1 '插入一条新记录
                sqlstr = "INSERT INTO student (xuehao,xingming,yingyu,dianzhi,vc," & _
                              "shuju,pass) VALUES ('" & xuehao & "'," & _
                                  "'" & xingming & "','" & yingyu & "'," & _
                                 "'" & dianzhi & "','" & vc & "'," & _
                                  "'" & shuju & "','" & xuehao & "') "

            Case 2 '更新记录

                sqlstr = "UPDATE student SET xingming = " & "'" & xingming & "' ,yingyu = " & "'" & yingyu & "',dianzhi = " & "'" & dianzhi & "',vc = " & "'" & vc & "',shuju = " & "'" & shuju & "'  WHERE xuehao='" & xuehao & "'"
                'sqlstr = "UPDATE student SET xingming = '" & xingming & "'   WHERE xuehao='" & xuehao & "'"
            Case 3 '删除一条记录
                sqlstr = "DELETE FROM student  WHERE xuehao='" & xuehao & "'"

        End Select
        If UpdateDataBase(sqlstr) = True Then '判断是否成功
            MsgBox("操作成功。", MsgBoxStyle.OKOnly + _
             MsgBoxStyle.Information, "操作成功")
        End If

3 楼

哈哈,谢谢大家,前几天已经弄懂了

我来回复

您尚未登录,请登录后再回复。点此登录或注册