1

这对我来说有点奇怪。

我有以下代码:

      public static object[,] dbMultipleSingleQueries(string strDBPath, object[] oSQL)
      {
       //This is needed for attaching a DB.
       object[,] objOut;
       objOut = new Object[1, 1];
       try
       {

        //Prepare DB connection string      
        strDBPath = "Data Source=" +
              @strDBPath +
              ";Version=3";

        //SQL Objs
        SQLiteConnection dbConnection = new SQLiteConnection(strDBPath);
        SQLiteCommand dbCommand = null;
        SQLiteDataAdapter dbAdapter = null;
        DataTable table = new DataTable();
        //For looping
        int j = 0;
        //Conn str
        dbConnection.Open();
        dbCommand = new SQLiteCommand(dbConnection);
        while (j < oSQL.Length)
        {
         try
         {
          dbCommand.CommandText = oSQL[j].ToString();
          dbCommand.CommandType = System.Data.CommandType.Text;
          if (j == (oSQL.Length - 1))
          {
           dbAdapter = new SQLiteDataAdapter(dbCommand);
           dbAdapter.Fill(table);
           objOut = table2Array(table);
          }
          else
          {

           dbCommand.ExecuteNonQuery();
           objOut[0, 0] = "Success!";
          }
         }
         catch (Exception e2)
         {
          System.Console.WriteLine(e2.StackTrace.ToString());
          objOut[0, 0] = e2.StackTrace.ToString();
         }
         j++;
        };
        dbConnection.Close();
        dbConnection = null;

       }
       catch (Exception e)
       {
        objOut[0, 0] = e.StackTrace.ToString();
       }
       return objOut;

      }

如果我从这样的电子表格中提供我的论点,这将非常有效:

    o = Application.Run("dbMultipleSingleQueries", "20130201.db", [test])

其中 [test] 是我的工作簿中的一个范围,其中包含有效的查询,看起来像这样:

attach database 'a.db' as d1

select * from d1.main

现在,如果我在 vba 中执行以下操作

    Option Explicit
    Sub Main()

    Dim sql(1 To 2, 1 To 1)

    sql(1, 1) = "attach database 'a.db' as d1"
    sql(2, 1) = "select * from d1.main"

    o = Application.Run("dbMultipleSingleQueries", "20130201.db", sql)

    End Sub

这不起作用。我收到“类型不匹配”错误。任何输入为什么会是一个巨大的帮助,这让我发疯!

4

1 回答 1

1

您的函数原型指定第二个参数必须是“Object []”类型 - 但是您传递的变量可能会根据分配认为它是一个字符串数组。

于 2013-02-05T03:41:32.400 回答