Wednesday, June 29, 2011

Character strings and Strings

#include <iostream>
#include <string> //adding the string library

using namespace std;

//function signature
void SimpleArray();
void CharacterArray();
void InputStrings();
void UsingStrings();

int main()
{
//comment or uncomment the one you want to test
//SimpleArray();
CharacterArray();
//InputStrings();
//UsingStrings();
char c;
cin >> c;
}

void SimpleArray()
{

//a basic array
const int SIZE=4; //only constants or literals can be used
int myArray[SIZE];
//assigning values
myArray[0]=23;
myArray[1]=16;
myArray[2]=29;
myArray[3]=30;

//a clumsy way to get the average
//the first part of the equation is cast to a double so
//that there can be decimals in the answer
cout << "The Average is " <<
(double)(myArray[0] + myArray[1] + myArray[2] + myArray[3])/SIZE << endl;

//another way of initializing an array
int myArray2[]={4,34,12,23,2};

//a way of calculating the length of a an array
int arraysize=sizeof(myArray2);
int intsize=sizeof(int);
int arraylength=arraysize/intsize;
cout<< arraysize <<" "<< intsize<<" " << arraylength << endl;


//outputing an element of an array
cout << myArray2[2] << endl;

}

void CharacterArray()
{
//in c character arrays are the way
//you do strings
//in C++ you can do character arrays
//or import the string library
//a literal way to construct a character array
//which you never have to do
//the '\0' is a termination character
char name[]={'S','t','e','v','e','\0'};

//an easier way to initialize a character array
char lastName[]="Conger";

//another character array
char office[]="My Office is in 3176";

//outputing the arrays. Notice, we can just use
//the array names and not have to specificy the particular index
//values
cout << name << " " << lastName << " " << office << endl;
//concating character array litterals
cout << "Steve " "Conger " "\n";
//outputing a single character
cout <<lastName[3] << endl;

//getting the length of a character array
cout << strlen(office) << endl;
}

void InputStrings()
{
//declaring an empty character array of 50 elements
char cityName[50];

cout << "Enter a city Name" << endl;
cin.getline(cityName, 50); //using cin.getline to get the whole line
//it requires the character array name and the size of the character array

cout << "the city you entered is " << cityName <<endl;
}

void UsingStrings()
{
//declare a string variable (using the string library)
string name, city;
cout << "Enter your name" << endl;
getline(cin, name);//a different form of getline using strings
cout << "Enter City " << endl;
getline(cin, city);

string output = name + ", " + city; //string concatination
cout << output << endl;

//getting the length of string
cout << " the length of city " << city.length() <<endl;






}

Tuesday, June 28, 2011

SQL Server Overview

Database Engine


The database engine is the core service of the SQL Server. It runs as a background service and processes On-Line transaction databases OLTP or On-Line Analytic processes (OLAP). It is what actually manages the databases.

Storage Engine


Controls how data is stored on the disk and how applications can access it.
Some elements of the storage Engine Include:
Database File Groups
Tables, Data types and data storage
indexes
partitions
Internal data architecture
Locking and transaction management
Database Snapshots
Data backup and recovery

Security Subsystem


This subsystem allows you secure the server and its contents.

Some elements are:
Authentication Methods
Service Accounts
Enabling and disabling features
Schema
Principles, securables and permissions
Data Encryption
Code signatures
Auditing
Policy Configuration, management and enforcement

Programming Interfaces


These are elements that let you interact with the server programmically.

These Include among others
SQL
stored procedures
triggers
functions
Database snapshots
Full text

Service Broker


Provides a message queuing system.

SQL Server Agent


Used for scheduling tasks and creating alerts.

Replication


Used to distribute copies of data and keep all the copies sychronized to a master data set. Now can make changes all across the network and have them synchronized.

High Availablity


High availablity refers to keeping the server up and running 24/7. To do this SQL Server uses

Failover
Database Mirroring
Log shipping
Replication

Relational Engine


This is part that controls the relations between tables and objects. For a list of new items look a page 8 in the book, or go online to Microsoft's SQL server Page.

Business Intelligence


These are the tools for Data Warehousing, Data Mining and Analysis.

Integration Services


These services all the user to build and automate complex imports and exports.

Reporting Services


Allows you to create Reports on data in SQL Server and post them to the web or sharepoint sites.

Analysis Services


Data cubes, Data Aggregation, etc.

Monday, June 27, 2011

C++ Beginning Examples

Here are the beginning examples of C++ we did in class. They are a bit of a mess, but show a lot of basic features.


#include <iostream>
#include <string>
#include <math.h>

/*********************
this is a multiline comment
the namespace includes all the classes
in the standard (std) library
**********************/
using namespace std;

//main is the starting point of any program

//a fucntion must be declared before it can be used
double Cube(double number);
int Remainder(int num1, int num2);

