码迷,mamicode.com
首页 > Windows程序 > 详细

C# 后台调用存储过程

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

标签: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<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# 后台调用存储过程

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

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

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