ADODB to ADO.NET conversion

S

sck10

Hello,

I am using the following in ASP.NET 2.0 VB using ADODB (which is working).
I would like to convert this to csharp using ADO.NET.

To build the connection, I am trying to convert:

LDAP_CONN = CreateObject("ADODB.Connection")
LDAP_CONN.Provider = "ADsDSOObject"
LDAP_CONN.Open()
LDAP_COM = CreateObject("ADODB.Command")
LDAP_COM.ActiveConnection = LDAP_CONN
RS = LDAP_CONN.Execute(strSQL)

to

OleDbConnection conn = new OleDbConnection("ADsDSOObject");
OleDbDataReader rdr = null;
conn.Open();
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
rdr = cmd.ExecuteReader();



When I run this, I get the following error:

Format of the initialization string does not conform to specification
starting at index 0 (for OleDbConnection conn = new
OleDbConnection("ADsDSOObject");).

Also, should the value that I have for LDAP_CONN.Provider = "ADsDSOObject"
be used for the OleDbConnection?

Any help with this would be appreciated. Thanks, sck10



VB Code
-----------------

Dim stlPost As SortedList = New SortedList()
Dim strValue As String = ""
Me.lblMessageText.Text = ""

Dim LDAP_CONN As Object, LDAP_COM As Object, RS As Object
Dim tmp As Object = "", Item As Object = ""

Dim strFields As String = "ntUserDomainId, employeeNumber, uid, cn,
telephonenumber, mail "
Dim strLDAP As String =
"'LDAP://ldap-uscentral.post.MyCompany.com:389/o=MyCompany.com/ou=people' "
Dim strWhere01 As String = "WHERE employeenumber='" & strHRID & "' "
Dim strWhere02 As String = "OR uid='" & strHRID & "'"
Dim strSQL As String = "SELECT " & strFields & " FROM " & strLDAP &
strWhere01 & strWhere02 & " ORDER BY sn"

Try
LDAP_CONN = CreateObject("ADODB.Connection")
LDAP_CONN.Provider = "ADsDSOObject"
LDAP_CONN.Open()
LDAP_COM = CreateObject("ADODB.Command")
LDAP_COM.ActiveConnection = LDAP_CONN
RS = LDAP_CONN.Execute(strSQL)

For Each Item In Split(Replace(strFields, " ", ""), ",")
tmp = RS(Item).Value
strValue = tmp(0).ToString
If Item.ToString = "ntUserDomainId" Then strValue = tmp ' Can't handle
"\" in domain\sck10
stlPost.Add(Item.ToString, strValue)
'Me.lblPost.Text &= Item.ToString & ": " & tmp(0).ToString & "<br />"
'Response.Write(Item & ": " & tmp(0) & "<br />") 'If Not
IsNull(tmp) Then Response.Write(tmp(0))
Next

stlPost.TrimToSize() ' Trim the list

'Asign values from Array
Dim strCN As String = stlPost("cn").ToString
Me.hdnLDAPLastName.Value = Trim(Left(strCN, InStrRev(strCN, ",") - 1))
Me.hdnLDAPFirstName.Value = Trim(Mid(strCN, InStrRev(strCN, ",") + 1))
Me.hdnLDAPEmail.Value = Trim(stlPost("mail").ToString)
Me.hdnLDAPTelephone.Value = Trim(stlPost("telephonenumber").ToString)

Catch ex As Exception
Me.pnMessage.Visible = True
End Try
 
S

Steven Cheng[MSFT]

Hello Steve,

From the code snippet you provided, you used to use the OLEDB provider to
query some LDAP directory storage and want to uprade the code into .NET
code, correct?

Based on my experience, in .net framework, the classes under the
System.DirectoryServices namespace are the prefered ones for manipulating
LDAP , ActiveDirectory service. For your scenario, if you want to query
some objects from a LDAP storage, you can use the "DirectorySearcher" class
which can be constructed with a LDAP connectionstring and some additional
search filter or optional properties. e.g.

#the following function use DirectorySearcher to query the user objects in
the domain
==========================
Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnQuery.Click

Response.Write("<br/>btnQuery_Click.............")

Dim connstr =
"LDAP://fareast.corp.microsoft.com/OU=UserAccounts,DC=fareast,DC=corp,
DC=Microsoft, DC=com"


Try

Dim entry As New
DirectoryEntry("LDAP://fareast.corp.microsoft.com/OU=UserAccounts,DC=fareast
,DC=corp, DC=Microsoft, DC=com")


Dim mySearcher As New DirectorySearcher(entry)

mySearcher.Filter =
"(&(objectCategory=person)(objectClass=user)(Displayname=Steven Cheng))"


Dim mySearchResult As SearchResultCollection =
mySearcher.FindAll()



Dim result As SearchResult = mySearchResult(0)


Dim item As DirectoryEntry = result.GetDirectoryEntry()

Response.Write("<br/>Name: " & item.Name)


For Each props As PropertyValueCollection In item.Properties

Response.Write("<br/>" & props.PropertyName & ": " &
props(0).ToString())

'if you want to print out all the property values
'If (props.Count > 0) Then
' For Each obj As Object In props
' Response.Write("<br/>&nbsp;&nbsp;&nbsp;&nbsp;" &
obj.ToString())

' Next
'End If

Next

Catch ex As Exception

Response.Write("<br/>" & ex.ToString())
End Try



End Sub
===================================

here are some other good reference and examples about ADSI programming
through VB.NET:

#Quick List for Visual Basic 2005 Code Examples
http://msdn2.microsoft.com/en-us/library/ms180834.aspx

#Active Directory and VB.NET
http://www.vbdotnetheaven.com/UploadFile/johncharles/ActiveDirectoryInVB1112
2005060642AM/ActiveDirectoryInVB.aspx?ArticleID=a775db4b-3909-4d36-86e2-917b
6d693465

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sck10

Steven,

Thank you very much for your help.

I got it to work, but I had to add:
entry.AuthenticationType = AuthenticationTypes.None;

Is this the default? I noticed that a lot of the examples on the web
don't show this in their examples.

Thanks again, sck10

try
{
DirectoryEntry entry = new
DirectoryEntry("LDAP://ldap-uscentral.post.lucent.com:389/o=lucent.com/ou=people");
entry.AuthenticationType = AuthenticationTypes.None;
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectclass=person)(employeenumber=" + HRID +
"))";
SearchResultCollection mySearchResult = mySearcher.FindAll();
SearchResult result = mySearchResult[0];
DirectoryEntry item = result.GetDirectoryEntry();

foreach(PropertyValueCollection props in item.Properties)
{
Response.Write("<br />" + props.PropertyName + ": " +
props[0].ToString());
}
} // end try
 
S

Steven Cheng[MSFT]

Thanks for your reply Steve,

Glad that you've got it working.

Yes, the DirectoryEntry.AuthenticationType is default to "Secure".
Actually, it is in .net framework 2.0 that has changed the default value to
"Secure", in previous versions, the default value is "None".

#DirectoryEntry.AuthenticationType Property
http://msdn2.microsoft.com/en-us/library/system.directoryservices.directorye
ntry.authenticationtype.aspx

I think that's why you've found many examples that didn't mention
this(since they're dedicated to .net 1.x).

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top