Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  SQL Server  »  An introduction to SQL Server Management Objects  »  Creating and dropping tables
An introduction to SQL Server Management Objects
by David Jeavons | Published  01/02/2007 | SQL Server | Rating:
Creating and dropping tables

Creating a new table involves creating a Table object and adding new columns to the Table’s Columns collection. Once this is done, you can then call the table’s Create method to create the table on the specified database. The following example creates a basic table containing two columns (IDField and TextField):

    1         Dim db As Database = sqlServer.Databases("MyNewDatabase")

    2         Dim tb As New Table(db, "MyNewTable")

    3 

    4         Dim newColumn As New Column(tb, "IDField", DataType.Int)

    5         tb.Columns.Add(newColumn)

    6 

    7         newColumn = New Column(tb, "TextField", DataType.Char(25))

    8         tb.Columns.Add(newColumn)

    9 

   10         tb.Create()

 

However, the code above does not specify whether the IDField is indexed nor does it specify if the IDField should be used as an Identity column. To do this, we first need to create the indexes and state that the IDField column will be used as an Identity column. The following is the above code with the added statements for creating the index:

 

    1         Dim db As Database = sqlServer.Databases("MyNewDatabase")

    2         Dim tb As New Table(db, "MyNewTable")

    3 

    4         Dim newColumn As New Column(tb, "IDField", DataType.Int)

    5         tb.Columns.Add(newColumn)

    6 

    7         newColumn.Identity = True

    8         newColumn.IdentitySeed = 1

    9         newColumn.IdentityIncrement = 1

   10 

   11         Dim pkIndex As New Index(tb, "IDFieldIndex")

   12         tb.Indexes.Add(pkIndex)

   13         pkIndex.IndexedColumns.Add(New IndexedColumn(pkIndex, newColumn.Name))

   14         pkIndex.IsUnique = True

   15         pkIndex.IndexKeyType = IndexKeyType.DriPrimaryKey

   16 

   17         newColumn = New Column(tb, "TextField", DataType.Char(25))

   18         tb.Columns.Add(newColumn)

   19 

   20         tb.Create()

 

To drop a table you simply call the Drop method of the table object you wish to delete:

 

    1         sqlServer.Databases("MyNewDatabase").Tables("MyNewTable").Drop()

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 03/21/2007)
Rating
coz i dont be run the sample
 
Comment #2  (Posted by an unknown user on 03/23/2007)
Rating
You've got me going with SMO. Nice article.
 
Comment #3  (Posted by an unknown user on 03/23/2007)
Rating
You've got me going with SMO. Nice article.
 
Comment #4  (Posted by Jose on 05/31/2007)
Rating
How would I get a connection string with a specified database as the Initial Catalog?
 
Comment #5  (Posted by David Jeavons on 05/31/2007)
Rating
Hi Jose

I'm not quite sure I understand what you mean, but if you simply want to connect to a particular SQL Server instance then set the ServerInstance property of the ConnectionContext object to the name of the SQL Server of interest.

To automatically use a single database, you can declare a database object and assign it a value from the Databases collection of the Server object. So for example, assuming I was already connected to the SQL Server and I wanted to work with a database called "TestDB" directly then the could would be similar too:

Dim db As Database = Server.Databases("TestDB")

If this is not what you meant, could you clarify your question a bit.


HTH
 
Comment #6  (Posted by paul on 06/11/2007)
Rating
I Get the following error when i try to create stored procedure

BC30057: Too many arguments to 'Public Sub New()'.

at this line

Dim sp As New StoredProcedure , "MyNewStoredProcedure")

 
Comment #7  (Posted by David Jeavons on 06/11/2007)
Rating
The syntax for creating a stored procedure is similar too:

Dim db As Database = sqlServer.Databases("DBName")
Dim sp As New StoredProcedure(db, "sp_Name")

sp.TextMode = False
sp.TextBody = "SELECT FieldList FROM Table"
sp.Create()

Basically, you reference the database where you want to create the stored procedure and then when instantiating a new stored procedure object you specify the database that it will be related to and the name for the new stored procedure.


HTH
 
