Newbiw to databases in C#

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,

I am trying to write a program, where I want to fill some records,
write them to a file and then display them back to screen.

What I have done is the following, but it is giving me an error during
execution.


/*********************************************/
using System;
using System.Data;
using System.IO;

namespace DBTesting
{
public class DBObject : DataSet // important for tables
{
// database tables
public const string TABLE_NAME = "Clubs";

// database fields
public const string CLUB_NAME = "Club Name";
public const string LEAGUES_WON = "Leagues Won";

public DBObject()
{
createTables();
} // end constructor

private void createTables()
{
// created the table
this.Tables.Add(new DataTable(TABLE_NAME));

// creating the field names
this.Tables[TABLE_NAME].Columns.Add(CLUB_NAME,
typeof(System.Byte[]));
this.Tables[TABLE_NAME].Columns.Add(LEAGUES_WON, typeof(Int32));
} // end method createTables

} // end class DBObject
} // end namespace DBTesting


namespace DBTesting
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// Instance of the database
DBObject db = new DBObject();

// Creating a row in the database
DataRow dr = db.Tables[DBObject.TABLE_NAME].NewRow();

// Filling the data in the row just created
dr[DBObject.CLUB_NAME] = "Milan";
dr[DBObject.LEAGUES_WON] = 17;

// Writing the row just filled to the database
db.Tables[DBObject.TABLE_NAME].Rows.Add(dr);

db.WriteXml(@"c:\test.xml");

Console.WriteLine ("Testing the program");
Console.WriteLine ("Record 1 : {0}", db.ReadXml(@"c:\test.xml"));
Console.ReadLine();
} // end Main method
} // end Class1
} // end namespace DBTesting
/******************************************/


This is only a test program, for me to learn how to program databases.

Can someone figure my problem out.



Thanks in Advance
 
Xarky said:
Hi,

I am trying to write a program, where I want to fill some records,
write them to a file and then display them back to screen.

What I have done is the following, but it is giving me an error during
execution.


/*********************************************/
using System;
using System.Data;
using System.IO;

namespace DBTesting
{
public class DBObject : DataSet // important for tables
{
// database tables
public const string TABLE_NAME = "Clubs";

// database fields
public const string CLUB_NAME = "Club Name";
public const string LEAGUES_WON = "Leagues Won";

public DBObject()
{
createTables();
} // end constructor

private void createTables()
{
// created the table
this.Tables.Add(new DataTable(TABLE_NAME));

// creating the field names
this.Tables[TABLE_NAME].Columns.Add(CLUB_NAME,
typeof(System.Byte[]));

Should be

// creating the field names
this.Tables[TABLE_NAME].Columns.Add(CLUB_NAME,typeof(string));

David
 
Might help if you copy and paste the error message you're receiving...

Thanks,
Michael C., MCDBA
 
Should be
// creating the field names
this.Tables[TABLE_NAME].Columns.Add(CLUB_NAME,typeof(string));

David

Thanks for your help given. That problem has been solved succesfully.
Now when I execute the program, the following output is being given:

Testing the program
Record 1 : IgnoreSchema


What should I do to obtain the record, that is the CLUB_NAME and
LEAGUES_WON.


Thanks in Advance for all your help.
 
Back
Top