Fill ListView control with ADODB.Connection Query Results?

巧了我就是萌 提交于 2019-12-25 02:43:37

问题


This is my first time trying to connect to a AS400 system, and somewhere along the lines I am failing. Below is my current code I have put together based off a few sample applications. Currently my code is failing at recordList.Open() with message:

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

I'm attempting to query the AS400, and use my results to fill a Windows Forms ListView control. Clearly I am overlooking or doing something wrong. Can someone more experience in this sort of thing spot where I'm messing up?

CODE:

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();
            }
        }
    }
}

EDIT:

Figured part of it out, haven't gotten the .SubItems to show up in the control yet:

 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;
            }

来源:https://stackoverflow.com/questions/19868926/fill-listview-control-with-adodb-connection-query-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!