使用数据库中的数据填充Outlook功能区中的comboBox

时间:2019-04-03 13:09:12

标签: c# vsto ribbon

我已经创建了一个带有按钮的功能区,用于将Outlook附件保存到本地磁盘,并且效果很好。现在,我想将一个comboBox添加到从ms sql数据库获取其值的功能区。我真的找不到很好的例子来描述我到底需要做什么。

我试图给我们提供一个comboBox和一个Menu,但是我遇到的所有示例始终显示编程错误是VS。

我试图遵循本指南,但问题多于答案。

https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/vsto/walkthrough-updating-the-controls-on-a-ribbon-at-run-time.md

到目前为止,这是我发现的,但是我想这是针对Web表单或类似程序的,但是它应该不比这更难,或者?

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {
        string connetionString = null;
        SqlConnection connection;
        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        int i = 0;
        string sql = null;
        connetionString = "Data Source=.;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
        sql = "select au_id,au_lname from authors";
        connection = new SqlConnection(connetionString);
        try
        {
            connection.Open();
            command = new SqlCommand(sql, connection);
            adapter.SelectCommand = command;
            adapter.Fill(ds);
            adapter.Dispose();
            command.Dispose();
            connection.Close();

            comboBox1.DataSource = ds.Tables[0];
            comboBox1.ValueMember = "au_id";
            comboBox1.DisplayMember = "au_lname";
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection ! ");
        }
    }

我链接到的指南在代码中给了我,但是有很多代码错误。我已经添加了所有使用要求,但无济于事。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.Outlook;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookAddIn1
{


  public partial class Ribbon1
  {

    //<Snippet3>
    private RibbonDropDownItem CreateRibbonDropDownItem()
    {
        return this.Factory.CreateRibbonDropDownItem();
    }

    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {
        //<Snippet2>
        //Declare the Northwind dataset.
        Ribbon_Update_At_Runtime.Northwind40DataSetTableAdapters.Northwind40DataSet nwDataSet = new Northwind40DataSet();

        //Declare the data tables.

        Northwind40DataSet.CustomersDataTable customerTable;
        Northwind40DataSet.OrdersDataTable orderTable;
        Northwind40DataSet.Order_DetailsDataTable orderDetailsTable;
        Northwind40DataSet.ProductsDataTable productsTable;

        //Declare the data table adapters for each table.

        CustomersTableAdapter customerTableAdapter = new CustomersTableAdapter();
        OrdersTableAdapter ordersTableAdapter = new OrdersTableAdapter();
        Order_DetailsTableAdapter detailsTableAdapter = new Order_DetailsTableAdapter();
        ProductsTableAdapter productsTableAdapter = new ProductsTableAdapter();
        //</Snippet2>


        customerTable = nwDataSet.Customers;
        customerTableAdapter.Fill(customerTable);

        var customerQuery = from customers in customerTable.AsEnumerable().Take(20)
                            select new
                            {
                                CustomerID = customers.Field<string>("Customer ID"),
                                CustomerName = customers.Field<string>("Contact Name")
                            };


        // Execute the query.
        foreach (var item in customerQuery)
        {
            this.comboBox1.Items.Add(CreateRibbonDropDownItem());
            this.comboBox1.Items.Last().Label = item.CustomerName + "|" + item.CustomerID.ToString();
        }
        this.comboBox1.Text = this.comboBox1.Items.First().Label;

    }
  }
}

0 个答案:

没有答案