繁体   English   中英

C#中的OdbcConnection行出现CS0246错误

[英]CS0246 error with OdbcConnection line in C#

我已经将这个CS0246错误追赶了几个小时,并且不确定如何解决它。 给出以下简单的C#代码:

using System;
// using Microsoft.Data.Odbc; 
using System.Data.Odbc; 

namespace dotnetdb
{
    class Program
    {
        static private void SelectRows(string[] args)
        {
            string passWord = "PWD=password";
            string uName    = "UID=username";
            string dbServer = "SERVER=server";
            string dbName   = "DATABASE=db";
            string driver   = "DRIVER={ODBC Driver 13 for SQL Server}"
            string connString = // assembled from above 

            string sql = // sql;

            OdbcConnection conn = new OdbcConnection(connString);

            conn.Open();

            OdbcCommand cmd = new OdbcCommand(sql, conn);

            // work with cmd

            Console.WriteLine("Didnt kick the bucket!");
        }
    }
}

第2行上的Microsoft节产生CS0234错误。 第3行上的节(来自Microsoft文档)为我提供了CS0246:

Program.cs(20,13): error CS0246: The type or namespace name 'OdbcConnection' could not be found

我一直在go和python中使用此ODBC连接,但这是我第一次尝试将其与C#一起使用。 也是我的第一个C#程序。 上面的代码几乎直接从MS文档中删除-我缺少什么? 如何获得对System.Data.Odbc访问权限?

在学习如何使用C#之前,我是否要跑步?

请注意,使用dotnet build [console|webapi]创建的应用程序可以正常运行。

谢谢!

您需要添加它作为参考。 有关如何在Visual Studio Code中添加引用,请参考此问题 我还注意到您的程序没有Main() ,这也将阻止它进行编译。

更改此:

static private void SelectRows(string[] args)

static void Main(string[] args)

或像这样从Main()调用它:

static void Main(string[] args)
{
    SelectRows(args);
}

private static void SelectRows(String[] args)
{
    ...
}

通常,引用是一段已编译的代码,大部分为.DLL格式,您可以将其包含在自己的项目中,以便可以使用引用提供的方法/代码。

例如,

假设我有一个人创建的MyMath.dll ,我想在其中使用此方法。

int Add(int a, int b) {
    return a + b;
}

我必须包括其他人创建的MyMath.dll才能使用该Add()方法。 因此,当我想使用它时,我会使用类似的东西。

using MyMath; // MyMath.dll

static void Main(string[] args)
{
    MyMath calculator = new MyMath();
    int result = calculator.Add(1, 2);
}

如果您不知道,Visual Studio也会提供一个非常强大的免费社区版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM