简体   繁体   English

无法从IIS使用DSN连接到SQL

[英]Unable to connect to SQL Using DSN from IIS

I am trying to connect to a DSN in my machine through IIS. 我正在尝试通过IIS连接到我的计算机中的DSN。 My code is like this 我的代码是这样的

  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;
        }
    }

This works properly when I run this as a console app through VisualStudio. 当我通过VisualStudio作为控制台应用程序运行此程序时,此方法正常工作。 But when I host this to IIS and try to run, it throws an error like this 但是,当我将其托管到IIS并尝试运行时,它会抛出这样的错误

System.Data.Odbc.OdbcException: ERROR [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'WORKGROUP\vg$'.

Make sure to use a System DSN, and not a User DSN. 确保使用系统DSN,而不是用户DSN。 Also try passing in the username and password. 另外,请尝试传递用户名和密码。 http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSN/tabid/89/Default.aspx http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSN/tabid/89/Default.aspx

You may also want to try to use a DSN-less connection. 您可能还想尝试使用无DSN的连接。
http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSNLess/tabid/90/Default.aspx http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSNLess/tabid/90/Default.aspx

For older SQL Server versions, you can use "OLE DB Provider for SQL Server" http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQLServer 对于较旧的SQL Server版本,可以使用“用于SQL Server的OLE DB提供程序” http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQLServer

For newer SQL Server versions, use the "Microsoft SQL Server .NET Data Provider" http://www.carlprothman.net/Default.aspx?tabid=86#SQLClientManagedProvider 对于较新的SQL Server版本,请使用“ Microsoft SQL Server .NET数据提供程序” http://www.carlprothman.net/Default.aspx?tabid=86#SQLClientManagedProvider

HTH HTH

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

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