• ADO.NET调用存储过程


    //using (SqlConnection mySqlConnection = new SqlConnection(sqlOpen))
    //{
    // string sqlStr = "usp_pageEach";//存储过程名称
    // using (SqlCommand mySqlCommand = new SqlCommand(sqlStr, mySqlConnection))
    // {
    // mySqlCommand.CommandType = CommandType.StoredProcedure;//设置执行为存储过程,默认是sql语句
    // //设置参数
    // SqlParameter[] mySqlParameter = new SqlParameter[]{
    // new SqlParameter("@pagesize",SqlDbType.Int){Value=pagesize},
    // new SqlParameter("@pageIndex",SqlDbType.Int){Value=pageIndex},
    // new SqlParameter("@recordcount",SqlDbType.Int){Direction=ParameterDirection.Output},//设置该参数为输出参数
    // new SqlParameter("@pagecount",SqlDbType.Int){Direction=ParameterDirection.Output}
    // };
    // //传入参数
    // mySqlCommand.Parameters.AddRange(mySqlParameter);
    // //打开连接
    // mySqlConnection.Open();

    // }
    //}

    例子

    string sqlStr = "usp_pageEach";//存储过程名称
    string sqlOpen = "Data Source=AJV91FQQCCEBZD1;Initial Catalog=战国七雄;Integrated Security=True";
    using (SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(sqlStr, sqlOpen))
    {
    mySqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;//作用就是加上exec,如果存储过程名称里面加了exec则这里可以不用设置
    SqlParameter[] mySqlParameter = new SqlParameter[]{
    new SqlParameter("@pagesize",SqlDbType.Int){Value=3},
    new SqlParameter("@pageIndex",SqlDbType.Int){Value=2},
    new SqlParameter("@recordcount",SqlDbType.Int){Direction=ParameterDirection.Output},//设置该参数为输出参数
    new SqlParameter("@pagecount",SqlDbType.Int){Direction=ParameterDirection.Output}
    };
    mySqlDataAdapter.SelectCommand.Parameters.AddRange(mySqlParameter);
    DataSet myDataSet = new DataSet();
    mySqlDataAdapter.Fill(myDataSet,"table1");
    recordcount =Convert.ToInt32( mySqlParameter[2].Value);//获取输出参数,这该输出参数必须是执行后才能获取的
    pagecount = Convert.ToInt32(mySqlParameter[3].Value);
    return myDataSet.Tables["table1"];
    }

    扩展:

    seletc赋值和set赋值的不同,set赋值时候当值是多个时候会报错。select赋值时候当值是多个时候会取最后一个

  • 相关阅读:
    xtu数据结构 I. A Simple Tree Problem
    图论trainning-part-1 A. 最短路
    xtu数据结构 D. Necklace
    xtu数据结构 G. Count the Colors
    xtu数据结构 B. Get Many Persimmon Trees
    xtu数据结构 C. Ultra-QuickSort
    NYOJ 118 修路方案
    POJ 1679 The Unique MST
    NYOJ 115 城市平乱
    NYOJ 38 布线问题
  • 原文地址:https://www.cnblogs.com/it-xcn/p/5734553.html
Copyright © 2020-2023  润新知