码迷,mamicode.com
首页 > 其他好文 > 详细

C#调用存储过程

时间:2014-09-10 12:16:20      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:io   ar   数据   cti   sp   on   c   ad   new   

        protected void btnQuery_Click(object sender, EventArgs e)
        {
                using (SqlConnection con =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "test2";
                        cmd.CommandType = CommandType.StoredProcedure;
                        con.Open();
                        SqlParameter id = new SqlParameter("@id", SqlDbType.TinyInt);
                        id.Value = txtId.Text;
                        id.Direction = ParameterDirection.Input;
                        cmd.Parameters.Add(id);
                        SqlParameter name = new SqlParameter("@name", SqlDbType.VarChar, 20);
                        name.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(name);
                        SqlParameter age = new SqlParameter("@age", SqlDbType.TinyInt);
                        age.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(age);
                       // cmd.ExecuteNonQuery();
                       // lblResult.Text = "姓名:" + name.Value + "\n" + "年龄:" + age.Value;
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            lblResult.Text = "姓名:" + name.Value + "\n" + "年龄:" + age.Value;
                        }
                        else
                        {
                            lblResult.Text = "没有数据";
                        }
 
 
                    }
                }
            }

C#调用存储过程

标签:io   ar   数据   cti   sp   on   c   ad   new   

原文地址:http://www.cnblogs.com/lschenblog/p/3964081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!