sql commands in visual basic express 2005

hi im trying to use a delete command to delete data from table1 if its in table2

my command is

delete table1 from table1, table2 where table1.test = table2.test

please help

i keep getting

delete is not declared so i tryed this

Dim delete As New SqlCommand()
delete table1 from table1, table2 where table1.test = table2.test

now i get delete isnt an expression please help

willing to change anything to get this to work




Answer this question

sql commands in visual basic express 2005

  • Maurice Maglalang

    ok i got that command to work but i had to use it as a query

    do you know of any way to run that command when i hit a button on the screen

    thanks

  • Emil Prager

    Can you paste in your connection string code into this thread   (change the username and password if security is a concern)


  • habnix

    Hi,

    your delete-statement is wrong. The syntax is:

    DELETE FROM table_name WHERE column_name = some_value
    from http://www.w3schools.com/sql/sql_delete.asp

    this page (http://www.w3schools.com/sql) may be very helpful, when learning sql.

    What exactly do you need only the sql statemen or some lines of VB code
    Please tell us what you want to delete in your tables, if you need further assistance.


  • djjos

    check this out: http://support.microsoft.com/ kbid=823679

    -- from the link --
    Version 1.1 of SqlClient introduced a regression in the handling of connection strings that contain apostrophes (') or double quotation marks (""). This causes connection strings with correctly escaped apostrophes or double quotation marks to fail, and you receive the following error message:
    Format of the initialization string does not conform to specification starting at index {0}.

  • paraGOD

    try this link: MSDN SqlCommand.ExecuteNonQuery

    In your on_click button event add this code
    ----------------
    Dim strCnn As String = "Your Connection String"
    Dim strSQL as String = "Delete From Server where Server IN (SELECT Product1 FROM Customer)"

    CreateCommand(strCnn, strCnn)
    ----------------

    Then add this sub (copied from the link above)

    Public Sub CreateCommand(ByVal queryString As String, ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.Connection.Open()
        command.ExecuteNonQuery()
    End Using
    End Sub

    Also add:
    Imports System.Data.SqlClient
    To the top of your .vb file


  • Javelin

    Try using a variable name other than delete, cmdDelete for instance.


  • Jivitesh

    i got that but when i use this code it states delete isnt declared


    Delete From Server where Server IN (SELECT Product1 FROM Customer);





    please help



  • trogers

    In the designer:
    1) Add a button to the page.
    2) Double click the button to open the on_click event handler code.
    3) Place the code you wish to execute in the on_click event handler.
    4) Compile and test.



  • stefan__

    Imports System.Data.SqlClient

    Public Class Form2

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim strCnn As String = "C:\Documents and Settings\Justin\My Documents\Visual Studio 2005\Projects\Inventory Systems1\Inventory Systems1\Inventory.mdf"

    Dim strSQL As String = "Delete From Server where Server IN (SELECT Product1 FROM Customer);"

    CreateCommand(strCnn, strCnn)

    End Sub

    Public Sub CreateCommand(ByVal queryString As String, ByVal connectionString As String)

    Using connection As New SqlConnection(connectionString)

    Dim command As New SqlCommand(queryString, connection)

    command.Connection.Open()

    command.ExecuteNonQuery()

    End Using

    End Sub

    End Class



  • Chakshu

    i read the site but im using
    visual basic 2005
    net framework 2


    it doesnt say anything about these also i cant figure out how to download it


    please help

  • mcfin

    ok i tryed that but im getting this error when i click on the button


    format of initialization string does not conform to specification starting at index 0


    thanks

  • GTH

    Hi Justin ,

    The Sql-command is: "Delete FROM table1 where (searchcondtion................"

    If you are deleting based on values from another table:

    "Delete From Table1 where test IN (SELECT Test FROM Table2);"

    By that you are taking all the values in Table2.field("Test") and If they also are in Table1.field("Test"), you delete them in Table1.

    Hope it helps




  • RoseyA

    That looks right finnsoft

    but i still having trouble with delete not being declared

    can someone please help me

    i know its some vb code

    but i cant figure it out

  • sql commands in visual basic express 2005