Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C填充对象_C#_Oop_Dapper - Fatal编程技术网

C# C填充对象

C# C填充对象,c#,oop,dapper,C#,Oop,Dapper,我知道人们会尖叫,这个话题在互联网上到处都是。我知道,我读过。我还是不明白。我只想根据存储过程的结果填充对象。现在,此存储过程至少需要两个参数—一个操作和要查找的内容 @行动 @客户名称 @Action只是确定存储过程需要做什么并返回结果。例如,如果我想更新客户,我会通过我的对象调用sp: public class Customer() { //properties public int UpdateCustomer() { using (SQLConn

我知道人们会尖叫,这个话题在互联网上到处都是。我知道,我读过。我还是不明白。我只想根据存储过程的结果填充对象。现在,此存储过程至少需要两个参数—一个操作和要查找的内容

@行动 @客户名称

@Action只是确定存储过程需要做什么并返回结果。例如,如果我想更新客户,我会通过我的对象调用sp:

public class Customer()
{
    //properties

    public int UpdateCustomer()
    {
        using (SQLConnection connection = new SqlConnection(Helper.CnnVal("DataConnection")))
        {
            SQLCommand = new SqlCommand(Helper.Procedure("Customer"), connection);

            command.CommandType = CommandType.StoredProcedure;

            SqlParameterCollection parameterCollection = command.Parameters;

            parameterCollection.Add("@Action", SqlDbType.NVarChar, -1).Value = "Update"

            //complete the rest of it....
        }
    }
}
这很有效。因此,当我只想用sp的结果填充对象时,问题就出现了。在这种情况下,我会将Retrieve作为@Action参数传递给sp,将this.customer\u name作为@customername参数传递给sp

但是如何将存储过程的结果放入对象中呢

我有这个

    public void GetCustomer()
    {
        using (SQLConnection connection = new SqlConnection(Helper.CnnVal("DataConnection")))
        {
            var retrieval = new DynamicParameters();

            retrieval.Add("@Action", "Retrieve");
            retrieval.Add("@name", this.customer_Name);

            connection.Open():
            connection.Execute(Helper.Procedure("Customer"), retrieval, commandType: CommandType.StoredProcedure);
        }
    }
但我认为它不起作用


很久以前,当我需要填充一个对象时,我常常为PHP运行fetch。我应该回到这里吗?

您需要在命令上执行SqlReader,如下所示:

using (var connection = new SqlConnection("Connection"))
using (var command = new SqlCommand("Retrieve", connection))
{
    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@Action", "Retrieve");
    command.Parameters.AddWithValue("@name", this.customer_Name);

    connection.Open();

    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            var item = new YourClass();
            // You can use GetX() methods to get the value of the fields
            item.Name = reader.GetString("name");
            // You can also access the fields by using bracket notation
            item.Age = (int)reader["age"];

            // Be careful of nullable fields though!
        }
    }
}

使用@Encrypt0r建议和指导,我让它工作起来:

public void GetCustomer() {
            using (SqlConnection connection = new SqlConnection(Helper.CnnVal("DataConnection"))) {
                SqlCommand command = new SqlCommand(Helper.Procedure("Customer"), connection);

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@Action", "Retrieve");
                command.Parameters.AddWithValue("@name", this.customer_name);

                connection.Open();

                using (var reader = command.ExecuteReader()) {
                    while (reader.Read()) {
                        this.tbl_id = (int)reader["tbl_id"];
                        this.customer_name = (string)reader["customer_name"];
                        this.customer_id = reader.GetInt32(customer_id);
                        this.customer_address = (string)reader["customer_address"];
                        this.customer_city = (string)reader["customer_city"];
                        this.customer_state = (string)reader["customer_state"];
                        this.customer_zip = reader.GetInt32(customer_zip);
                    }
                }

用简洁的语言看一下这里的代码。可以根据结果填充模型