Create a Stored Procedure Stored in SQL Server and call C,

Source: Internet
Author: User
Tags microsoft sql server management studio sql server management sql server management studio

Create a Stored Procedure Stored in SQL Server and call C,

Stored ProcedureSome compiled and optimized SQL statements placed on the database server can be directly called by the program. Using Stored Procedures has the following advantages:
1. Faster execution speed than normal SQL statements
2. Easy Centralized Control
3. reduces network traffic
4. Ensure database security and integrity
5. Flexibility

Create a stored procedure
You can use the Transact-SQL statement to create the Stored Procedure Stored Procedured. In Microsoft SQL Server Management Studio, right-click Databases-> Database Name-> Programmability-> Stored Procedures and choose Stored Procedure to generate a template for creating a Stored Procedure, modify the content and then execute it to create the Stored Procedured.

The following describes how to create a stored procedure for inserting data into a table. For example, my original table is created using the following statement:

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'Persons') AND OBJECTPROPERTY(id,N'isUserTable') = 1) CREATE TABLE Persons (Id_P int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))

The SQL statement used to create the insert function for Stored Procedure is as follows:

USE [databasename]GO/****** Object:  StoredProcedure [dbo].[insert_persions]    Script Date: 2/25/2015 11:14:11 AM ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOIF OBJECT_ID ('insert_persons','p') IS NOT NULL    DROP PROCEDURE insert_persons;GO-- =============================================-- Author:      <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================CREATE PROCEDURE insert_persons    -- Add the parameters for the stored procedure here    @id int,     @lastname varchar(255),    @firstname varchar(255),    @adress varchar(255),    @city varchar(255)ASBEGIN    -- SET NOCOUNT ON added to prevent extra result sets from    -- interfering with SELECT statements.    SET NOCOUNT ON;    -- Insert statements for procedure here    INSERT Persons (Id_P, LastName, FirstName, Address, City)    VALUES (@id, @lastname, @firstname, @adress, @city)ENDGO

After the preceding code is complete, first check whether the Stored Procedure exists. If yes, delete the Stored Procedure first, and then create the Stored Procedure. The function is to insert a record through input parameters. Click Execute or press F5 to Execute the above Code. A Stored Procedure name is insert_persons in the corresponding database. Take SQL Server 2014 as an example. You only need to refresh the corresponding database, and then go to Programmability-> Stored Procedures to find that dbo. insert_persons is added.

Execute the Stored Procedure
Find the corresponding Stored Procedure under Stored Procedures, right-click and choose Execute Stored Procedure... A new dialog box is generated. Enter the input parameters and click OK. When you go back to the corresponding table, you will find that one more row of data exists.

Of course, we can also use SQL statements to execute stored procedures.

EXECUTE insert_persons 5,"Wang","San","Zhongguancun","Beijing"EXECUTE insert_persons @id = 6,@firstname = "Wang",@lastname = "San",@adress = "Zhongguancun",@city = "Beijing"

C # execute the Stored Procedure
The following code demonstrates how to use C # To execute the stored procedure created above:

            String conStr = @"Data Source=host\SQLEXPRESS;Initial Catalog=dbtan;Integrated Security=SSPI";            SqlConnection con = new SqlConnection(conStr);            try            {                con.Open();                Console.WriteLine("Connect Sucess!");                SqlCommand com = new SqlCommand();                com.CommandType = System.Data.CommandType.StoredProcedure;                com.Connection = con;                com.CommandText = "insert_persons";                SqlParameter pId = new SqlParameter("id", 10);                SqlParameter pLastName = new SqlParameter("lastname", "last");                SqlParameter pFirstName = new SqlParameter("firstname", "first");                SqlParameter pAdress = new SqlParameter("adress", "pop");                SqlParameter pCity = new SqlParameter("city", "Hangzhou");                com.Parameters.Add(pId);                com.Parameters.Add(pLastName);                com.Parameters.Add(pFirstName);                com.Parameters.Add(pAdress);                com.Parameters.Add(pCity);                com.ExecuteNonQuery();            }            catch (Exception e)            {                Console.WriteLine(e.ToString());            }            finally            {                con.Close();            }            Console.ReadKey();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.