Results 1 to 7 of 7

Thread: sql 2008 and VB 2010

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    8

    sql 2008 and VB 2010

    Hi,

    I am using the following select statement to inject data into my table

    "INSERT INTO TblVersion([ComputerName],[Version],[Date]) VALUES ('" & compname & "','" & myversion & "','" & mytime & "' )"

    This works fine.

    I need some advice:
    If the computer name is already in the 'computerName' field I just want to update the 'myversion' and 'mytime' fields, rather than have another entry for the same computer.

    how do i perform this check?

    Thabks in advance


    VB2010 and SQL 2008

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: sql 2008 and VB 2010

    Can you use a STORED PROCEDURE instead of this single sql statement?

    If so you would do something like this.

    Code:
    Create Procedure awc_PartMonthly_Edit_Save
    			 @Part varchar(100)
    			,@NoteKey varchar(100)
    			,@Note varchar(5000)
    			,@Flag1 varchar(100)
    			,@yn varchar(1)
    As
    Set NoCount On
    
    Update PartMonthly_T
       Set  Note=@Note
              ,TDate=GetDate()
       Where PartId=@Part
           and Convert(varchar(10),NoteDate,101)+'-'+NoteGen+'-'+Cast(NoteType as varchar(10))=@NoteKey
    
    If @@RowCount=0
        Insert into PartMonthly_T values (@Part,Left(@NoteKey,10),Substring(@NoteKey,12,1)
                       ,Right(@NoteKey,Len(@NoteKey)-13),@Note,GetDate())

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    8

    Re: sql 2008 and VB 2010

    Hi,

    I am quite new to Vb and SQL. Is the stored procedure created on the SQL server or in my vb application?

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: sql 2008 and VB 2010

    It is created on the server - it's basically a "package" of several queries that run as "one".

    The script I showed you is run from a query window in SQL SERVER MANAGEMENT STUDIO - and it creates the SPROC. You do this only once - SPROC is retained in the DB for user after that. You can drop/re-create it or ALTER it...

    They are called from the client using code something like this...

    Code:
    Try
        Using dcn As New SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("LocalSQLServerAWC").ToString)
            Using cmd As New SqlCommand
                cmd.CommandType = CommandType.StoredProcedure
                cmd.CommandText = "dbo.awc_SetFormula"
                cmd.Connection = dcn
                cmd.CommandTimeout = 0
                DetermineParameters(dcn, cmd)
                cmd.Parameters("@CtrlVal1").Value = ctrlval1
                cmd.Parameters("@CtrlVal2").Value = ctrlval2
                dcn.Open()
                strMessage = cmd.ExecuteScalar().ToString
                .NewObject("formula", "true")
                .Seperate()
            End Using
        End Using
    Catch ex As Exception
        strSuccess = ""
        strMessage = ex.Message
    End Try
    They have many advantages - but some of the obvious ones are CODE RE-USE (you can call it from many places in the client side) and the ability to change them in the DB without changing your client code (unless of course you add a parameter - but there are even ways to eliminate that requirement).

    I only use SPROCS - never write INLINE SQL statements.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    8

    Re: sql 2008 and VB 2010

    Hi,
    Stored prices do make sense. Could you help me create one for my situation as i am finding it a little confusing to say the least. I can work out the vb side of things.

    Thanks

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: sql 2008 and VB 2010

    I am making assumptions on the datatype and size of your fields - you will need to adjust that to make them real.

    Code:
    Create Procedure TblVersionPut_P
         @ComputerName varchar(100)
        ,@Version varchar(100)
        ,@Date datetime
    As
    Set NoCount On
    
    Update TblVersion Set [Version]=@Version
                                  ,[Date]=@Date
                 Where [ComputerName]=@ComputerName
    
    If @@RowCount=0
      Insert into TblVersion values (@ComputerName, @Version, @Date)
    Put that into a SSMS Query Window - and click Execute

    Go to a new query window and type this to test it

    Code:
    Begin Tran
    Exec TblVersionPut_P 'TestComp','123','2012-01-01'
    Select * From TblVersion
    Exec TblVersionPut_P 'TestComp2','999','2012-02-01'
    Exec TblVersionPut_P 'TestComp','456','2012-02-02'
    Select * From TblVersion
    Rollback
    The Begin Tran/Rollback makes all these test inserts go away - but you should see the results as a new row for TESTCOMP - then a new row for TESTCOMP2 and a change to TESTCOMP.

    I always do all my testing in SSMS - before trying to get the VB side to talk to the sprocs - makes for easy creation of SQL code that's fully vetted on the server before you waste your time making it work from VB.
    Last edited by szlamany; Feb 17th, 2012 at 02:49 PM. Reason: typo

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    8

    Re: sql 2008 and VB 2010

    Fantastic!
    Thanks for your support. I can understand how this works and how valueable SP can be. I will attempt to call this SP from VB tomorrow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width