Leo Koach

Leo Koach

  • NA
  • 601
  • 223.7k

ADODB connection

Feb 23 2010 6:20 PM

***EDIT***
Actually, this is not a problem, code works. I am just seeking the better ways using latest available tools for C#.

***END EDIT***


Following is the connection I use for an Access DB. Would you please let me know if this connection method is ok for C# or there is a better way. I am new in C#, coming from VB6 world. I would like to learn the best and easiest way to do things as much as possible...
Thanks,
Leo



string SqlStr;
ADODB.Connection Conn1;
ADODB.Recordset Rs1;
Conn1 = new ADODB.Connection();
Rs1 = new ADODB.Recordset();
Conn1.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Leo\\Documents\\Visual Studio 2008\\Projects\\CodeDatabase\\CodeDatabase\\CodeDatabase.mdb;Persist Security Info=False;", "", "", 0 );
SqlStr = "SELECT * FROM Codes";

// Open Recordset
Rs1.Open (SqlStr, Conn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockReadOnly, 0);
// Check if anything in the table
if (Rs1.EOF == false)

// If so, loop and get all the information to fill the list
do
{
lstSubjects.Items.Add(Rs1.Fields["ID"].Value + "~" + Rs1.Fields["_Subject"].Value);
Rs1.MoveNext();
}
while (Rs1.EOF == false);


// Call CloseDatabase
Conn1.Close();
Conn1 = null;
}

Answers (2)