Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends

How do i create a table in C#.
Posted
Comments
DamithSL 25-Dec-14 21:38pm    
where you want to create table? what have you tried so far?
PIEBALDconsult 25-Dec-14 22:34pm    
That doesn't sound like a good idea; what are you trying to do?
satya.Sw 26-Dec-14 0:01am    
Hi Experts.
Thank u all for replying.

I am very new to dotnet.

I have a window. and I wanted to add automatic table while clicking on button. and
For each table i need a specific name.

Example : I have Passport No. For Every Passport No. I want to create seperate table so i have choosen this way. and its working.

If there is any other solution. please Update me i am highly interested to take advices.

private void button1_Click(object sender, EventArgs e)
{

OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Documents\Database1.accdb");

myConnection.Open();

OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;
string qry = "CREATE TABLE [Database3]([Ename] text, [ESurname] text)";


myCommand.CommandText = qry.Replace("Database3", textBox1.Text);
myCommand.ExecuteNonQuery();
MessageBox.Show("Table Created Successfully");
myConnection.Close();
}

PIEBALDconsult 26-Dec-14 2:14am    
"For Every Passport No. I want to create seperate table"

I _strongly_ advise against that; please find a better solution.

Refer to the following

http://msdn.microsoft.com/en-us/library/ms753342(v=vs.110).aspx

A FlowDocument is created to host the Table, and a new Table is created and added to the contents of the FlowDocument.

// Create the parent FlowDocument...
flowDoc = new FlowDocument();

// Create the Table...
table1 = new Table();
// ...and add it to the FlowDocument Blocks collection.
flowDoc.Blocks.Add(table1);


// Set some global formatting properties for the table.
table1.CellSpacing = 10;
table1.Background = Brushes.White;
 
Share this answer
 
if you need to create table in access database you can use OleDbConnection and OleDbCommand classes to do it, for example:
C#
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbCommand cmmd = new OleDbCommand("CREATE TABLE  TableName ( [ID] Counter Primary Key, [FirstName] Text, [LastName] Text)", conn);
con.Open();
cmmd.ExecuteNonQuery();

refer https://www.connectionstrings.com/access/[^] for how you can give connection string for access database.
 
Share this answer
 
Comments
satya.Sw 26-Dec-14 0:04am    
Thank U Boss.

Now How could i Insert Data into Created table. Can You Please Explain me with any Reference.
Thank You Boss. Its Working Good.

private void button1_Click(object sender, EventArgs e)
{

OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Documents\Database1.accdb");

myConnection.Open();

OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;
string qry = "CREATE TABLE [Database3]([Ename] text, [ESurname] text)";


myCommand.CommandText = qry.Replace("Database3", textBox1.Text);
myCommand.ExecuteNonQuery();
MessageBox.Show("Table Created Successfully");
myConnection.Close();
}
 
Share this answer
 
Comments
BillWoodruff 25-Dec-14 23:57pm    
Please post a reply to a solution as a comment on the reply, not as a "solution."

If a solution helped you, or solved your problem, be sure and vote for the solution, or accept it as your answer, to let the person who assisted you know you appreciate their time and effort.
Hi Experts.
Thank u all for replying.

I am very new to dotnet.

I have a window. and I wanted to add automatic table while clicking on button. and
For each table i need a specific name.

Example : I have Passport No. For Every Passport No. I want to create seperate table so i have choosen this way. and its working.

If there is any other solution. please Update me i am highly interested to take advices.
 
Share this answer
 
Comments
DamithSL 26-Dec-14 0:08am    
don't post your questions as solutions. create new question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900