zoukankan      html  css  js  c++  java
  • C# 后台调用存储过程

    例一丶返回集合

          [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 "";
                }
            }
    作者:chenze
    出处:https://www.cnblogs.com/chenze-Index/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    致虚极守静笃
    DNS 透明代理
    Java“禁止”泛型数组
    Java和C#语法对比
    JVM 内存区域 (运行时数据区域)
    Java8 使用
    G1收集器的收集原理
    BZOJ 2222: [Cqoi2006]猜数游戏【神奇的做法,傻逼题,猜结论】
    数据结构之网络流入门(Network Flow)简单小节
    BZOJ 1257: [CQOI2007]余数之和sum【神奇的做法,思维题】
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/10333788.html
Copyright © 2011-2022 走看看