Use stored procedures in C # (no parameters are input)

Source: Internet
Author: User

Stored Procedure without Parameters

1. Use stored procedures in SQL Server

Create procedure [DBO]. [sp_fuck]
As
Select sage, sname, ssex
From students
Order by sage, sname

 

2. Create a console application project.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data;
Using system. Data. sqlclient;

Namespace callsp1
{
Class Program
{
Static void main (string [] ARGs)
{
// Create connection
Sqlconnection conn = new sqlconnection
(@ "Server =./wb_james;
Integrated Security = sspi;
Database = Master"
);
Try
{
Conn. open (); // open connection
Sqlcommand cmd = conn. createcommand (); // CREATE Command;
Cmd. commandtype = commandtype. storedprocedure;
Cmd. commandtext = "sp_fuck ";
Sqldatareader RDR = cmd. executereader (); // Execute Command;
While (RDR. Read ())
{
Console. writeline (
"{0} {1} {2 }"
, RDR [0]. tostring (). padright (5), RDR [1]. tostring (), RDR [2]. tostring ());

}
RDR. Close ();
}
Catch (sqlexception ex)
{
Console. writeline (ex. tostring ());
}
Finally
{
Conn. Close ();
}
}
}
}

Note: This is the students table of the master database on my computer! Other tables are similar

 

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.