0% found this document useful (0 votes)
66 views7 pages

Using System

This document shows code for creating a new database table in SQL Server using SQL Server Management Objects. It establishes a connection to the database server, selects a database, defines columns and data types for the new table, creates a primary key index on the ID column, and calls Create() to physically build the table in the database. The code demonstrates how to programmatically generate database objects using a .NET library for SQL Server administration.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views7 pages

Using System

This document shows code for creating a new database table in SQL Server using SQL Server Management Objects. It establishes a connection to the database server, selects a database, defines columns and data types for the new table, creates a primary key index on the ID column, and calls Create() to physically build the table in the database. The code demonstrates how to programmatically generate database objects using a .NET library for SQL Server administration.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

using System;

namespace [Link]

// The Vector and GeneralMatrix classes reside in the

// [Link] namespace.

using [Link];

/// <summary>

/// Illustrates operations on GeneralMatrix objects and combined

/// operations on Vector and GeneralMatrix objects from the

/// [Link] namespace of the Extreme


Optimization

/// Mathematics Library for .NET.

/// </summary>

class MatrixVectorOperations

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

// For details on the basic workings of Vector

// objects, including constructing, copying and

// cloning vectors, see the BasicVectors QuickStart

// Sample.

//
// For details on the basic workings of GeneralMatrix

// objects, including constructing, copying and

// cloning vectors, see the BasicVectors QuickStart

// Sample.

//

// Let's create some vectors to work with.

Vector v1 = new GeneralVector(1, 2, 3, 4, 5);

Vector v2 = new GeneralVector(1, -2, 3, -4, 5);

[Link]("v1 = {0}", v1);

[Link]("v2 = {0}", v2);

// These will hold results.

Vector v;

// Also, here are a couple of matrices.

// We start out with a 5x5 identity matrix:

GeneralMatrix m1 = [Link](5);

// Now we use the GetDiagonal method and combine it

// with the SetValue method of the Vector class to

// set some of the off-diagonal elements:

[Link](1).SetValue(2);

[Link](2).SetValue(3);

[Link](-1).SetValue(4);

[Link]("m1 = {0}", m1);

// We define our second matrix by hand:

GeneralMatrix m2 = new GeneralMatrix(5, 5, new double[]

1, 2, 3, 4, 5,

1, 3, 5, 7, 9,
1, 4, 9, 16, 25,

1, 8, 27, 64, 125,

1, -1, 1, -1, 1

});

[Link]("m2 = {0}", m2);

[Link]();

// This one holds the results:

Matrix m;

//

// Matrix arithmetic

//

// The Matrix class defines operator overloads for

// addition, subtraction, and multiplication of

// matrices.

// Addition:

[Link]("Matrix arithmetic:");

m = m1 + m2;

[Link]("m1 + m2 = {0}", m);

// Subtraction:

m = m1 - m2;

[Link]("m1 - m2 = {0}", m);

// Multiplication is the true matrix product:

m = m1 * m2;

[Link]("m1 * m2 = {0}", m);

[Link]();
//

// Matrix-Vector products

//

// The GeneralMatrix class defines overloaded addition,

// subtraction, and multiplication operators

// for vectors and matrices:

[Link]("Matrix-vector products:");

v = m1 * v1;

[Link]("m1 v1 = {0}", v);

// You can also multiply a vector by a matrix on the right.

// This is equivalent to multiplying on the left by the

// transpose of the matrix:

v = v1 * m1;

[Link]("v1 m1 = {0}", v);

// Now for some methods of the Vector class that

// involve matrices:

// Add a product of a matrix and a vector:

[Link](m1, v1);

[Link]("v + m1 v1 = {0}", v);

// Or add a scaled product:

[Link](-2, m1, v2);

[Link]("v - 2 m1 v2 = {0}", v);

// You can also use static Subtract methods:

[Link](m1, v1);

[Link]("v - m1 v1 = {0}", v);


[Link]();

//

// Matrix norms

//

[Link]("Matrix norms");

// Matrix norms are not as easily defined as

// vector norms. Three matrix norms are available.

// 1. The one-norm through the OneNorm property:

double a = [Link]();

[Link]("OneNorm of m2 = {0}", a);

// 2. The infinity norm through the

// InfinityNorm property:

a = [Link]();

[Link]("InfinityNorm of m2 = {0}", a);

// 3. The Frobenius norm is often used because it

// is easy to calculate.

a = [Link]();

[Link]("FrobeniusNorm of m2 = {0}", a);

[Link]();

// The trace of a matrix is the sum of its diagonal

// elements. It is returned by the Trace property:

a = [Link]();

[Link]("Trace(m2) = {0}", a);

// The Transpose method returns the transpose of a

// matrix. This transposed matrix shares element storage


// with the original matrix. Use the CloneData method

// to give the transpose its own data storage.

m = [Link]();

[Link]("Transpose(m2) = {0}", m);

[Link]("Press Enter key to exit...");

[Link]();

/ Establish the database server


string connectionString = "...";
SqlConnection connection =
new SqlConnection(connectionString);
Server server =
new Server(new ServerConnection(connection));

// Create table in my personal database


Database db = [Link]["davidhayden"];

// Create new table, called TestTable


Table newTable = new Table(db, "TestTable");

// Add "ID" Column, which will be PK


Column idColumn = new Column(newTable, "ID");
[Link] = [Link];
[Link] = false;
[Link] = true;
[Link] = 1;
[Link] = 1;

// Add "Title" Column


Column titleColumn = new Column(newTable, "Title");
[Link] = [Link](50);
[Link] = false;

// Add Columns to Table Object


[Link](idColumn);
[Link](titleColumn);

// Create a PK Index for the table


Index index = new Index(newTable, "PK_TestTable");
[Link] = [Link];

// The PK index will consist of 1 column, "ID"


[Link](new IndexedColumn(index,"ID"));

// Add the new index to the table.


[Link](index);

// Physically create the table in the database


[Link]();

 
You can easily make this code generic and wrap it up as a method in a class that creates
tables using SQL Server Management Objects.

You might also like