Aufgabenstellung
Von C# soll auf eine Access-Datenbank zugegrieffen werden.
Lösung
public static string getCustomerCPD(string customerno)
{
string cpd = "Customer using getCustomerCPD not found";
OleDbConnection conn = new OleDbConnection(
"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;"+
"Jet OLEDB:Database Locking Mode=1; "+
"Data Source=\"" + AppDomain.CurrentDomain.BaseDirectory +
"\\OCT.mdb\";Jet OLEDB:Engine Type=5;"+
"Provider=\"Microsoft.Jet.OLEDB.4.0\";Jet OLEDB:System database=;"+
"Jet OLEDB:SFP=False;"+
"persist security info=False;Extended Properties=;"+
"Jet OLEDB:Encrypt Database=False;"+
"Jet OLEDB:Create System Database=False;"+
"Jet OLEDB:Don't Copy Locale on Compact=False;"+
"Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;"+
"Jet OLEDB:Global Bulk Transactions=1";);
try
{
conn.Open();
OleDbCommand comm = new OleDbCommand();
comm.Connection = conn;
comm.CommandText = "SELECT cpdnumber FROM [tbl_Customer] "+
"WHERE debitor=@customerno";
comm.Parameters.AddWithValue("customerno", customerno);
OleDbDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
cpd = reader.GetValue(0).ToString();
}
}
catch (Exception e)
{
return e.Message;
}
finally
{
conn.Close();
}
return cpd;
}