调用存储过程

来源:互联网 发布:儿童注意力训练软件 编辑:程序博客网 时间:2024/06/05 07:02

using (SqlConnection ection = new SqlConnection(connString))

{
         try
        {
                //创建存储过程字符串
                string sql="proc_zhuce2";
                //创建command对象
                SqlCommand command = new SqlCommand(sql, ection);
                //设置参数类型
                command.CommandType = CommandType.StoredProcedure;
                //添加输入
                command.Parameters.AddWithValue("@name", textBox3.Text);
                command.Parameters.AddWithValue("@img", bytes);
                command.Parameters.AddWithValue("@money", Convert.ToInt32(textBox4.Text));
                //添加输出参数
                SqlParameter p = new SqlParameter("@count", SqlDbType.Int);
                p.Direction = ParameterDirection.Output;
                command.Parameters.Add(p);
                ection.Open();
                command.ExecuteNonQuery();
                int a=Convert.ToInt32(command.Parameters["@count"].SqlValue.ToString());
                if (a == 0)
                {
                    MessageBox.Show("帐号已经存在");
                }
                else
                {
                    string he = string.Format("你的帐号是{0}", a);
                    MessageBox.Show(he);
                }
                textBox1.Text = a.ToString();
         }
          catch (Exception ex)
          {
                MessageBox.Show(ex.Message.ToString());
          }
}
0 0
原创粉丝点击