简体   繁体   English

oledb检索整个记录并从.accdb返回

[英]oledb retrieve entire record and return from .accdb

example of a return function that returns only string value, how do I return multiple values consisting of different data types in a single record by simply calling one function? 仅返回字符串值的返回函数示例,如何通过简单地调用一个函数在单个记录中返回由不同数据类型组成的多个值?

    public static string selectPassword(string user)
    {
        using (var connection = new OleDbConnection(connectionString))
        using (var command = connection.CreateCommand())
        {
            command.Parameters.AddWithValue("@user", user);
            command.CommandText = "SELECT [Password] FROM [Password_Table] WHERE Password_ID = [@user]";
            command.CommandType = CommandType.Text;

            connection.Open();

            var value = command.ExecuteScalar();

            return value == DBNull.Value ? null : value.ToString();
        }
    }

my record would be searched by Participant_Name, and would need to return Participant_Name, Participant_ID, Address, Contact_Number & Gender fields, all consisting of string, integers etc.. 我的记录将由Participant_Name搜索,并且需要返回Participant_Name,Participant_ID,地址,Contact_Number和Gender字段,这些字段均由字符串,整数等组成。

Create a data-type which consists of fields and properties that are able to hold the information that you want to retrieve. 创建一个由字段和属性组成的数据类型,这些字段和属性可以保存要检索的信息。

Populate an instance of that type in your method, and return it. 在您的方法中填充该类型的实例,然后将其返回。

Something like this, for instance: 像这样的东西:

public class Participant
{
   public int Id { get; private set; }
   public string Name {get; set;}
   public string Address {get; set; }


   public Participant( int id, string name, string address )
   {
       this.Id = id;
       this.Name = name;
       this.Address = address;
   }
}

public Participant GetParticipant( string name )
{
    using( var conn = new OleDbConnection (connectionString) )
    {
        using( var cmd = connection.CreateCommand() )
        {
              command.CommandText = "SELECT [Id], [Name], [Address] FROM Participant WHERE [name] LIKE @p_name";


               command.Parameters.Add ("@p_name", OleDbType.VarChar).Value = name + "%";

               using( var reader = command.ExecuteReader() )
               {
                    if( !reader.HasRows() ) return null;

                    reader.Read();

                    return new Participant (reader.GetString("Id"), reader.GetString("name"), reader.GetString("address"));
               }
        }
    }

}

Note: there can be syntax errors, since I haven't pulled it through the compiler, but I think you'll catch the drift. 注意:可能会出现语法错误,因为我还没有通过编译器来提取它,但是我认为您会遇到麻烦。

for your needs you should return a DataRow object or an object array which you can get by calling .ItemArray on a DataRow 根据您的需要,应该返回一个DataRow对象或对象数组,可以通过在DataRow上调用.ItemArray获得

what you need to change in your method above is to use a DataAdapter and call its Fill method to fill a DataTable then return the first row of such DataTable, if any row is present. 您需要在上面的方法中进行的更改是使用DataAdapter并调用其Fill方法填充DataTable,然后返回该DataTable的第一行(如果存在任何行)。

you could also do this with the DataReader but then you should construct the array or objects container to return by yourself... I think dataRow.ItemArray is faster to code. 您也可以使用DataReader进行此操作,但是随后您应该构造数组或对象容器以自行返回...我认为dataRow.ItemArray的编码速度更快。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM