Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Visual Studio on click

hi I am using visual studio to connect to a database and update a database table

please see code

Public Class DatabaseConn

    Private Sub DatabaseConn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objConnection As SqlClient.SqlConnection = New _
        SqlClient.SqlConnection("Server=svricassql5;Database=pubs;Trusted_Connection=yes")

        Dim queryStr As String = "INSERT INTO authors " & _
        "(au_id,au_lname,au_fname, Contract) " & _
        "VALUES(30,'testtesttest','au_fname',1)"
        Dim objCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(queryStr, objConnection)

        objCommand.Connection.Open()

        objCommand.ExecuteNonQuery()

        objCommand.Connection.Close()

       
    End Sub


This all works well but what I would like to do now is using the above code update the database table only when a button is clicked, can anyone give me an example of this please?

Thanks
0
ac_davis2002
Asked:
ac_davis2002
1 Solution
 
jpaulinoCommented:
You can add a button and do something like this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objConnection As SqlClient.SqlConnection = New _
        SqlClient.SqlConnection("Server=svricassql5;Database=pubs;Trusted_Connection=yes")
        Dim queryStr As String = "INSERT INTO authors " & _
        "(au_id,au_lname,au_fname, Contract) " & _
        "VALUES(30,'testtesttest','au_fname',1)"
        Dim objCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(queryStr, objConnection)
        objCommand.Connection.Open()
        objCommand.ExecuteNonQuery()
        objCommand.Connection.Close()
 
    End Sub

Open in new window

0
 
ac_davis2002Author Commented:
Thats great thanks for your help
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now