-
December 4th, 2008, 06:32 AM
#1
Simple beginners problem
Hi,
I'm new to ADO. NET and I want to create a complete new Access DB (mdb)
insert few tables and some columns in each table.
Further on I want add or update existing data.
But unfortunately I have trouble to find information in the web. This is the code
I have so far.
Code:
using System;
using ADOX;
using ADODB;
using System.IO;
public class Program
{
static int Main()
{
if (File.Exists("C:\\Exercise1.md")) File.Delete("C:\\Exercise1.mdb");
// CREATE FILE
ADOX.CatalogClass catDatabase;
string strDatabase = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source='C:\\Exercise1.mdb'";
catDatabase = new ADOX.CatalogClass();
catDatabase.Create(strDatabase);
Console.WriteLine("A new Microsoft JET database named " +
"Exercise1.mdb has been created\n");
// CREATE DB with a table
ConnectionClass conDatabase = new ConnectionClass();
try
{
object objAffected;
string strStatement = "CREATE TABLE Customers(FirstName char, " +
"MiddleName String, " +
"LastName varchar);";
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source='C:\\Exercise1.mdb';";
conDatabase.Open(strConnection, "", "", 0);
conDatabase.Execute(strStatement, out objAffected, 0);
}
finally
{
conDatabase.Close();
}
// ADD column
ADODB.Connection conADO = new ADODB.Connection();
object obj = new object();
string stStatement;
conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source='C:\\Exercise1.mdb'", "", "", 0);
stStatement = "ALTER TABLE Customers ADD COLUMN CellPhone string;";
conADO.Execute(stStatement, out obj, 0);
conADO.Execute("INSERT INTO Customers(FirstName, MiddleName, LastName) " +
"VALUES('James', 'Carlton', 'Male');", out obj, 0);
Console.WriteLine("A new column named CellPhone has been added " +
"to the Employees table.");
conADO.Close();
// UPDATING OF A COLUMN???
return 0;
}
}
Could some explain, how to extend that lower part, in order to update table contents, please?
And how can I search for a specific value on a table?
Thanks in advance!
-
December 4th, 2008, 08:09 AM
#2
Re: Simple beginners problem
For update
Code:
UPDATE Table SET Field1 = 'value' WHERE Field2 = 'criteria'
For select
Code:
SELECT * FROM Table WHERE Field2 = 'criteria'
-
December 4th, 2008, 09:32 PM
#3
Re: Simple beginners problem
Sounds like fill in the blank homework. We usually don't do others work for them.
-
December 5th, 2008, 12:58 PM
#4
Re: Simple beginners problem
@dglienna
It cannot be some homework. I'm 39 years old. Just think of it for a few seconds :-/
I found a better way throurgh OleDB,
but thanks to dee-u dee-u anyway.
-
December 6th, 2008, 03:04 PM
#5
Re: Simple beginners problem
Originally Posted by MNovy
I found a better way throurgh OleDB,
but thanks to dee-u dee-u anyway.
May we know what would be that better way through OleDB?
-
December 8th, 2008, 03:17 AM
#6
Re: Simple beginners problem
Originally Posted by dee-u
May we know what would be that better way through OleDB?
I'll post my code later, after I could finish my little tool :-D
I have to manage some documents, where Excel is not fast enough.
But Access is new to me. I need a bit time to get along with, so please excuse my beginner's questions. I'm not a student or sth like this.
-
December 8th, 2008, 07:36 AM
#7
Re: Simple beginners problem
there is no SQL command that lets you create an empty database in MS Acess, unlike with the other databases. so I always include a blank MDB whenever my application needs to created a new Access database on the fly (File.Copy ). By the way, the code posted above is ADO.
Busy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|