C# 后台调用存储过程

2021-06-17 09:04

阅读:562

标签:ddr   tab   turn   command   exce   esc   ext   arch   set   

例一丶返回集合

      [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 list = new List()
                {
                    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# 后台调用存储过程

标签:ddr   tab   turn   command   exce   esc   ext   arch   set   

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


评论


亲,登录后才可以留言!