首页 > Windows开发 > 详细

C# 后台调用存储过程

时间:2019-01-29 14:41:33      阅读:184      评论:0      收藏:0      [点我收藏+]

例一丶返回集合

      [WebMethod]
        public object RegisterMethod(string type, string username, string password, string devicecode)
        {
            string connectString = System.Configuration.ConfigurationSettings.AppSettings["connStr"];
            SqlConnection conn = new SqlConnection(connectString);
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "Proc_Register";
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            List<SqlParameter> list = new List<SqlParameter>()
                {
                    new SqlParameter("@type",SqlDbType.VarChar),
                    new SqlParameter("@username",SqlDbType.VarChar),
                    new SqlParameter("@password",SqlDbType.VarChar),
                    new SqlParameter("@devicecode",SqlDbType.VarChar)
                };
            list[0].Value = type;
            list[1].Value = username;
            list[2].Value = password;
            list[3].Value = devicecode;
            //list[3].Direction = ParameterDirection.Output;
            comm.Parameters.AddRange(list.ToArray());
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            //必须在Fill之后调用
            var result = ds.Tables[0].Rows[0][0].ToString();
            return result;
        }

 

例二丶执行查询,并返回由查询返回的结果集中的第一行的第一列【ExecuteScalar】

        public object RegisterMethod2(string type, string username, string password, string devicecode)
        {
            try
            {
                string connectString = System.Configuration.ConfigurationSettings.AppSettings["connStr"];
                SqlConnection conn = new SqlConnection(connectString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Proc_Register";
                comm.CommandType = System.Data.CommandType.StoredProcedure;
                //传值以及赋值  
                SqlParameter[] sps = new SqlParameter[] {
                    new SqlParameter("@type",type),
                    new SqlParameter("@username",username),
                    new SqlParameter("@password",password),
                    new SqlParameter("@devicecode",devicecode)
                  };
                comm.Parameters.AddRange(sps);
                object Result1 = comm.ExecuteScalar();
                return Result1;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

 

C# 后台调用存储过程

原文:https://www.cnblogs.com/chenze-Index/p/10333788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!