Results 1 to 7 of 7

Thread: [Resolved]Is VB.NET or C# able to use Store Procedure?

  1. #1

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Post [Resolved]Is VB.NET or C# able to use Store Procedure?

    Hi all,

    I want to build one website that using VB.NET or C#
    but I'm really dont want to use dynamic sql.
    I would to use stored procedure.

    If it possible, can some one give me a simple of code
    that shows how to use stored procedure, how to send a parameter?

    Thanks for all
    Last edited by naruponk; Mar 6th, 2005 at 12:50 AM.

  2. #2
    Lively Member
    Join Date
    Dec 2004
    Posts
    121

    Re: Is VB.NET or C# able to use Store Procedure?

    Try this:

    VB Code:
    1. Dim Conn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;" & _
    2.                                                        "Initial Catalog=databasename")
    3.  
    4. Dim slsCMD As OleDbCommand = New OleDbCommand("SalesByCategory", Conn)
    5. slsCMD.CommandType = CommandType.StoredProcedure
    6.  
    7. Dim myParm As OleDbParameter = slsCMD.Parameters.Add("@CategoryName", OleDbType.VarChar, 15)
    8. myParm.Value = "yourParameterValue"
    9.  
    10. Conn.Open()
    11.  
    12. //DO STUFF
    13.  
    14. Conn.Close()

  3. #3

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Re: Is VB.NET or C# able to use Store Procedure?

    I got an error this line
    Value of type 'String' cannot be converted to 'System.Data.OleDb.OleDbParameter'.
    VB Code:
    1. Dim myParm As OleDbParameter = "slsCMD.Parameters.Add(@MemberName,OleDbType.Bigint, ,1)

    VB Code:
    1. Dim slsCMD As OleDbCommand = New OleDbCommand("SalesByCategory", Conn)
    Is "SalesByCategory" to define a Stored Procedure name?
    Last edited by naruponk; Mar 4th, 2005 at 09:24 PM.

  4. #4
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Is VB.NET or C# able to use Store Procedure?

    VB Code:
    1. Dim myParm As OleDbParameter = [b]"[/b]slsCMD.Parameters.Add(@MemberName,OleDbType.Bigint, ,1)

    you get that error because you have " around the slscmd.Parameters.add....

    VB Code:
    1. Dim Conn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;" & _
    2.                                                        "Initial Catalog=databasename")
    3.  
    4. Dim slsCMD As OleDbCommand = New OleDbCommand("[Insert Stored proc name Here!]", Conn)
    5. slsCMD.CommandType = CommandType.StoredProcedure
    6.  
    7. Dim myParm As OleDbParameter = slsCMD.Parameters.Add("@[Parameter Name Here]", OleDbType.VarChar, 15) ' Change OleDbType.VarChar to the proper type for the param
    8. myParm.Value = "yourParameterValue" ' set the value here
    9.  
    10. Conn.Open()
    11.  
    12. 'Now do a
    13.  
    14. slsCMD.ExecuteScalar ' if one value is returned
    15. 'or
    16. slsCMD.ExecuteNonQuery ' if no values are returned
    17. 'ect...
    18.  
    19. Conn.Close
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  5. #5

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Re: Is VB.NET or C# able to use Store Procedure?

    Ok, below are my code for this thread.
    What's wrong of this code?

    <%@ Import NameSpace="System.Data.OLEDB" %>
    <% Dim Conn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=ServerName;Integrated Security=SSPI;Initial Catalog=DatabaseName")
    Dim slsCMD As OleDbCommand = New OleDbCommand("StoredProcName", Conn)
    slsCMD.CommandType = "CommandType.StoredProcedure"
    Dim myParm As OleDbParameter = slsCMD.Parameters.Add("@MemberName",OleDbType.Bigint, ,1)
    myParm.Value = 1
    Conn.Open()
    Conn.Close()
    %>

    Thanks

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Is VB.NET or C# able to use Store Procedure?

    Quote Originally Posted by naruponk
    Ok, below are my code for this thread.
    What's wrong of this code?

    <%@ Import NameSpace="System.Data.OLEDB" %>
    <% Dim Conn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=ServerName;Integrated Security=SSPI;Initial Catalog=DatabaseName")
    Dim slsCMD As OleDbCommand = New OleDbCommand("StoredProcName", Conn)
    slsCMD.CommandType = "CommandType.StoredProcedure"
    Dim myParm As OleDbParameter = slsCMD.Parameters.Add("@MemberName",OleDbType.Bigint, ,1)
    myParm.Value = 1
    Conn.Open()
    Conn.Close()
    %>

    Thanks

    ok make your code like this one.
    VB Code:
    1. Dim Conn As New OleDbConnection("Provider=SQLOLEDB;Data Source=ServerName;Integrated Security=SSPI;Initial Catalog=DatabaseName")
    2. Dim slsCMD As New OleDbCommand()
    3. slsCMD.CommandText = "StoredProcName"
    4. slsCMD.CommandType = CommandType.StoredProcedure
    5. slsCMD.Connection = Conn
    6. slsCMD.Parameters.Add("@MemberName", textbox1.text)
    7. Conn.Open()
    8. slsCMD.executenonquery
    9. Conn.Close()

  7. #7

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Re: Is VB.NET or C# able to use Store Procedure?

    OK, Resolved!
    Thanks for all help

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