how to use stored procedure to display the whole table to a Datagrid from sql server

Answered how to use stored procedure to display the whole table to a Datagrid from sql server

  • Tuesday, June 12, 2012 4:31 PM
     
     

    Hi all,

    I want to define a stored procedure in sql server 2008 so that it can be called from my c# code and display the table in a Data grid . so far i was doig with sending a query statement like select * from Tablename after creating the necessary connections  and data adapters fill method. i want to replace it with a stored procedure to avoid repeated connection overhead

    regards

    Iqbal


    itismeiqbal

All Replies

  • Tuesday, June 12, 2012 5:30 PM
     
     Answered Has Code

    Iqbal,

    A stored procedure is stored in SQL Server. So you will not be able to use it to define your data access methods !!

    A stored procedure is a set of SQL statements. It simplifies the "select * from tablename" part, but cannot replace the connection setting. A stored procedure may accept arguments, which you provide in the calling code.

    Using myConnexion As SqlConnection = New SqlConnection(myConnexionString)
    
         Using cmd As SqlCommand = New SqlCommand()
            cmd.Connection = myConnexion
            cmd.CommandType = CommandType.StoredProcedure
            myConnexion.Open()
            cmd.CommandText = "NameOfStoredProcedure"
            cmd.Parameters.Clear()
            cmd.Parameters.Add(New SqlParameter("nameOfParameter", SqlDbType.Int)).Value = valueOfParameter                        
            cmd.ExecuteNonQuery()
               myConnexion.Close()
          End Using
    End Using


    • Edited by Sygrien Tuesday, June 12, 2012 5:30 PM
    • Proposed As Answer by Elmozamil Elamir Wednesday, June 13, 2012 5:33 AM
    • Marked As Answer by iqbal1980 Wednesday, June 13, 2012 2:00 PM
    •