Hi, you can use the following method to add column to SQL Server table:
using (DbConnection connection = new SqlConnection(/*connectionString*/))
{
connection.Open();
using (DbCommand command = new SqlCommand("alter table [AddColumn] add [AddColumnID] int default 0 NOT NULL"))
{
command.Connection = connection;
command.ExecuteNonQuery();
}
}
Asp.net king
Member
35 Points
93 Posts
Dynamic Table Using C#
Apr 16, 2012 06:06 PM|LINK
How i can add columns to table in SQL server using Asp.net C#.
(John Johnson)
krishdsouza
Member
250 Points
50 Posts
Re: Dynamic Table Using C#
Apr 16, 2012 06:18 PM|LINK
shatru
Participant
1513 Points
292 Posts
Re: Dynamic Table Using C#
Apr 16, 2012 06:21 PM|LINK
Look at the below link:
Add a column to a sql server table with a asp.net form
Regards
Ajatshatru
Dragons Lab
rakeshreddym
Member
299 Points
79 Posts
Re: Dynamic Table Using C#
Apr 17, 2012 11:23 AM|LINK
========================== FrontPage === <form id="form1" runat="server"> <div> <br /><br /> <asp:button id="IP_TextBtn" onclick="btnAddColumn_Click" runat="server" text="Submit" /> <br /> <br /> <asp:textbox id="txtIP_TextField" runat="server"></asp:textbox> <br /> <br /> <asp:Label id="lblResults" runat="server" Width="575px" Height="121px" Font-Bold="True"></asp:Label> <br /> <br /> </div> </form> ========================= BackPage === // Creating the Method for adding a new column to the database public virtual void btnAddColumn_Click(object sender, EventArgs args) { { string alterSQL; alterSQL = "ALTER TABLE Products3 "; alterSQL += "ADD '" + txtIP_TextField + "' bool()"; SqlConnection con = new SqlConnection(GetConnectionString()); SqlCommand cmd = new SqlCommand(alterSQL, con); cmd.Parameters.AddWithValue("@txtIP_TextField ", txtIP_TextField.Text); int SQLdone = 0; try { con.Open(); SQLdone = cmd.ExecuteNonQuery(); lblResults.Text = "Column created."; } catch (Exception err) { lblResults.Text = "Error Creating column. "; lblResults.Text += err.Message; } finally { con.Close(); } } }Hi,
you can use this.
FightAsABull
Contributor
2228 Points
424 Posts
Re: Dynamic Table Using C#
Apr 18, 2012 06:41 AM|LINK
Hi, you will like this:
http://www.dotnetperls.com/sqlclient
Allen Li - M...
Star
10411 Points
1196 Posts
Re: Dynamic Table Using C#
Apr 24, 2012 07:40 AM|LINK
Hi, you can use the following method to add column to SQL Server table:
using (DbConnection connection = new SqlConnection(/*connectionString*/)) { connection.Open(); using (DbCommand command = new SqlCommand("alter table [AddColumn] add [AddColumnID] int default 0 NOT NULL")) { command.Connection = connection; command.ExecuteNonQuery(); } }If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
sriramabi
Contributor
4351 Points
1277 Posts
Re: Dynamic Table Using C#
Apr 24, 2012 08:46 PM|LINK
Hai
ref pls
http://www.c-sharpcorner.com/UploadFile/mahesh/CreatingDBProgrammaticallyMCB11282005064852AM/CreatingDBProgrammaticallyMCB.aspx
http://www.c-sharpcorner.com/Forums/Thread/42411/
http://stackoverflow.com/questions/1348712/creating-a-sql-server-table-from-a-c-sharp-datatable
rakeshreddym
Member
299 Points
79 Posts
Re: Dynamic Table Using C#
Apr 25, 2012 11:18 AM|LINK
Hi,
using (DbConnection connection = new SqlConnection("Your connection string")) { connection.Open(); using (DbCommand command = new SqlCommand("alter table [Product] add [ProductId] int default 0 NOT NULL")) { command.Connection = connection; command.ExecuteNonQuery(); } }viratsingh2
Member
78 Points
50 Posts
Re: Dynamic Table Using C#
Apr 25, 2012 06:13 PM|LINK
using (DbConnection connection = new SqlConnection("Your connection string")) { connection.Open(); using (DbCommand command = new SqlCommand("alter table [Product] add [ProductId] int default 0 NOT NULL")) { command.Connection = connection; command.ExecuteNonQuery(); } }Check http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/4929a0a8-0137-45f6-86e8-d11e220048c3/
You can use SQL Server Management Objects to create tables in SQL Server using SMO. Here is an example I wrote particular to creating a table:
Create Table in SQL Server 2005 Using C# and SQL Server Management Objects (SMO) - Code Generation
There is a SQL Server SMO Forum here:
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=88&SiteID=1
Hope this helps,