Manual De C Sharp

Páginas: 30 (7371 palabras) Publicado: 12 de diciembre de 2012
How to Create a Database with SQL Server Express
To create your database, launch your C# .NET software. Start a new Windows project by clicking File > New Project. Call it anything you like, because we won't be using this form. But without SQL Server Management Studio Express installed, you need a new Windows Application to create a SQL Server Express database.
From the menu bars at the top ofC# .NET, click Project. From the Project menu, select Add New Item:
[pic]
When you click on Add New Item, you should see the following dialogue box appear (we've chopped a few templates out for convenience sake):
[pic]
However, for later versions of C# you'll see this instead:
[pic]
Select Service-based database (or SQL Database for older versions).
Give your database a name. Call itMyWorkers.mdf. We'll create a database of fictitious people who work for us, and give them job descriptions.
Click the Add button. In C# 2010 you're then asked to choose a Database model. Leave it on the default of Dataset and click Next. In earlier versions of C#, you'll see the following:
[pic]
Select Tables, and then Finish. (The Dataset is important, and you'll see how they work later).
In C#2010, you'll see this:
[pic]
Just click Finish to return to the C# design environment.
In all C# versions, it may seem as though nothing has happened. But take a look at the Solution Explorer on the right and you'll see that your database has been added to your project:
[pic]
However, your database is empty at the moment. We need to add a table to it. So right click on MyWorkers.mdf. From themenu that appears, click Open.
You should see the Database Explorer appear:
[pic]
Right click on Tables to see the following menu:
[pic]
Select Add New Table from the menu, and a new table will appear in your main window:
[pic]
The Columns are all the Columns that you want in your table. The idea is that you type a Name for the Column, and then specify what kind of data (Data Type) you wantto go in it, such as text, numbers, Yes/No values, etc. Allow Nulls means, "Does this column need to be filled in?" For example, if you had a middle name column then this will often be left blank, because not everybody has a middle name. In which case that particular column can have Nulls. If you need the data, such as an Identifying Number, then you leave the Allow Nulls box unchecked.
We'llcreated a very simple table with just four columns:
Worker_ID
first_Name
last_Name
job_Title
The first Column, Worker_ID, will be a number. We can let the database itself handle this. Every time a new worker is added, SQL Server Express will then take care of assigning a new number for that worker. This is known as Auto Increment in other databases, such as Access.
So type Worker_ID underColumn Name and your screen will look like this:
[pic]
The next thing to do is tell SQL Server Express what kind of data is going in to the Worker_ID column. Click inside of Data Type and you'll see it's a dropdown list of options:
[pic]
As you can see, there's an awful lot of them! Select int from the list, though. Leave the Allow Null box unchecked, and your screen will look like this:
[pic]One more thing to do with this Column. Have a look at the bottom of your screen and you'll see a list of Properties. The one we want is Identity Specification. Set Is Identity to Yes, and Identity Increment and Identity Seed will appear:
[pic]
By setting these values, SQL Server Express will add 1 to the Worker_ID column when we add a new worker.
Click under Column Name and type the second of ourtable fields:
[pic]
For the Data Type, select nvarchar(50). The varchar is short for variable-length character string. With nvarchar, the n is short for Unicode, and the data will be stored in the UTF-16 format. Use nvarchar if you're going to be storing non-English characters. Otherwise, use varchar. The same is true of all the n's on the list.
Check the Allow Nulls box and your new Column...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Manual de c sharp básico
  • C Sharp
  • C sharp
  • C SHARP
  • C Sharp
  • caracteristicas de c sharp
  • Consola C Sharp
  • Wiley

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS