0

这是我第一次尝试连接到 AS400 系统,但在某处我失败了。下面是我根据一些示例应用程序整理的当前代码。目前我的代码失败并recordList.Open()显示消息:

Execption Source: Microsoft OLE DB Provider for ODBC Drivers
Message: ODBC driver does not suppor tthe requested properties.
Data: System.Collections.ListDIctionaryInternal

我正在尝试查询 AS400,并使用我的结果来填充 Windows 窗体 ListView 控件。显然我忽略了或做错了什么。有人可以在我搞砸的这种事情上获得更多经验吗?

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;


namespace MemberRecordChange
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.dtpDate.Value = DateTime.Now;
            cmbLetterType.Items.Add("Address Change (old address)(Active/Inactive member)");
            cmbLetterType.Items.Add("Address Change (old address)(Benefit Recipeint)");
            cmbLetterType.Items.Add("Address Change (new address)(Active/Inactive member)");
            cmbLetterType.Items.Add("Address Change (new address)(Benefit Recipeint)");
            cmbLetterType.Items.Add("Name Change Letter");
            cmbLetterType.Items.Add("Welcome Letter");
            cmbLetterType.Items.Add("Nomination of Beneficiary Form");

            //~~~~~~~~~~~~This is for debugging purposes. I have other code between this step and the function being called below, but first I need to fill the ListView control~~~~~~~~~~~~~~~~~~~~~~~~~~~
            GetiSeriesData("test", "test", "MY-FULL-QUERY");
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            .......
        }

        public void GetiSeriesData(string docLoc, string docSource, string query)
        {
            DataTable addresses = new DataTable();
            DataTable changes = new DataTable();

            string connString = string.Format("DRIVER=Client Access ODBC Driver (32-bit); SYSTEM=XX.XX.X.XX; UID=XXXXX; PWD=XXXXX");

            ADODB.Connection conn = new ADODB.Connection();
            ADODB.Recordset recordList = new ADODB.Recordset();
            OdbcDataAdapter da = new OdbcDataAdapter();
            ADODB.Command cmd = new ADODB.Command();

            string memName = "";
            string startTime = this.dtpDate.Value.ToString("yyyy-MM-dd") + "-00.00.00.000000";
            string endTime = this.dtpDate.Value.ToString("yyyy-MM-dd") + "-23.59.00.000000";

            try
            {
                conn.Open(connString, null, null, 0);
                //????
                cmd.ActiveConnection = conn;
                cmd.CommandText = query;
                //addresses.Clear();
                //da.Fill(addresses);


                // EXCEPTION THROWN ON BELOW LINE

                recordList.Open(query, conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 1);



                // CODE EXECUTION DOES NOT REACH THE BELOW

                if (!recordList.EOF)
                {
                    memName = recordList.Fields["NAME"].Value.ToString();
                    lvData.View = View.Details;
                    Int16 x = 0;
                    while (!recordList.EOF)
                    {
                        lvData.Items.Add(recordList.Fields["MEMNO"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["NAME"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["ADDR1"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["ADDR2"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["CITY"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["STATE"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["ZIP"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["OLD_ADDR1"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["OLD_ADDR2"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["OLD_CITY"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["OLD_STATE"].Value.ToString().Trim());
                        lvData.Items[x].SubItems.Add(recordList.Fields["OLD_ZIP"].Value.ToString().Trim());
                        x += 1;

                        recordList.MoveNext();
                    }
                }
                else
                {
                    MessageBox.Show("No records found");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
            }
            finally
            {
                recordList.Close();
                conn.Close();
            }
        }
    }
}

编辑:

想通了它的一部分,还没有让 .SubItems 出现在控件中:

 string connString = "DRIVER=Client Access ODBC Driver (32-bit); SYSTEM=XX.XX.X.XX; UID=XXXX; PWD=XXXX";

            OdbcConnection conn = new OdbcConnection();
            conn.ConnectionString = connString;
            OdbcCommand cmd = new OdbcCommand(query, conn);
            conn.Open();
            OdbcDataReader dr = cmd.ExecuteReader();

            if (!dr.HasRows)
            {
                throw new Exception("No records found.");
            }

            Int16 x = 0;
            while (dr.Read())
            {
                lvData.Items.Add(dr["memno"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["NAME"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["ADDR1"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["ADDR2"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["CITY"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["STATE"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["ZIP"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["OLD_ADDR1"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["OLD_ADDR2"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["OLD_CITY"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["OLD_STATE"].ToString().Trim());
                lvData.Items[x].SubItems.Add(dr["OLD_ZIP"].ToString().Trim());
            x += 1;
            }
4

0 回答 0