Link to home
Start Free TrialLog in
Avatar of GarySB
GarySB

asked on

Calling Stored Procedure from VB express 2008

I created a stored procedure that performs an update on a single field and now i want to call it from my vb application.  Below is what I have tried with some variations.  

       EXEC (StoredProcedure7)
OR
        Dim i As Int16
        i = CompanySetupDataSetTableAdapters.ExecuteSQL("StoredProcedure7")

Also, how do I rename a stored procedure?
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

You can do something like this:
        Dim myConnectionString As String = _
       "Data Source=.\SQLEXPRESS;AttachDbFilename='c:\myDatabase.mdf';Integrated Security=True;User Instance=True"
 
        Using connection As New SqlClient.SqlConnection(myConnectionString)
            connection.Open()
 
            Using command As New SqlClient.SqlCommand("StoredProcedure7", connection)
                command.CommandType = CommandType.StoredProcedure
                ' If you want to pass some parameter
                'command.Parameters.Add("@codigo", SqlDbType.Int).Value = SomeValue
 
                command.ExecuteNonQuery()
            End Using
        End Using

Open in new window

To rename a stored procedure you can use in a function this
EXEC sp_rename 'oldname', 'newname'
Avatar of GarySB
GarySB

ASKER

Thank you for responding so quickly.

Can the above code you wrote be referenced using one of my existing data connections?
I'm already able to read and write to the tables with the below connections.
SetupDataSet, TableAdapterManager, TableAdapter or BindingSource
Thanks
I believe that the TableAdapter can use a Stored Procedure as it's source.  You just have to find and populate the SelectCommand I believe.  
Avatar of GarySB

ASKER

Can you give me  a sample of the syntax using my names please.  I thought the GETDATA might be usefull, but does not appear to be for this.

ProjectDataTableAdapter, StoredProcedure7
Avatar of GarySB

ASKER

I'm trying to use your solution. How can I correct this error?. I'm trying both of these and I keep getting the same error:

Cannot open database "<C:\Documents and Settings\user\My Documents\CompanySetup.mdf>" requested by the login. The login failed. Login failed for user 'HP2\user'.

 'HP2\user' is correct because it shows in the properties of the database.

The database does not have a password and the properties shows that the bottom one below is exactly the right path.  I also tried the C: drive location. The database is on my local computer

Dim myConnectionString As String = _
                "Data Source=.\SQLEXPRESS;Database=<C:\Documents and Settings\user\My Documents\CompanySetup.mdf>;Integrated Security=SSPI"

Dim myConnectionString As String = _
                "Data Source=.\SQLEXPRESS;Database=<CompanySetup.mdf>;Integrated Security=SSPI"
Try this connection string instead.

Server=.\SQLExpress;AttachDbFilename=C:\Documents and Settings\user\My Documents\CompanySetup.mdf;Database=CompanySetup; Trusted_Connection=Yes;
Avatar of GarySB

ASKER

Hi dbaduck,  I appreciate your help, but it is still not cooperating with the open statement.  I placed the error msg below along with the exact code in my program.  

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

        Dim myConnectionString As String = _
                "Data Source=.Server=.\SQLExpress;AttachDbFilename=C:\Documents and Settings\user\My Documents\CompanySetup.mdf;Database=CompanySetup; Trusted_Connection=Yes;"

        Using connection As New SqlClient.SqlConnection(myConnectionString)
            connection.Open()  'STOPS HERE

            Using command As New SqlClient.SqlCommand("StoredProcedure7", connection)
                command.CommandType = CommandType.StoredProcedure
                ' If you want to pass some parameter
                command.Parameters.Add("@ChangeFlag", SqlDbType.Text).Value = "J"
                command.ExecuteNonQuery()
            End Using
        End Using
ASKER CERTIFIED SOLUTION
Avatar of DBAduck - Ben Miller
DBAduck - Ben Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GarySB

ASKER

Thank you for working this out with me.  Moving it to the local C: drive did the trick.  I also wanted to show you the final connection string that worked.
"Data Source=.\SQLExpress;AttachDbFilename=C:\CompanySetup.mdf;Integrated Security=True;User Instance=True;Trusted_Connection=Yes;"