Connecting Tech Pros Worldwide Forums | Help | Site Map

C#, Stored procedure

Newbie
 
Join Date: Jul 2008
Posts: 30
#1: Jul 25 '08
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.

insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: Jul 25 '08

re: C#, Stored procedure


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#.
Newbie
 
Join Date: Jul 2008
Posts: 30
#3: Jul 25 '08

re: C#, Stored procedure


Thanks ;]

Maybe you now, how like this, whit Stored Procedure

Expand|Select|Wrap|Line Numbers
  1.                  SqlConnection conn;
  2.                 SqlCommand comm;
  3.  
  4.                 String connectionString =
  5.                 ConfigurationManager.ConnectionStrings[
  6.                 "Smurfas"].ConnectionString;
  7.                 conn = new SqlConnection(connectionString);
  8.                 comm = new SqlCommand(
  9.                 "INSERT INTO Bendra (Name,  DataNow) " +
  10.                 "VALUES (@Name, @Data)", conn);
  11.                 //---Nusiunciam duomenis
  12.                 comm.Parameters.Add("@Name",
  13.                 System.Data.SqlDbType.NVarChar, 15);
  14.                 comm.Parameters["@Numeris"].Value = Name;
  15.  
  16.                 comm.Parameters.Add("@Data",
  17.                 System.Data.SqlDbType.NVarChar, 15);
  18.                 comm.Parameters["@Data"].Value = DataNow;
  19.  
  20.                 try
  21.                 {
  22.                     conn.Open();              
  23.                     comm.ExecuteNonQuery();  
  24.  
  25.                 }
  26.                 catch
  27.                 {
  28.                     error.Text = "Error!";
  29.                 }
  30.                 finally
  31.                 {
  32.  
  33.                     conn.Close();  //
  34.                 }
  35.  
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Jul 25 '08

re: C#, Stored procedure


Are you asking how to write that query as a SP? You should probably get a tutorial on T-SQL stored procedures.
Newbie
 
Join Date: Jul 2008
Posts: 30
#5: Jul 25 '08

re: C#, Stored procedure


Quote:

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
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#6: Jul 25 '08

re: C#, Stored procedure


Quote:

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.
Newbie
 
Join Date: Jul 2008
Posts: 30
#7: Jul 26 '08

re: C#, Stored procedure


Quote:

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.
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#8: Jul 28 '08

re: C#, Stored procedure


Quote:

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:
Expand|Select|Wrap|Line Numbers
  1. USE [Sandbox]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[Test]    Script Date: 07/28/2008 10:31:52 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author:        Tomas Scheel
  10. -- Create date: July 28th, 2008
  11. -- Description:    Sample Procedure
  12. -- =============================================
  13. CREATE PROCEDURE [dbo].[Test] 
  14.     -- Add the parameters for the stored procedure here
  15.     @Name varchar(50) = '', 
  16.     @Data varchar(50) = ''
  17. AS
  18. BEGIN
  19.     -- SET NOCOUNT ON added to prevent extra result sets from
  20.     -- interfering with SELECT statements.
  21.     SET NOCOUNT ON;
  22.  
  23.     -- Insert statements for procedure here
  24.     Insert into dbo.SandboxTable ([Name], [Data]) values (@Name, @Data)
  25. END
  26.  
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
Reply