1

I am trying to connect to a DSN in my machine through IIS. 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. But when I host this to IIS and try to run, it throws an error like this

System.Data.Odbc.OdbcException: ERROR [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'WORKGROUP\vg$'.
4
  • Check your app pool identity
    – ElGavilan
    May 6, 2015 at 13:22
  • stackoverflow.com/questions/2806438/… - different user. May 6, 2015 at 13:23
  • @ElGavilan already Checked and made it to networkkusers . Still same issue
    – None
    May 6, 2015 at 13:24
  • Did you grant permission to access database by the application. Try sp_grantlogin 'NT AUTHORITY\NETWORK SERVICE'
    – wonderbell
    May 6, 2015 at 14:23

1 Answer 1

1

Make sure to use a System DSN, and not a User DSN. Also try passing in the username and password. http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSN/tabid/89/Default.aspx

You may also want to try to use a DSN-less connection.
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

For newer SQL Server versions, use the "Microsoft SQL Server .NET Data Provider" http://www.carlprothman.net/Default.aspx?tabid=86#SQLClientManagedProvider

HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.