Comment #8  (Posted by paul on 06/11/2007)
Rating
I get the same error when i tryed to create a new store dprocedure on button click event

error : Compiler Error Message: BC30057: Too many arguments to 'Public Sub New()'.

my click even code is given below :

Dim sqlServer As New Server()
Dim db As Database = sqlServer.Databases("sample")
Dim sp As New StoredProcedure(db, "TestSmo")
sp.TextMode = False
sp.TextBody = "select state_name from state"
sp.Create()

ALL MY SMO COMMAND WORKES EXCEPT STORED PROCEDURE , KINDLY GET ME OUT OF THIS ERROR

 
Comment #9  (Posted by David Jeavons on 06/12/2007)
Rating
Hi Paul

The majority of your code should work as expected, however, I noticed that you are declaring a new server object and then using it immediately to grab a reference to the database of interest. You should in fact first create a connection to the server and then use that connection to create your reference to the database and create the procedure.

The above code example demonstrates connecting to the SQL Server instance of interest and once you have that you should then be able to utilise the code that you have to create the procedure.


HTH
 
Comment #10  (Posted by paul on 06/12/2007)
Rating
Hi David
I'm not able to correct the error till now just go through this code and please correct the lines where i make mistake

Dim connectionstring As String
connectionstring = "Data Source=.;Initial Catalog=sample;Integrated Security=True;Pooling=False"
Dim connection As New SqlConnection(connectionstring)
Dim sqlServer As New Server()
connection.Open()
Dim Server = New ServerConnection(connection)
Dim db As Database = sqlServer.Databases("sample")
Dim sp As New StoredProcedure(db, "testproc")
sp.TextMode = False
sp.TextBody = "SELECT * FROM state"
sp.Create()
Thanks and Regards
 
Comment #11  (Posted by David Jeavons on 06/13/2007)
Rating
Hi Paul

Try the following:

Dim sqlServer As New Server("ServerName")
sqlServer.ConnectionContext.Connect()

Dim db As Database = sqlServer.Databases("sample")
Dim sp As New StoredProcedure(db, "testproc")
sp.TextMode = False
sp.TextBody = "SELECT * FROM State"
sp.Create()

sqlServer.ConnectionContext.Disconnect()


HTH
 
Comment #12  (Posted by paul on 06/13/2007)
Rating
Hi david
still i get error on the line

Dim sp As New StoredProcedure(db, "testproc")

error : Too many arguments to 'Public Sub New()'.

i got a code written in c# which workes fine if possible tell me where lies the problem in vb code


c# code i used

string connectionString = "data source=.;initial catalog=sample;timeout=200;user id=sample; pwd=sample123;";
SqlConnection connection = new SqlConnection(connectionString);
Server server = new Server(new ServerConnection(connection));

Database db = server.Databases["sample"];

StoredProcedure SP = new StoredProcedure(db, "GetstateID");
SP.TextMode = false;
SP.AnsiNullsStatus = false;
SP.QuotedIdentifierStatus = false;

StoredProcedureParameter idParam = new StoredProcedureParameter(SP, "@ID", DataType.Int);
SP.Parameters.Add(idParam);

SP.TextBody = "Select * from state WHERE [ID] = @ID";

SP.Create();

thanks and regards
 
Comment #13  (Posted by paul on 06/28/2007)
Rating
Hi
is there a way to display the records inside the table i.e i would like to use a select statement on a particular table
 
Comment #14  (Posted by an unknown user on 01/03/2008)
Rating
Hi
Is there a way to export and import data from one server to another using SMO ? If so please post the syntax and procedure
 
Comment #15  (Posted by an unknown user on 09/11/2008)
Rating
Nice and Easy
 
Comment #16  (Posted by vaibhav kadian on 09/27/2008)
Rating
the drop command on database doesn't work.
also tried the db.DatabaseOptions.UserAccess = DatabaseUserAccess.Restricted before dropping , it also failed.

any suggestions
 
Comment #17  (Posted by Anke on 10/12/2008)
Rating
don't just hit the back button once, they hit it twice. ,
 
Comment #18  (Posted by Luetta Mcglone on 10/12/2008)
Rating
channel or someday they may well find ,
 
Sponsored Links