int main()
{
/********************************
this main is a mess containing many different
examples of different things
**********************************/
//the std:: is unnecessary if you have the using statement above
//if not you must use it. the :: is a "membership operator. cout is a member of
//std. cout means console out
std::cout << "Enter your name \n" ;
char name[10]; //declare a character array--the native way to do strings
std::cin >> name; //cin means console in
std::cout << "Hello, " << name << " out there" << std::endl; //endl=end line
cout << endl;
cout << " Enter a number " << endl;
double number; //declare a variable
cin >> number; //get it
int intNumber=(int)number; //cast a double to an int
//ints drop all decimal parts. they don't round
cout << "intNumber: " << intNumber <<endl <<endl;

//size of function for determining the size of a data type
cout << "int is " << sizeof(int) << " bytes" << endl;
cout << "double is " << sizeof(double) << " bytes" << endl;
cout << " Long is " << sizeof(long) << " bytes" <<endl;

//sqrt comes from the math library included above
cout << "The square root of " << number << " is " << sqrt(number) << endl;

//calling our home made function Cube
cout << "the cube of " << number << " is " << Cube(number) ;

//just showing the put method
cout.put('$');

//get integer variables
cout << "Enter the first integer: " ;
int num1, num2;
cin >> num1;
cout << endl;
cout << "Enter the second integer:";
cin >> num2;

//show the use of a modulus
cout << "The quotient of " << num1 << " divided by " << num2 << " is "
<< num1/num2 <<endl;
cout << "The remainder is " << Remainder(num1,num2);

//this is just to pause the console when in VS
char c;
std::cin >> c;
}

double Cube (double number)
{
return number * number * number;
}

int Remainder(int num1, int num2)
{
//the modules returns the remainder of integer division
return num1 % num2;
}


Here is the first exercise in chapter 3 which asks a user to enter his or her height in inches. The program returns the height in feet and inches.


#include <iostream>

using namespace std;

const int CONVERSION = 12;
int GetFeet(int);
int GetInches(int);
void GetInput();

int main()
{
GetInput();

char c;
cin >> c;
}

int GetFeet(int height)
{
return height / CONVERSION;
}

int GetInches(int height)
{
return height % CONVERSION;
}

void GetInput()
{
cout << "Enter your height in Inches " << endl;
int inches;
cin >> inches;
cout << " You are "
<< GetFeet(inches)
<< " feet tall and "
<< GetInches(inches)
<< " inches " << endl;
}

Friday, June 17, 2011

Doing ANSI C++ in Visual Studio 2010

Start Visual Studio 2010.
Go to FILE/NEW/PROJECT
Choose C++, and a win32 Console Project. Give it a name.



This starts a wizard:



Click Next. On the second screen make sure you select the check boxes for Console and empty project.



Now you should see the following directory structure in the Solution Explorer:



Right click on the Source Files folder and choose new item. Choose a C++ code file and give it a name.



Now type in the following code. It makes a simple Hello world program. The include iostream adds a library with input and output objects such as cout and cin. Every C++ program must have a main method.

SSIS (SQL Server Integration Services) Imports

Overview


SSIS is a way of creating packages that allow you to do complex imports over and over again. The packages can be run periodically to automate the import process. They are really too much work to justify for a one time import, but we are going to so anyway just to get a feel of how to create the packages.

We are going to import a simple and brief file called TacomaEmployees.Text



This would be easy enough just to type in--and if it was for real, that is what I would advise. But we are going to create a package. There are a couple of things to note about the file. While it only has three columns, two go in one table (Person) and one, hiredate, goes in Employee. We also need the PersonID from the person table to link the employee to the name.

Creating the Package



First start a new project with Visual Studio 2008 (this is the stub of VS that SQL Server 2008 installs). Choose the "Integration Services Project." This will open a designer and a toolbox.

Right click in the bottom of the designer and make a new OLEDB Connection



Drag a DataFlow control onto the designer. Name it "AddPerson"



Click on the DataFlow Tab. Add a Flat File Source and name it PersonSource



Double click the PersonSource to Open its properties. Name the Manager EmployeeManager. Click browse and locate the file TacomaEmployees.txt.



Click on Advanced and change the column names to "FirstName", "LastName" and "HireDate." Change the datatype of "HireDate" to Database date.



Now add a SQL Server Destination Control and hook it to the Employee Source:



Double click it to open up the properties. Click on Mapping and make sure the mapping is correct.



Go back to the Control Flow Tab and Add a second DataFlow control hooked to the first. Name it "AddEmployee".



With AddEmployee selected click on the DataFlow tab. Add another FlatFile source and name it EmployeeSource.



You can use the same connection manager (EmployeeManager) as you did for the other flat file source.

Now add a lookup control and connect it to the AddEmployee source.



Double click the control to open the properties. Use the automart connection and the Person table. Click on columns and set it to look like the picture. You can drag from one to the other to get the linking lines.



Now add a SQL Destination control and link it to the lookup.



Configure the SQL Destination control to Insert into Employee:



Now, return to the control flow tab. Add cn Execute SQL Control:



Configure the control. Make sure you choose the automart connection.



For the SQL Statement property click the 3 dots to open up the SQL editor. Type in the SQL statement in the image. This will assign the employees to the last Location which in this case is Tacoma.



Now execute the package, and check SQL Server to see if the data was added.

Troubleshooting


Don't assume I included every tiny step. I tried to be comprehensive, but I could easily have missed something. Read the dialog boxes and use your common sense.

If everything turns green that means success. Yellow means it is in process. Red is an error. I found that if I set the error output on the data flows to "ignore failure" that everything processed correctly: