Solved

How to create a table in SQL Server 2005 using SMO?

Posted on 2008-06-25
3
241 Views
Last Modified: 2013-11-26
How to create a table in SQL Server 2005 using SMO?
Please provide sample code.
0
Comment
Question by:sepknow
3 Comments
 
LVL 13

Accepted Solution

by:
rickchild earned 250 total points
ID: 21866537
0
 
LVL 9

Assisted Solution

by:Rob Siklos
Rob Siklos earned 250 total points
ID: 21866561
Add a reference to:
  Microsoft.SqlServer.ConnectionInfo
  Microsoft.SqlServer.Smo
  Microsoft.SqlServer.SqlEnum

Then:

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
 
class Test {
 
  public void CreateTable() {
    using (SqlConnection dbConn = new SqlConnection(CONN_STRING))
    {
        Server server = new Server(new ServerConnection(dbConn));
        Database db = server.Databases[dbConn.Database];
 
        Table table = new Table(db, "MyTable");
 
        Column col = new Column(table, "MyColumn", DataType.Int);
        col.Nullable = false;
        table.Columns.Add(col);
 
        table.Create();
    }
  }
}

Open in new window

0
 

Author Closing Comment

by:sepknow
ID: 31470592
Thanks.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2 questions 10 25
export sql results to csv 6 34
get column names from table in vb.net 8 26
Create a WebAPI using a generic return parameter 9 24
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now