Download Using Databases in C2k

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Endgame tablebase wikipedia , lookup

Tandem Computers wikipedia , lookup

DBase wikipedia , lookup

Commitment ordering wikipedia , lookup

Global serializability wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Serializability wikipedia , lookup

Microsoft Access wikipedia , lookup

IMDb wikipedia , lookup

Btrieve wikipedia , lookup

Oracle Database wikipedia , lookup

Functional Database Model wikipedia , lookup

SQL wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Ingres (database) wikipedia , lookup

Open Database Connectivity wikipedia , lookup

PL/SQL wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Concurrency control wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Transcript
Databases: C# and SQL
QUEEN’S UNIVERSITY BELFAST
Database Options
Table of Contents
PRACTICAL 3..................................................................................................................................................... 2
APPROACH 1: SQL Express .......................................................................................................................... 2
APPROACH 2: Service-based Database .................................................................................................. 3
1 | QUEEN’S UNIVERSITY BELFAST
PRACTICAL 3
There are a number of different ways to incorporate an SQL database into a C# project. Each of
the approaches has advantages and disadvantages. We will examine two different approaches
in this document; however, this is by no means an exhaustive list.
APPROACH 1: SQL Express
This is the approach that we use in our weekly lab guides. While the database connection
appears in the Server Explorer in Visual Studio, the database is actually hosted on an SQL Express
server. SQL Express is a separate piece of software and not part of Visual Studio. We favour this
approach as it emulates an approach often used in industry whereby the database server and
the application code (C# in this case) are separated, often on different physical servers.
Important points to note:



In this approach, the database is not integrated into the C# solution. If the C# solution is
moved to another computer (for example, by memory stick, or cloud storage) the
database will not be included. The developer will have to separately export the database
and import it (or create it from scratch) on the new computer.
This approach is supported in the QUB lab. It is also supported on most personal
development computers, assuming that SQL Express was not de-selected during the Visual
Studio install process.
This approach is not supported in the C2k Programming Environment. If you are using the
C2k Programming Environment, please see approach 2.
2 | QUEEN’S UNIVERSITY BELFAST
APPROACH 2: Service-based Database
This approach differs from the previous approach. In this case the database is integrated into the
C# solution. This means that if the C# solution is moved to another computer, the database
moves with the solution. There is no need for the developer to separately export the database
and import it (or create it from scratch) on the new computer. This approach is particularly useful if
solutions are frequently moved between different computers.
The Service-based DB is supported in the C2k Programming Environment. However, if you are
following our weekly lab guides there are a few differences in the database creation and
connection processes:

Creating a Service-based Database:
o Create a new C# project in Visual Studio.
o Right click on the application in the solution explorer and then choose Add>New
Item
o
Choose Service-based Database from the list of Visual C# Items. Don’t forget to give
your database a sensible name. You must keep the .mdf extension.
3 | QUEEN’S UNIVERSITY BELFAST

o
The new Service-based Database will now appear in the Solution Explorer.
o
The new Service-based database will also appear in the Server Explorer. If the
database has a red x beside it, right click on the database and choose Refresh.
o
It is now possible to create tables and insert data into the the new Service-based
database in the usual manner. (Hint: Right click on the database name and choose
New Query from the context menu).
Database connection:
o The process for connecting a Service-based Database to a C# project is very similar
to the process of connecting a SQL Express Database to a C# project.
o
In Practical 2, Task 3, Steps 2 to 6 explain the process for connecting an SQL Express
database to a C# project. To connect a Service-based Database in place of an
SQL Express Database follow Steps 2 to 6. Note the following:
 In Step 3 you will see a different string in the Data Source box. You must copy
this string as it contains all of the properties needed for the string builder
object, which is created in Step 6.
 The property AttachDBFilename will most likely contain an absolute path to
the database .mdf file. It’s a good idea to replace the absolute path with the
special keyword |DatsaDirectory|. For example:

scStrBuild.AttachDBFilename = "C:\project\Database.mdf";
should become

scStrBuild.AttachDBFilename = "|DataDirectory|Database.mdf";
4 | QUEEN’S UNIVERSITY BELFAST
o
Depending on the contents of the Data Source box in Step 3, the connection code
for Step 6 may be similar to this:
public bool connect() {
SqlConnectionStringBuilder scStrBuild = new SqlConnectionStringBuilder();
scStrBuild.DataSource = "(LocalDB)\\v11.0";
localbuilder.AttachDBFilename = "|DataDirectory|Database1.mdf";
scStrBuild.IntegratedSecurity = true;
conn = new SqlConnection(scStrBuild.ToString());
try {
conn.Open();
} catch (SqlException ex) {
Console.WriteLine(ex);
}
if (conn.State == ConnectionState.Open) {
return true;
} else {
return false;
}
5 | QUEEN’S UNIVERSITY BELFAST