如何执行简单的 C# 导入

本文关键字:简单 导入 执行 何执行 | 更新日期: 2023-09-27 18:36:22

好吧,这可能看起来很愚蠢。但是,我不知道如何在Visual Studio 2008中的C#中进行"导入"。我知道Java,但不懂C#。目前,我没有时间阅读 c# 书籍。所以,我需要一些帮助才能把它做好。

我正在尝试使用此处的代码 - SSIS 获取执行 SQL 任务结果集对象

DataTable dt = new DataTable();
OleDbDataAdapter oleDa = new OleDbDataAdapter();
oleDa.Fill(dt, Dts.Variables["User::objShipment"].Value);

我收到错误 - 找不到类型或命名空间名称"OledDbDataAdapter"(您是否缺少使用指令或程序集引用?

我尝试进行java样式导入。但它失败了。

using System.Data.OleDb::OleDbDataAdapter

完整代码如下 -


using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ST_LongCodeGoesHere.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
        //To see the comment here, look at beyond end of this code.
        public void Main()
        {
            // TODO: Add your code here
            DataTable table = new DataTable();
            OledDbDataAdapter oleDbA = new OleDbDataAdapter();
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

评论是——

   /*
            The execution engine calls this method when the task executes.
            To access the object model, use the Dts property. Connections, variables, events,
            and logging features are available as members of the Dts property as shown in the following examples.
        To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
        To post a log entry, call Dts.Log("This is my log text", 999, null);
        To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
        To use the connections collection use something like the following:
        ConnectionManager cm = Dts.Connections.Add("OLEDB");
        cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
        Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        To open Help, press F1.
    */

如何执行简单的 C# 导入

在 C# 中,不导入单个类型,而是导入整个命名空间。像这样的东西应该工作:

using System.Data.OleDb;
...
OleDbDataAdapter oleDa = new OleDbDataAdapter();

或者,您可以创建如下所示的类型别名:

using OleDbDataAdapter = System.Data.OleDb.OleDbDataAdapter;

延伸阅读

  • 使用指令(C# 参考)

在解决方案下的"解决方案资源管理器"中找到"引用",然后通过右键单击和"添加引用"添加 System.Data。您可能还想将导入键入为:使用 System.Data.OleDb;

看看这个 http://msdn.microsoft.com/en-us/library/vstudio/wkze6zky.aspx

  • 您需要先添加引用,然后才能使用它。