简体   繁体   中英

C# retrieve MS Access Memo Field

I'm using C# (Visual Studio 2015) and communicating with MS Access database. My code below works fine, except that the _statusLadder variable is returning an empty value from a Memo data type field (I run the same query in MS Access and it pulls the correct memo value).

I have also tried:

string _statusLadder = "";

Can someone help me figure out how to retrieve a Memo data type field from MS Access?

See code:

private string retrieveJobByID(int xID)
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;

        command.CommandText = "SELECT TOP 1 [ID], [JOB_NUM], [CUSTOMER], [MODELNO], [CREATE_DATE], [MODEL_FAMILY], [VER], [COM_PROTOCOL], [STATUS_LADDER] FROM tbl_job_tables WHERE([ID] = ?)";
        command.Parameters.Add("@ID", OleDbType.Integer).Value = xID;

        OleDbDataReader reader = command.ExecuteReader();

        string _id = "", _jobnum = "", _customer = "", _modelFamily = "", _modelNum = "", _createDate = "", _ver = "", _comProtocol = "";
        var _statusLadder = "";

        while (reader.Read())
        {
            _id = reader["ID"].ToString();
            _jobnum = reader["JOB_NUM"].ToString();
            _customer = reader["CUSTOMER"].ToString();
            _modelFamily = reader["MODEL_FAMILY"].ToString();
            _modelNum = reader["MODELNO"].ToString();
            _createDate = reader["CREATE_DATE"].ToString();
            _ver = reader["VER"].ToString();
            _comProtocol = reader["COM_PROTOCOL"].ToString();
            _statusLadder = reader["STATUS_LADDER"].ToString();  //<-- This returns empty when it should be ~300 characters.
            Console.WriteLine("Status is: " + _statusLadder);
        }

        if (!reader.IsClosed)
        {
            reader.Close();
        }

        if (connection.State == ConnectionState.Open) { 
            connection.Close();
        }

        string result = _id + "|" + _jobnum + "|" + _customer + "|" + _modelFamily + "|" + _modelNum + "|" + _createDate + "|" + _ver + "|" + _comProtocol + "|" + _statusLadder;


        return result;
    }

EDIT - Here's some pictures of my MS Access 2010 setup (ACCDB):

(1) - The first shows the SQL query. I entered ID=116 as a parameter

(2) - The second picture shows the result from the query. You can see that "STATUS_LADDER" returns the proper value.

(3) - This is the setup of the table (in Design Mode) for the Field "STATUS_LADDER"

在此处输入图片说明

I had no issue reading a memo/long text field from an Access database. Try this code (I simplified it some).

using System.Data.OleDb;   //other standard namespaces needed also.   


public class AccessDataHelper
{
    string myConn;  //value set in this class constructor

     //Constructor
    public AccessDataHelper()
    {
        myConn = System.Configuration.ConfigurationManager.ConnectionStrings["LocalDB"].ToString();
        //If myConn is null, then a real problem.
    }

    public List<KAddress> GetUnvalidatedAddresses()
    {
        List<KAddress> kAddresses = new List<KAddress>();
        string sqlCommandText = String.Empty;

        using (OleDbConnection myOleDbConnection = new OleDbConnection())
        {

            if (myConn != null)
            {
                myOleDbConnection.ConnectionString = myConn;

                sqlCommandText = "SELECT tblAddresses.AddressId, " +
                    "tblAddresses.USPSValidated, tblAddresses.Notes, " +
                    "tblAddresses.Address, tblAddresses.City, tblAddresses.State, tblAddresses.ZIP " +
                    "FROM tblAddresses  " +
                    "WHERE ( ((IsNull(tblAddresses.USPSValidated)) Or ((tblAddresses.USPSValidated) <> 1)) ) ";
                    //Field USPSValidated is an integer 1=USPS valid address

                OleDbCommand oleDbCommand = new OleDbCommand(sqlCommandText, myOleDbConnection);
                oleDbCommand.Connection.Open();
                OleDbDataReader reader = oleDbCommand.ExecuteReader();

                while (reader.Read())
                {
                    KAddress kAddress = new KAddress();

                    kAddress.AddressID = Convert.ToInt32(reader["AddressID"]);
                    kAddress.Notes = reader["Notes"].ToString();    // Notes is a memo,long text field in Access

                    kAddress.Address = reader["Address"].ToString();
                    kAddress.City = reader["City"].ToString();
                    kAddress.State = reader["State"].ToString();
                    kAddress.Zip = reader["Zip"].ToString();

                    kAddresses.Add(kAddress);
                }
            }
        }

        return kAddresses;
    }

} //end of class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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