Solved

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

Posted on 2008-06-25
3
238 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
triggered use of sp_send_dbmail failure 2 22
Connection String 16 41
Visual Studio 2015 Source Code Control 10 25
Get Duration of last Status Update 4 29
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

930 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

10 Experts available now in Live!

Get 1:1 Help Now