博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ADO.NET中调用存储过程
阅读量:4981 次
发布时间:2019-06-12

本文共 3846 字,大约阅读时间需要 12 分钟。

写以下几个存储过程:

RegionUpdate: 多个参数

CREATE PROCEDURE RegionUpdate(@RegionID INTEGER, @RegionDescription NCHAR(50))ASSET NOCOUNT OFFUPDATE RegionSET RegionDescription = @RegionDescriptionWHERE RegionID = @RegionIDGO

RegionDelete 一个参数

CREATE PROCEDURE RegionUpdate(@RegionID INTEGER)ASSET NOCOUNT OFFDELETE FROM RegionWHERE RegionID = @RegionIDGO

RegionInsert 带返回参数

CREATE PROCEDURE RegionInsert(@RegionDescription NCHAR(50), @RegionID INTEGER OUTPUT)ASSET NOCOUNT OFFSELECT @RegionID = MAX(RegionID) + 1 FROM RegionINSERT INTO Region VALUES(@RegionID, @RegionDescription)GO

工程添加下面的配置文件

App.config

下面是C# code:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Configuration;using System.Data.SqlClient;namespace SQLProcedureDemo{    class Program    {        private static string GetConnectionStringsConfig(string connectionName)        {            string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();            Console.WriteLine(connectionString);            return connectionString;        }        static void Main(string[] args)        {            string source = GetConnectionStringsConfig("Northwind");            try            {                using (SqlConnection conn = new SqlConnection(source))                {                    conn.Open();                    // Invoke RegionUpdate Procedure                    SqlCommand cmd = new SqlCommand("RegionUpdate", conn);                    cmd.CommandType = CommandType.StoredProcedure;                    SqlParameter updatepara1 = new SqlParameter("@RegionID", SqlDbType.Int);                    updatepara1.Value = 3;                    cmd.Parameters.Add(updatepara1);                    SqlParameter updatepara2 = new SqlParameter("@RegionDescription", SqlDbType.NChar);                    updatepara2.Value = "Northern";                    cmd.Parameters.Add(updatepara2);                    // You can also use the following statement                    //cmd.Parameters.AddWithValue("@RegionID", 3);                    //cmd.Parameters.AddWithValue("@RegionDescription", "Northern");                    cmd.ExecuteNonQuery();                    // Invoke RegionDelete Procedure                                        SqlCommand cmdDel = new SqlCommand("RegionDelete", conn);                    cmdDel.CommandType = CommandType.StoredProcedure;                    SqlParameter myParameter = new SqlParameter("@RegionID", SqlDbType.Int);                    myParameter.Value =5;                    cmdDel.Parameters.Add(myParameter);                    cmdDel.ExecuteNonQuery();                                        // Invoke RegionInsert Procedure                    SqlCommand cmdInsert = new SqlCommand("RegionInsert", conn);                    cmdInsert.CommandType = CommandType.StoredProcedure;                                        SqlParameter para1 = new SqlParameter("@RegionDescription", SqlDbType.NChar);                    para1.Value = "South West";                    cmdInsert.Parameters.Add(para1);                    SqlParameter para2 = new SqlParameter("@RegionID", SqlDbType.Int);                    para2.Direction = ParameterDirection.Output;                    cmdInsert.Parameters.Add(para2);                    cmdInsert.ExecuteNonQuery();                    int newRegionID = (int)cmdInsert.Parameters["@RegionID"].Value;                    Console.WriteLine(newRegionID);                                        conn.Close();                }            }            catch (SqlException ex)            {                //                Console.WriteLine(ex.Message);             }            Console.ReadKey();        }    }}

转载于:https://www.cnblogs.com/fumj/archive/2012/07/15/2592576.html

你可能感兴趣的文章
Travel to all Points 【Codechef】
查看>>
将组件拼装使用
查看>>
spring boot 初识实例及问题小结
查看>>
G面经prepare: set difference
查看>>
蓝牙的key event
查看>>
FtpHelper
查看>>
Opportunity的chance of success的赋值逻辑
查看>>
codevs1228 (dfs序+线段树)
查看>>
关于与后端接口对接,自己总结的几个原则
查看>>
webstorm 格式化代码及常用快捷键
查看>>
适配器模式-Adapter(Java实现)
查看>>
安装phpwind报错
查看>>
Python第五章(北理国家精品课 嵩天等)
查看>>
WPF中设置快捷键
查看>>
vector(C++)讲解
查看>>
Java动态编译
查看>>
[I2C].I2C总线详解
查看>>
计算机网络面试知识总结3
查看>>
数字签名与数字证书以及https
查看>>
Zabbix 监控数据库MSSqlServer
查看>>