现在的位置: 首页 > 综合 > 正文

在C#中使用存储过程(没有输入参数)

2013年10月13日 ⁄ 综合 ⁄ 共 1328字 ⁄ 字号 评论关闭

没有输入参数的存储过程

1首先在SQL Server中使用存储过程

CREATE procedure [dbo].[sp_fuck] 
as 
select Sage,Sname,Ssex 
from Students 
order by Sage,sname

 

2然后创建一个控制台应用程序项目

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();
            }
        }
    }
}

备注:这个是用我电脑的数据库master的Students表!其他表类似

 

抱歉!评论已关闭.