C#, Stored procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Smurfas
    New Member
    • Jul 2008
    • 30

    C#, Stored procedure

    Hi,
    what's is Stored Procedure? Why use?

    Maybe have simple example, who send information to SQL DB.


    O long use google, bet found many information, and don't now which information is correct. Found examples but all hard. I don't understand.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    A stored procedure is a procedure created and stored on the database side. It is useful, because you can condense complex and commonly used queries into a single statement. SPs really have nothing to do with C# per se, but with the database. If you choose to store your commonly used queries and procedures as SPs, you can still call them from C#.

    Comment

    • Smurfas
      New Member
      • Jul 2008
      • 30

      #3
      Thanks ;]

      Maybe you now, how like this, whit Stored Procedure

      Code:
                       SqlConnection conn;
                      SqlCommand comm;
                   
                      String connectionString =
                      ConfigurationManager.ConnectionStrings[
                      "Smurfas"].ConnectionString;
                      conn = new SqlConnection(connectionString);
                      comm = new SqlCommand(
                      "INSERT INTO Bendra (Name,  DataNow) " +
                      "VALUES (@Name, @Data)", conn);
                      //---Nusiunciam duomenis
                      comm.Parameters.Add("@Name",
                      System.Data.SqlDbType.NVarChar, 15);
                      comm.Parameters["@Numeris"].Value = Name;
      
                      comm.Parameters.Add("@Data",
                      System.Data.SqlDbType.NVarChar, 15);
                      comm.Parameters["@Data"].Value = DataNow;
                                      
                      try
                      {
                          conn.Open();              
                          comm.ExecuteNonQuery();  
                          
                      }
                      catch
                      {
                          error.Text = "Error!";
                      }
                      finally
                      {
      
                          conn.Close();  //
                      }

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Are you asking how to write that query as a SP? You should probably get a tutorial on T-SQL stored procedures.

        Comment

        • Smurfas
          New Member
          • Jul 2008
          • 30

          #5
          Originally posted by insertAlias
          Are you asking how to write that query as a SP? You should probably get a tutorial on T-SQL stored procedures.
          "Are you asking how to write that query as a SP? " - Yes

          Comment

          • TRScheel
            Recognized Expert Contributor
            • Apr 2007
            • 638

            #6
            Originally posted by Smurfas
            "Are you asking how to write that query as a SP? " - Yes
            How much SQL do you know? Depending on your knowledge will determine where I would start showing you how.

            Comment

            • Smurfas
              New Member
              • Jul 2008
              • 30

              #7
              Originally posted by TRScheel
              How much SQL do you know? Depending on your knowledge will determine where I would start showing you how.
              I just need simpla example...

              I can put information in DB, get information for DB, update, delete. (without SP)
              But about Stored Procedures I don't know near nothing.

              Comment

              • TRScheel
                Recognized Expert Contributor
                • Apr 2007
                • 638

                #8
                Originally posted by Smurfas
                I just need simpla example...

                I can put information in DB, get information for DB, update, delete. (without SP)
                But about Stored Procedures I don't know near nothing.
                Should be something like:
                Code:
                USE [Sandbox]
                GO
                /****** Object:  StoredProcedure [dbo].[Test]    Script Date: 07/28/2008 10:31:52 ******/
                SET ANSI_NULLS ON
                GO
                SET QUOTED_IDENTIFIER ON
                GO
                -- =============================================
                -- Author:		Tomas Scheel
                -- Create date: July 28th, 2008
                -- Description:	Sample Procedure
                -- =============================================
                CREATE PROCEDURE [dbo].[Test] 
                	-- Add the parameters for the stored procedure here
                	@Name varchar(50) = '', 
                	@Data varchar(50) = ''
                AS
                BEGIN
                	-- SET NOCOUNT ON added to prevent extra result sets from
                	-- interfering with SELECT statements.
                	SET NOCOUNT ON;
                
                    -- Insert statements for procedure here
                	Insert into dbo.SandboxTable ([Name], [Data]) values (@Name, @Data)
                END
                For more information take a look at this MSDN link:
                MSDN - How to create a stored procedure

                I would also suggest downloading Microsoft SQL Server Management Studio Express to play in as it will help you double check your code.
                Microsoft SQL Server Management Studio Express

                Comment

                Working...