本文介绍了如何从C#.net桌面应用程序中的存储过程中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从所需的存储过程中获取数据。 sp将返回一个有4列的表。

如何获取我试用的数据代码



I want to get data from required stored procedure. sp will return me a table having 4 columns.
how to get data i tried following code

var connectionString = ConfigurationManager.ConnectionStrings["cse"].ConnectionString;
           using (SqlConnection conn = new SqlConnection(connectionString))
           using (SqlCommand cmd = conn.CreateCommand())
           {
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.CommandText = "TimeSummary_ForInvoice";

               cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = "2014-05-31"; // dtFrom.Value;
               cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = "2014-06-01";// dtTo.Value;
               cmd.Parameters.Add("@Company", SqlDbType.Int).Value = 2;
               cmd.Parameters.Add("@InvoiceStartNo",SqlDbType.Int).Value=1;
               conn.Open();
               var result = cmd.ExecuteReader();
           }





帮助将会很明显



help will be appreciable

推荐答案

List<ErpPriceColumn > resultList = new List<ErpPriceColumn>();
            IDataReader reader = null;
            SqlConnection dbConnection = null;
            //
            try
            {
                dbConnection = new  SqlConnection(connectionString);   
                IDbCommand dbCommand = new SqlCommand();
                dbCommand.Connection = dbConnection;
                dbCommand.CommandType = CommandType.StoredProcedure;
                dbCommand.CommandText = "TimeSummary_ForInvoice";
                //
                dbConnection.Open();
                //
                reader = dbCommand.ExecuteReader();
                //
                while (reader.Read())
                {
                    ErpPriceColumn erpEntity = new ErpPriceColumn();
                    erpEntity.ID = (int)reader["ID"];
                    erpEntity.Comment = (string)reader["Comment"];
                    erpEntity.BeforeTaxPrice = (0 == (byte)reader["BeforeTaxPrice"]);
                    erpEntity.UpdateDate = (DateTime)reader["UpdateDate"];
                    //
                    resultList.Add(erpEntity);
                }
            }
            catch (SqlException exception)
            {
                throw new MyExceptionClass(exception.Message, exception); //This is good to have!
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                //
                dbConnection.Close();
            }
            //
            return resultList.ToArray();




这篇关于如何从C#.net桌面应用程序中的存储过程中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 01:21