E-mail
See: http://www.programgood.net/2009/10/20/TestDrivenDevelopmentInNETDALCh5.aspx
ADO.NET (ActiveX Data Objects) consists of two primary parts:
These classes provide access to a data source, such as a Microsoft SQL Server. Each data source has its own set of provider objects, but they each have a common set of utility classes:
DataSet objects, a group of classes describing a simple in-memory relational database,
OLE-DB is a replacement for ODBC.
When adding in a reference (a COM – Component Object Model).. I had to restart VS to get Resharper/VS to recognize and give me intellisense.
The reference was an ADOX reference – ADO ext 2.8 for DDL and Security. We have to use this as the OLE DB features do not give create new database functionality.
int index;
string newDB;
string dbName = txtDatabaseName.Text;
try
{
if (dbName.Length == 0)
return;
index = dbName.LastIndexOf('.');
if (index == -1) // no secondary filename
dbName += ".mdb";
}
string pathName = Application.StartupPath.ToString();
string combinedName = Path.Combine(pathName, dbName);
ADOX.CatalogClass myCat = new ADOX.CatalogClass();
newDB = CONNECTSTRING + combinedName + ";" + CONNECTSTRINGPART2;
myCat.Create(newDB);
myCat = null;
Good bit of functionality, if you double click on an element in the list, it deletes it.
void lstFieldsToAdd_DoubleClick(object sender, EventArgs e)
ListView.SelectedIndexCollection indexes = lstFieldsToAdd.SelectedIndices;
foreach (int index in indexes)
lstFieldsToAdd.Items[index].Remove();
Writing to the database he uses a class in clsDB called ProcessCommand
However to read data he does this from a form object. So only using the clsDB to get the connection string.
OleDbConnection myDB = new OleDbConnection();
OleDbDataReader myReader;
OleDbCommand myCommand;
clsDB myData = new clsDB("Friend");
myDB.ConnectionString = myData.getConnectString + dbName;
myDB.Open();
if (txtLastName.Text.Length != 0)
sql = "SELECT * FROM Friend WHERE LastName = '" +
txtLastName.Text + "'";
else
sql = "SELECT * FROM Friend WHERE ID = " + txtRecordID.Text;
ClearFields();
myCommand = new OleDbCommand(sql, myDB);
myReader = myCommand.ExecuteReader();
if (myReader.Read() == true)
record = (int)myReader[0];
txtRecordID.Text = record.ToString();
Changing properties on the grid, such as docking to allow window resizing, colour schemes, formatting of cells.
Double click on the database table automatically runs the execute query. User can drag column headers around, and sort each column. User can add/edit/del data (code not implemented in this example).
var query = from p in numbers // the linq query
where p > lo && p < hi
select p;
foreach (var val in query) // display results
lstOutput.Items.Add(val.ToString());
This is an interesting construct, using anonymous types. What this is actually saying
var friends = new[]
new {name = "Tom", state = "IN"},
new {name = "Alice", state = "VA"},
new {name = "Tammy", state = "IN"},
new {name = "Ann", state = "KY"},
};
var query = from p in friends
where p.state == txtLow.Text
foreach (var val in query)
lstOutput.Items.Add(val.name.ToString() + ", " + val.state.ToString());
Remember Me
a@href@title, strike