Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图将FirebirdDB中的邮件合并到Word中,但无法打开数据源_C#_Ms Word_Connection String_Firebird_Mailmerge - Fatal编程技术网

C# 试图将FirebirdDB中的邮件合并到Word中,但无法打开数据源

C# 试图将FirebirdDB中的邮件合并到Word中,但无法打开数据源,c#,ms-word,connection-string,firebird,mailmerge,C#,Ms Word,Connection String,Firebird,Mailmerge,我正在尝试将firebird数据库中的数据邮件合并到word文档中。我有一个表“USERS”,其中包含字段“NAME”、“ADDRESS1”和“CITY” 但是,我无法让word打开数据源。下面的一行 wrdDoc.MailMerge.OpenDataSource(myConnection1.Database, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref

我正在尝试将firebird数据库中的数据邮件合并到word文档中。我有一个表“USERS”,其中包含字段“NAME”、“ADDRESS1”和“CITY”

但是,我无法让word打开数据源。下面的一行

        wrdDoc.MailMerge.OpenDataSource(myConnection1.Database, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oQuery, ref oMissing, ref oFalse, ref oMissing);
正在引发:ProtoLAS.exe中发生类型为“System.Runtime.InteropServices.COMException”的未处理异常

其他信息:Word无法打开数据源

我觉得我什么都试过了。您可以在下面找到完整的表单代码

using FirebirdSql.Data.FirebirdClient;
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 Word = Microsoft.Office.Interop.Word;
namespace ProtoLAS
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
    {
        string connectionString =
            "User=SYSDBA;" +
            "Password=masterkey;" +
            "Database=E:\\data\\LAS.FDB;" +
            "DataSource=localhost;" +
            "Port=3050;" +
            "Dialect=3;" +
            "Charset=NONE;" +
            "Role=;" +
            "Connection lifetime=15;" +
            "Pooling=true;" +
            "MinPoolSize=0;" +
            "MaxPoolSize=50;" +
            "Packet Size=8192;" +
            "ServerType=0";
        FbConnection myConnection1 = new FbConnection(connectionString);
        try
        {
            // Open connection.
            myConnection1.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Word.Application wrdApp;
        Word._Document wrdDoc;
        Object oTemplate = "E:\\Template.docx";
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;
        Object oTrue = true;
        Word.MailMerge wrdMailMerge;

        // Create an instance of Word  and make it visible.
        wrdApp = new Word.Application();
        wrdApp.Visible = true;

        // Create MailMerge Data.                        
        wrdDoc = wrdApp.Documents.Open(ref oTemplate, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        wrdDoc.Select();
        wrdMailMerge = wrdDoc.MailMerge;
        object oQuery = "SELECT NAME,ADDRESS1,CITY from USERS";
        wrdDoc.MailMerge.OpenDataSource(myConnection1.Database, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oQuery, ref oMissing, ref oFalse, ref oMissing);
        wrdMailMerge.SuppressBlankLines = true;

        // Perform mail merge.
        wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
        wrdMailMerge.Execute(ref oFalse);

        // Close the Template document.
        wrdDoc.Saved = true;
        wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        // Release References.            
        wrdMailMerge = null;
        wrdDoc = null;
        wrdApp = null;
        myConnection1.Close();
    }
为什么Word不能打开数据源

编辑:Stacktrace

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2146822366
  HResult=-2146822366
  HelpLink=wdmain11.chm#25402
  Message=Word was unable to open the data source.
  Source=Microsoft Word
  StackTrace:
       at Microsoft.Office.Interop.Word.MailMerge.OpenDataSource(String Name, Object& Format, Object& ConfirmConversions, Object& ReadOnly, Object& LinkToSource, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Connection, Object& SQLStatement, Object& SQLStatement1, Object& OpenExclusive, Object& SubType)
       at ProtoLAS.Main.button1_Click(Object sender, EventArgs e) in C:\Users\nathan\Source\Repos\PrototypeLAS\ProtoLAS\ProtoLAS\Main.cs:line 64
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at ProtoLAS.Program.Main() in C:\Users\nathan\Source\Repos\PrototypeLAS\ProtoLAS\ProtoLAS\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

如果你能负担得起在第三方产品上花费一些钱,我会推荐两种让你的生活更轻松的产品:

  • LLBLGENPRO是一个ORM工具,用于创建Firebird(或许多其他数据库)的数据访问层,这使得在C代码中操作数据变得非常容易
  • Docentric Toolkit是一个用于将MS Word模板与数据合并以创建docx、pdf或xps格式的最终报告的工具包。您可以在MS Word中设计模板,并将其与C#应用程序中的数据合并。它也可以在服务器上运行,并且不需要在该服务器上安装MS Office

  • 我正在非常有效地使用这两种产品。

    删除
    角色=来自连接字符串(我看到其他驱动程序在指定空角色而不是无角色时出现问题)。如果这不能解决问题,那么请发布异常的完整stacktrace。删除角色,仍然获得相同的结果。Stacktrace已添加到post。