Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   program to create tables and queries (http://www.programmingforums.org/showthread.php?t=5749)

re10 Sep 5th, 2005 3:16 AM

program to create tables and queries
 
Hello,

Can anyone help me? I need to create a program in C# to automatically create tables/queries in MS Access (using ADO).

Thx.

Ooble Sep 5th, 2005 9:49 AM

Yes. Write some code and I'll help you.

re10 Sep 6th, 2005 12:26 AM

type or namespace 'ADO' problem
 
Hi,

Here is how the codes look like:

////Program to create table
namespace database1
{
using System;
using System.Data.ADO;

public class Class1
{
public Class1()
{
//
// TODO: Add Constructor Logic here
//
}

public static int Main(string[] args)
{
try
{
ADOConnection s = new ADOConnection("Data Source=mymdb");
s.Open();
Console.WriteLine("Connection established");

//Create the table
Console.Write("Do you want to create a table?(y/n) ");
string ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand CreateTable = new ADOCommand("Create Table Table1(vno integer,name char(20))", s);
CreateTable.ExecuteNonQuery();
Console.WriteLine("ADOCommand executed / Table created");
}


//Insert values into the table
Console.Write("Do you want to insert some values in a table?(y/n) ");
ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand InsTable = new
ADOCommand("insert into Table1 values(1, 'hi')", s);

InsTable.ExecuteNonQuery();
Console.WriteLine("Values inserted");
}

//Delete the whole table
Console.Write("Do you want to delete all records present in the table?(y/n) ");
ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand DeleteTable = new ADOCommand("Delete from Table1", s);
DeleteTable.ExecuteNonQuery();
Console.WriteLine("All records deleted");
}

//View all records
Console.Write("Do you want to view all records present in the table (y/n)? ");
ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand AllRecs = new ADOCommand("select * from Table1", s);
//AllRecs.ExecuteNonQuery();
ADODataReader r;
AllRecs.Execute(out r);
while (r.Read())
{
for (int i = 0; i < r.FieldCount; i++)
{
Console.Write(r.GetValue(i) + " ");
}
Console.WriteLine();
}
Console.WriteLine("All Records Displayed");
r.Close();
}

s.Close();
Console.ReadLine();

}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}

return 0;
} // Main End
} // Class Ends
}// namespace ends
//End of program


I'm getting the following error:
The type or namespace name 'ADO' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)C:\Documents and Settings\rm\Desktop\Program\test1\Program.cs
Line 4
Column 23
Project database1

Pls help me to sort this out
Thx in advance

iignotus Sep 6th, 2005 1:33 AM

Edit your post and put that in CODE tags.

re10 Sep 6th, 2005 2:49 AM

The codes (in code tags):

:

////Program to create table
namespace database1
{
using System;
using System.Data.ADO;

public class Class1
{
public Class1()
{
//
// TODO: Add Constructor Logic here
//
}

public static int Main(string[] args)
{
try
{
ADOConnection s = new ADOConnection("Data Source=mymdb");
s.Open();
Console.WriteLine("Connection established");

//Create the table
Console.Write("Do you want to create a table?(y/n) ");
string ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand CreateTable = new ADOCommand("Create Table Table1(vno integer,name char(20))", s);
CreateTable.ExecuteNonQuery();
Console.WriteLine("ADOCommand executed / Table created");
}


//Insert values into the table
Console.Write("Do you want to insert some values in a table?(y/n) ");
ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand InsTable = new
ADOCommand("insert into Table1 values(1, 'hi')", s);

InsTable.ExecuteNonQuery();
Console.WriteLine("Values inserted");
}

//Delete the whole table
Console.Write("Do you want to delete all records present in the table?(y/n) ");
ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand DeleteTable = new ADOCommand("Delete from Table1", s);
DeleteTable.ExecuteNonQuery();
Console.WriteLine("All records deleted");
}

//View all records
Console.Write("Do you want to view all records present in the table (y/n)? ");
ch = Console.ReadLine();
if (ch == "y")
{
ADOCommand AllRecs = new ADOCommand("select * from Table1", s);
//AllRecs.ExecuteNonQuery();
ADODataReader r;
AllRecs.Execute(out r);
while (r.Read())
{
for (int i = 0; i < r.FieldCount; i++)
{
Console.Write(r.GetValue(i) + " ");
}
Console.WriteLine();
}
Console.WriteLine("All Records Displayed");
r.Close();
}

s.Close();
Console.ReadLine();

}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}

return 0;
} // Main End
} // Class Ends
}// namespace ends
//End of program


Getting the following error:
The type or namespace name 'ADO' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)C:\Documents and Settings\rm\Desktop\Program\test1\Program.cs
Line: 4
Column: 23
Project: database1

Thx for any help

Infinite Recursion Sep 6th, 2005 8:47 AM

Sounds like you need to add a reference to ADO in your project.


All times are GMT -5. The time now is 8:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC