第三十讲 : 综合应用&存储过程 返回值参数实例
2014-04-16 00:02
363 查看
建个表先:
CREATE TABLE [dbo].[Blogs]( [blog_id] [int] IDENTITY(1,1) NOT NULL,--博客ID [blog_name] [varchar](50) NOT NULL, --博客标题 [blog_content] [text] NOT NULL --博客内容 )插些数据进去后,创建一个存储过程:
create proc ReturnValue ( @id int, --查寻条件 @name varchar(100) output --要返回去的 ) as select @name =(select blog_name from Blogs where blog_id=@id) go测试原码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace ReturnValue { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //连接字符串 string strConn = "server=.; database=hrms; uid=sa; pwd=123123"; string str = "select blog_id,blog_name,blog_content from [Blogs]"; private void Form1_Load(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(strConn)) { SqlCommand cmd = new SqlCommand(str, conn); cmd.CommandType = CommandType.Text; conn.Open(); SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (sdr.Read()) { ListViewItem lvi = new ListViewItem(sdr["blog_id"].ToString()); lvi.SubItems.Add(sdr["blog_name"].ToString()); lvi.SubItems.Add(sdr["blog_content"].ToString()); listView1.Items.Add(lvi); } sdr.Close(); } } /// <summary> /// 测试返回值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { int id=0; try { id = Convert.ToInt32(tbValue.Text.Trim()); } catch (Exception ex) { MessageBox.Show(ex.Message); } using (SqlConnection conn = new SqlConnection(strConn)) { SqlCommand cmd = new SqlCommand("ReturnValue", conn); cmd.CommandType = CommandType.StoredProcedure; conn.Open(); cmd.Parameters.AddWithValue("@id",id); // 下面的是定义一个返回值变量 SqlParameter name = new SqlParameter("@name",SqlDbType.VarChar,50); name.Direction = ParameterDirection.Output; cmd.Parameters.Add(name); cmd.ExecuteNonQuery(); lblReturn.Text = cmd.Parameters["@name"].Value.ToString(); } } /// <summary> /// 用户单击时,将自动ID填写到查寻条件框里 /// 多些一举不?呵呵,因为我实在不想手动输入 /// 你可以手输的 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { foreach(ListViewItem lvi in listView1.SelectedItems) { tbValue.Text = lvi.Text; } } } } }结果如图:
相关文章推荐
- 第三十讲 : 综合应用&存储过程
- C#调用存储过程 之返回值与输出参数的区别与用法实例
- DB2 存储过程应用(返回值和输出参数应用)
- 在Sqlserver里获取存储过程返回值和带入参数实例
- 系统架构-性能篇章1(应用系统性能2-OOM&参数配置)
- 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值
- UISegmentedControl ,UIStepper,UISwitch,UISlider综合应用(实例需求)
- LinQ综合应用实例
- Oracle Tablespace & Datafiles--Oracle表空间--应用实例(1)
- 23种设计模式综合实例应用目录
- 【思科】ACL综合实例应用
- SQL Server 2000 元数据应用(三):获得数据库中存储过程的参数和数据集字段
- C++中引用(&)的用法和应用实例
- C++中引用(&)的用法和应用实例
- C++中引用(&)的用法和应用实例
- 线性表的综合应用实例
- mysql高级 存储过程[2] ~之 传入的参数 && 控制结构
- JsonResult&FileResult应用实例
- 朴素贝叶斯算法 & 应用实例
- C++ 容器的综合应用的一个简单实例——文本查询程序