简体   繁体   English

如何从存储过程中返回记录的集合并将结果存入C#代码中?

[英]How to return collection of record from stored procedure and get result in to c# code?

I have stored procedure called uspGetAddress : 我已经存储了名为uspGetAddress过程:

ALTER PROCEDURE [dbo].[uspGetAddress] @sdate dateTime,@edate dateTime
AS
   (select a_code 
    from cust_personal 
    where c_no in (Select c.c_no    
                   from a_basic a     
                   INNER JOIN cust_personal b ON a.a_code = b.a_code 
                   INNER JOIN cust_installment c ON b.c_no = c.c_no  
                   where c.idate BETWEEN @sdate AND @edate))

This procedure returns multiple records 此过程返回多个记录

Example O/P: 示例O / P:

a_code
------    
 10004 
 10002 
 10003 
 10006

How to return these rows from the stored procedure and how to get this returned values in C# code? 如何从存储过程返回这些行,以及如何在C#代码中获取此返回值?

try this : 尝试这个 :

        using (var connection = new SqlConnection("connectionstringHere"))
        {
            var cmd = connection.CreateCommand();
            cmd.CommandText =  "uspGetAddress"
            cmd.Parameters.Add(new SqlParameter() { ParameterName = "@sdate" , SqlDbType = SqlDbType.DateTime , Value = DateTime.Now /* bind your value*/});
            cmd.Parameters.Add(new SqlParameter() { ParameterName = "@edate" , SqlDbType = SqlDbType.DateTime , Value = DateTime.Now /* bind your value*/});
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            var reader = cmd.ExecuteReader();
            while(reader.Read()){
             //do your work, 
            }
            cmd.Connection.Close();
            cmd.Dispose();
            connection.Close();
            connection.Dispose();
        }

If you're using .Net Framework 3.5 or newer try this... 如果您使用的是.Net Framework 3.5或更高版本,请尝试此...

    public class MyAddressObject
    {
        public int a_code { get; set; }
    }

    public static List<MyAddressObject> GetAddresses(DateTime dtStart, DateTime dtEnd)
    {
        #region Create SqlCommand
        SqlCommand cmd;
        cmd = new SqlCommand("[dbo].[uspGetAddress]");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@sdate", dtStart);
        cmd.Parameters.AddWithValue("@edate ", dtEnd);
        #endregion

        #region Talk to the Database
        DataSet objDataSet;
        using (System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection(
                    "My Connection String"))
        {
            cmd.Connection = objConn;
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                objDataSet = new DataSet();
                da.Fill(objDataSet);
            }
        }
        #endregion

        #region Turn the data into a list of MyAddressObjects
        var objResult = from data in objDataSet.Tables[0].AsEnumerable()
                        select new MyAddressObject
                        {
                            a_code = data.Field<int>("a_code")
                        };
        return objResult.ToList();
        #endregion
    }

Now you have a simple List of MyAddressObject that you can do with what you will! 现在,您有了一个简单的MyAddressObject列表,可以使用您将要执行的操作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用c#中的存储过程从数据库获取记录 - get Record from DB using stored procedure in c# 从documentdb存储过程C#获取结果 - Get result from documentdb stored procedure c# 如何获得C#中存储过程的结果? - How can I get the result of a stored procedure in C#? 从C#中的存储过程获取返回值(登录过程) - Get return values from a stored procedure in c# (login process) 如何从从 SQL Server 存储过程获取 JSON 结果的 C# Web API 返回 JSON 结果? - How to return JSON result from C# Web API that gets JSON result from SQL Server Stored Procedure? 如何通过存储过程获取C#中的返回值和行 - How to get the return value and rows in c# thru stored procedure 如何在C#中获取存储过程返回值? - How can I get Stored Procedure return value in C#? 如何使用linq和C#获取存储过程的返回值? - How to get stored procedure return value using linq with C#? 如何从存储过程中获取SQL字符串结果并将其保存在C#Windows应用程序字符串变量中 - How to get SQL String Result from Stored Procedure and save it in C# Windows Application string variable 如何从存储过程中获取输出结果在C#中使用Entity Framework? - How to get output result from stored procedure use Entity Framework in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM