Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Error in LDAP query

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2010-02-08 : 15:17:37
I have created a sp querying LDAP such as:

ALTER PROCEDURE [dbo].[sp_LDAP_Query] 

as

set nocount on

select *
from openquery(ADSI, '
select title, sAMAccountName, displayName, userAccountControl
from ''LDAP://DC=xxx,DC=COM''
where objectCategory = ''User''')
where title is not null and (userAccountControl <> '66050' and userAccountControl <> '514')
order by displayName


I sign on to Sequel Server 2005 in Windows Authentication mode and execute the sp:

exec vecellio.dbo.sp_LDAP_Query


A result set is returned. So far, so good.

Now in ASP I have a connection string like:

<add key="ConnectionString" value="Data Source=VGIBESQL;Initial Catalog=XXX;Integrated Security=SSPI;"/>


also using Windows Authentication and code to call the same sp:

If mySqlConnection.State = Data.ConnectionState.Closed Then
mySqlConnection.Open()
End If

Try
mySqlCommand.Connection = mySqlConnection
mySqlCommand.CommandType = Data.CommandType.StoredProcedure
mySqlCommand.CommandText = ("vecellio.dbo.sp_LDAP_Query")
mySqlCommand.Connection = mySqlConnection
mySqlDataAdapter.SelectCommand = mySqlCommand
mySqlDataAdapter.Fill(mySqlDataTable)
Session("myMenuDataTable") = mySqlDataTable
Catch ex As Exception
Message = ex.ToString
myError = True
End Try


Now I get error on the "mySqlCommand.ExecuteScalar()" line:


System.Data.SqlClient.SqlException: An error occurred while preparing the query " 
select title, sAMAccountName, displayName, userAccountControl
from 'LDAP://DC=Vecelliogroup,DC=COM'
where objectCategory = 'User'" for execution against OLE DB provider "ADsDSOObject" for linked server "ADSI".


Any ideas???

Jaime
Starting Member

19 Posts

Posted - 2010-02-08 : 17:04:09
Do the LDAP query from the application instead of the database server.

I'm sure that's not what you want to hear, but using SQL Server as a proxy to get to Active Directory adds an additional layer of confusion, makes debugging more difficult, and requires you to create a linked server object that can become a security hole.
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2010-02-08 : 19:32:28
I tried to use code behind:

Dim con = CreateObject("ADODB.Connection")
Dim com = CreateObject("ADODB.Command")
Dim RS = CreateObject("ADODB.Recordset")

con.Provider = "ADsDSOObject"
con.Open()
com.ActiveConnection = con
com.CommandText = "select sAMAccountName from 'LDAP://DC=Vecelliogroup,DC=COM' where objectCategory = 'User'"

RS = com.Execute()

Do Until RS.EOF
Dim mystring As String
mystring = RS.Fields("sAMAccountName").ToString
RS.MoveNext()
Loop


I put debug on "RS.MoveNext()" and the my value of mystring is System.__ComObject. It must be something wrong with my syntax or?
Go to Top of Page

Jaime
Starting Member

19 Posts

Posted - 2010-02-09 : 09:15:10
Why did you switch from ADO.Net to ADO?

Simply change the original stored procedure call to:

Dim cn As New OleDb.OleDbConnection("Provider=ADSDSOObject")
Dim cmd As OleDb.OleDbCommand = cn.CreateCommand()
cmd.CommandText = "select sAMAccountName from 'LDAP://DC=Vecelliogroup,DC=COM' where objectCategory = 'User'"

Dim da As New OleDb.OleDbDataAdapter(cmd)
da.Fill(mySqlDataTable)
Session("myMenuDataTable") = mySqlDataTable
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2010-02-10 : 08:58:48
Hi,

That worked great. I need to expand my LDAp query select

select sAMAccountName from 'LDAP://DC=Vecelliogroup,DC=COM' where objectCategory = 'User'


to include

where title is not Null and (userAccountControl <> '66050' and userAccountControl <> '514')


I have problems with the syntax. Thank you.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-10 : 09:27:16
quote:
Originally posted by snufse

Hi,

That worked great. I need to expand my LDAp query select

select sAMAccountName from 'LDAP://DC=Vecelliogroup,DC=COM' where objectCategory = 'User'


to include

where title is not Null and (userAccountControl <> '66050' and userAccountControl <> '514')


I have problems with the syntax. Thank you.


What is the difficulty you are facing?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2010-02-10 : 10:23:28
Have this:


Dim prefixText As String = "vg"
Dim cn As New Data.OleDb.OleDbConnection("Provider=ADSDSOObject")
Dim cmd As Data.OleDb.OleDbCommand = cn.CreateCommand()
cmd.CommandText = "select sAMAccountName, displayName, title, userAccountControl from 'LDAP://DC=Vecelliogroup,DC=COM' where objectCategory = 'User' and title <> ' ' and (userAccountControl <> '66050' and userAccountControl <> '514') and sAMAccountName like '%" & prefixText & "%'"
Try
Dim da As New Data.OleDb.OleDbDataAdapter(cmd)
da.Fill(mySqlDataTable)


Value of my select string is: select sAMAccountName, displayName, title, userAccountControl from 'LDAP://DC=Vecelliogroup,DC=COM' where objectCategory = 'User' and title <> ' ' and (userAccountControl <> '66050' and userAccountControl <> '514') and sAMAccountName like '%vg%'

Getting error:

System.Data.OleDb.OleDbException: 'ADSDSOObject' failed with no error message available, result code: DB_E_ERRORSINCOMMAND(0x80040E14).

Now, if I leave out the "title" and the "sAMAccountName" from my select query it works fine. I am trying to get records where the "title" is not empty and name matches my prefix.
Go to Top of Page
   

- Advertisement -