C#, Stored procedure | Newbie | | Join Date: Jul 2008
Posts: 30
| | |
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.
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,608
| | | 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
| | | re: C#, Stored procedure
Thanks ;]
Maybe you now, how like this, whit Stored Procedure -
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(); //
-
}
-
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,608
| | | 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
| | | 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
|  | Expert | | Join Date: Apr 2007 Location: Iowa
Posts: 624
| | | 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
| | | 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.
|  | Expert | | Join Date: Apr 2007 Location: Iowa
Posts: 624
| | | 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: -
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 |  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|