0.3 Creating a Database, a Table, and Connecting to the Database Flashcards
1
Q
What are the steps to creating and connecting to a database?
A
- Create the database
- Create tables in the database
- Obtain the connection string
- Connect to the Database, using a data provider
2
Q
How do you create a database in ADO.net?
A
- Open Server explorer in visual studio
- Right click on data connections and select ‘add new connection’ and fill the following information:
- Enter your server name
- Choose windows authentication
- Enter your database name
- Click ok
3
Q
How do you create a table in ADO.net?
A
- Find your database name in server explorer
- Expand the database folder
- Right click on the tables folder and select ‘add new table’
- Enter your table creation command as so:
CREATE TABLE [dbo].[TableName] (
[ColumnName] DATATYPE CONSTRAINTS,
[COLUMNNAME] DATATYPE CONSTRAINTS
) - Click on the update button to create the database
4
Q
In ADO.net how do you obtain the connection string for your database?
A
Right click on the database in server explorer, select properties, and find and copy the database connection string
5
Q
How do you connect to a database in ADO.net?
A
SqlConnection conn = new SqlConnection(‘connection string’);
SqlCommand cmd = new SqlCommand(“Sql Query”, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();