如何进行简单的C#导入?

时间:2013-10-21 06:14:31

标签: c# visual-studio-2008

好吧,这看起来很傻。但是,我不知道在Visual Studio 2008中如何在C#中进行“导入”。我知道java,但不知道C#。目前,我没有时间阅读c#书。所以,我需要一些帮助才能做到正确。

我试图在这里使用代码 - SSIS Getting Execute Sql Task result set object

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

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

我尝试进行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.
    */

3 个答案:

答案 0 :(得分:3)

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

using System.Data.OleDb;

...

OleDbDataAdapter oleDa = new OleDbDataAdapter();

或者你可以创建一个这样的类型别名:

using OleDbDataAdapter = System.Data.OleDb.OleDbDataAdapter;

进一步阅读

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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