我使用C#(Visual Studio 2015)并与MS Access数据库通信。我的代码工作正常,只是_statusLadder
变量从Memo数据类型字段返回一个空值(我在MS Access中运行相同的查询,它会提取正确的备忘录值)。
我也尝试过:
string _statusLadder = "";
有人可以帮我弄清楚如何从MS Access中检索备注数据类型字段吗?
参见代码:
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;
}
编辑 - 这是我的MS Access 2010设置(ACCDB)的一些照片:
(1) - 第一个显示SQL查询。我输入ID = 116作为参数
(2) - 第二张图显示了查询的结果。你可以看到&#34; STATUS_LADDER&#34;返回正确的值。
(3) - 这是Field&#34; STATUS_LADDER&#34;
的表格(在设计模式中)的设置答案 0 :(得分:0)
从Access数据库读取备忘录/长文本字段没有问题。试试这个代码(我简化了一些)。
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