惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
Last Week in AI
Last Week in AI
V
V2EX
博客园_首页
IT之家
IT之家
Jina AI
Jina AI
博客园 - 叶小钗
The Cloudflare Blog
T
Tailwind CSS Blog
腾讯CDC
B
Blog
D
Docker
L
LangChain Blog
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI

博客园 - aito

在Umbraco中使用XSLTsearch包实现搜索功能 在Umbraco使用Edit in Cavas(实时编辑器)注意两点 在Umbraco中建立连接列表 在空的Umbraco中建立网页 win 7上安装Umbraco Entity Framework基础:新建一个EF应用 利用VS2010安装的SQL Express2008直接建立数据库文件 新建MCV建立测试工程,编译出错的解决办法 VS中各种文件及其扩展名 MVC学习笔记一:基础知识 分页查询SQL语句 ADO.NET事务处理 参数化SQL语句 ADO.NET常用对象 ADO.NET简介 jQuery选择器 插入运行代码 - aito - 博客园 Button控件 - aito - 博客园 CSS选择器
将存储过程用于Command对象
aito · 2010-08-25 · via 博客园 - aito

存储过程提供数据驱动程序很多优点,利用存储过程,数据库操作可以封装在单个命令中,为获取最佳性能而进行优化并通过附加的安全性得到增强。调用存储过程,需要将Command对象的 CommandType设置为StoreProcedure,CommandType一旦设置为StoreProcedure,就可以使用Parameters集合来定义参数。代码所示:

using System;
using System.Data;
using System.Data.SqlClient;

namespace CommandSmp
{

    class Class1
    {

        static void Main(string[] args)
        {
            SqlConnection nwindConn=new SqlConnection("Data Source=LA;Integrated Security=SSPI;Initial Catalog=northwind");
            SqlCommand salesCMD=new SqlCommand("SalesByCategory",nwindConn);
            salesCMD.CommandType=CommandType.StoredProcedure;
            SqlParameter myParm=salesCMD.Parameters.Add("@CategoryName",SqlDbType.NVarChar,15);
            myParm.Value="Beverages";
            nwindConn.Open();

            SqlDataReader myReader=salesCMD.ExecuteReader();
            Console.WriteLine("{0},{1}",myReader.GetName(0),myReader.GetName(1));
            while(myReader.Read())
            {
                Console.WriteLine("{0},${1}",myReader.GetString(0),myReader.GetDecimal(1));
            }
            myReader.Close();
            nwindConn.Close();
        }
    }
}