Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends
I wanted to create a table with Datatable class.i used this code:


C#
DataTable dt = new DataTable("tbl1");
            DataColumn column1 = new System.Data.DataColumn();
            column1.ColumnName = "First";
            column1.DataType = typeof(int);
            column1.DefaultValue = 0;
            column1.Unique = true;
            column1.AllowDBNull = false;
            dt.Columns.Add();
            DataSet se= new System.Data.DataSet();
            se.Tables.Add(dt);


But It doesn't work.
How can i create table in database with Datatable class?is it possible?

please guide me!
thanks

What I have tried:

.................................................................
Posted
Updated 8-Jul-16 4:59am

1 solution

Your code works: it just doesn't do much!
Specifically, it doesn't display anything, so you can;t see that it has worked.
Use the DataTabe as the DataSource for a data aware control such as a DatGirdView or similar, and you'll see.

And it'd work better if you added some data!
C#
column1.AllowDBNull = false;
dt.Columns.Add();
dt.Rows.Add(111);
dt.Rows.Add(112);
dt.Rows.Add(113);
DataSet se = new System.Data.DataSet();
se.Tables.Add(dt);
myDataGridView.DataSource = dt;
 
Share this answer
 
Comments
Member 12509819 8-Jul-16 12:33pm    
Thanks for your answer.
I added above row but it didn't add any table to database.
My table is not exist.i want to add it with datatable class.
Ofcourse i did this with table class.but i couldn't define primary key with table class.
Is there way anymore?
OriginalGriff 8-Jul-16 13:56pm    
If you want to create database tables then you *have to tell us that*!
Remember that we can't see your screen, access your HDD, or read your mind - we only get *exactly* what you type to work with.
Creating tables from a datatable is unusual - it's not a "normal" thing to do with a DB as Tables are generally finely crafted and well thought out (getting the design wrong can make your applications that use it really difficult to code and maintain).
But... it's possible:
http://stackoverflow.com/questions/1348712/creating-a-sql-server-table-from-a-c-sharp-datatable

However. Just because you *can* do something doesn't mean your *should* do it. I'd think long and hard about what you are going to end up with and what you are goign to use it for before going down this route if I was you.
Member 12509819 9-Jul-16 7:34am    
Thanks again! i asked a question.
But i think your answer "Just because you *can* do something doesn't mean your *should* do it. I'd think long and hard about what you are going to end up with and what you are going to use it for before going down this route if I was you" has got nothing to do with the subject!
OriginalGriff 9-Jul-16 7:43am    
You'd be surprised!
If you are doing something that is decidedly odd - and you are - then that's very often a sign that you are doing something very, very wrong and going down the wrong route. Hence the "Just because you *cn* do something..." comment.
Remember, we don't know why you are trying to do this - so we can't say "that's silly, do it like this instead and your life will be a whole lot easier". All we can do is say "think before you do this - it isn't something you probably need to do!"

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