C# 无法使用IIS中的DSN连接到SQL

C# 无法使用IIS中的DSN连接到SQL,c#,sql,asp.net,iis,dsn,C#,Sql,Asp.net,Iis,Dsn,我正在尝试通过IIS连接到计算机中的DSN。我的代码是这样的 internal class ODBCClass:IDisposable { private readonly OdbcConnection oConnection; private OdbcCommand oCommand; public ODBCClass(string DataSourceName) { //Instantiate the conne

我正在尝试通过IIS连接到计算机中的DSN。我的代码是这样的

  internal class ODBCClass:IDisposable
    {
        private readonly OdbcConnection oConnection;

        private OdbcCommand oCommand;

        public ODBCClass(string DataSourceName)
        {
//Instantiate the connection
            oConnection = new OdbcConnection("Dsn=" + DataSourceName);
            try
            {
//Open the connection
                oConnection.Open();
//Notify the user that the connection is opened
                Console.WriteLine("The connection is established with the database");
            }
            catch (OdbcException caught)
            {
                Console.WriteLine(caught.Message);
                Console.Read();
            }
        }

        public void CloseConnection()
        {
            oConnection.Close();
        }

        public OdbcCommand GetCommand(string Query)
        {
            oCommand = new OdbcCommand();
            oCommand.Connection = oConnection;
            oCommand.CommandText = Query;
            return oCommand;
        }

        public void Dispose()
        {
            oConnection.Close();
        }
    }
public DataSet GetOrderData()
    {
        using (var o = new ODBCClass("TL"))
        {
            OdbcCommand oCommand = o.GetCommand("select * from Department");
            var oAdapter = new OdbcDataAdapter(oCommand);
            var ds = new DataSet();
            oAdapter.Fill(ds);
            //TO DO : Make use of the data set
            return ds;
        }
    }
System.Data.Odbc.OdbcException: ERROR [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'WORKGROUP\vg$'.
当我通过VisualStudio将其作为一个控制台应用程序运行时,它可以正常工作。但是,当我将其托管到IIS并尝试运行时,它会抛出如下错误

  internal class ODBCClass:IDisposable
    {
        private readonly OdbcConnection oConnection;

        private OdbcCommand oCommand;

        public ODBCClass(string DataSourceName)
        {
//Instantiate the connection
            oConnection = new OdbcConnection("Dsn=" + DataSourceName);
            try
            {
//Open the connection
                oConnection.Open();
//Notify the user that the connection is opened
                Console.WriteLine("The connection is established with the database");
            }
            catch (OdbcException caught)
            {
                Console.WriteLine(caught.Message);
                Console.Read();
            }
        }

        public void CloseConnection()
        {
            oConnection.Close();
        }

        public OdbcCommand GetCommand(string Query)
        {
            oCommand = new OdbcCommand();
            oCommand.Connection = oConnection;
            oCommand.CommandText = Query;
            return oCommand;
        }

        public void Dispose()
        {
            oConnection.Close();
        }
    }
public DataSet GetOrderData()
    {
        using (var o = new ODBCClass("TL"))
        {
            OdbcCommand oCommand = o.GetCommand("select * from Department");
            var oAdapter = new OdbcDataAdapter(oCommand);
            var ds = new DataSet();
            oAdapter.Fill(ds);
            //TO DO : Make use of the data set
            return ds;
        }
    }
System.Data.Odbc.OdbcException: ERROR [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'WORKGROUP\vg$'.

确保使用系统DSN,而不是用户DSN。 还可以尝试输入用户名和密码。

您可能还希望尝试使用无DSN连接。

对于较旧的SQL Server版本,可以使用“用于SQL Server的OLE DB提供程序”

对于较新的SQL Server版本,请使用“Microsoft SQL Server.NET数据提供程序”


HTH

检查你的应用程序池标识-不同的用户。@ElGavilan已经检查并发送给networkkusers。仍然是相同的问题您是否授予应用程序访问数据库的权限。请尝试
sp\u grantlogin'NT AUTHORITY\NETWORK SERVICE'