C# First dapper App Flashcards

(21 cards)

1
Q

Get connectionString from app.config

A

ConfigurationManager.ConnectionStrings[name].ConnectionString

(In the static helper class using a static method)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Add connection string in config.app

A

In configuration

< add name=“sampledb” connectionstring=“server=.;database=sample;trusted_connection=true;” ProviderName= “system.data.sqlClient”>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Step to have a simple c# app connected to sql with windows form

A
Define class
Add helper to get connection string
Define connectionstring in config.app
Define ui
Add dataaccess class
Use an instance of dataaccess to get the result (usually db)
Add dapper reference in DataAccess class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to not make crash a function that you havent implement yet

A
Add in the code:
Throw new NotaimplementedException();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Connect a list to a listbox

A

Mylistbox.DataSource=peopleList
MyListBox.DisplayMember=“myProperty”

With peopleList is a list.
No need to have a bindingsource anymore.
The property is in double quote, make sure to write it properly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Property that return a string that is a list of existing property using $ sign

A

Return $”{property1} {property2} ({property3})”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Add dapper reference

A

Go to reference, right click, select manage nuget packages,browse dapper, can add using dapper at the top of your dataAccess class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Using statement with dapper

A

Using (IDBConnection connection =system.data.sqlclient.sqlconnection(myConnectionString))
{

}

If you have an helper, instead of myConnectionString then
Helper.connval(myname);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dapper query with direct sql and result is return by a function

A

Return Connection.query($”select * from people where name=‘{myName}’ “).ToList();

With connection of type IDBConnection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dapper query with store procedure

A

Var output= connection.Query(“dbo.mystoreProcedure @myProcVariable”,new { myProcVariable=myfunctionName} )

We create a new dynamic class instance with {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Insert record with Dapper

A

Using line is the same. Need to create a list of element to insert even if only one.

connection.execute(“addperson @first name @lastname @email”, people)

People is the list of object:
List People=new List()
people.Add(new {FirstName=“John”,LastName=“Doe”};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create instance of a class and set property right away (no constructor)

A

Person myPerson = new Person {Firstname=“x”, LastName=“y”…};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Search people button function

A
Private void SearchButtonClick(object sender, eventArgs e)
{
DataAccess db=new DataAccess();
People=db.GetPeople(seachtextbox.text);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Dapper: select query with join person-cell Phone

A

String sql=@”select pe.,ph. from dbo.Person pe left join dbo.Phone ph on pe.CellPhoneId=ph.id;”;
Var people=conn.Query(sql,(person, phone)=> {person.CellPhone=phone; return person;});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

display person firstname, last name and phone from people

A

Foreach( var p in people)
{
Console.WriteLine($”{p.FirstName} {p.LastName} Cell: {p.CellPhone?.CellNumber}”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Dapper: select query with join person-cell Phone with parameter

A
var p=new { LastName=lastName}; 
String sql=@”select pe.*,ph.* from dbo.Person pe left join dbo.Phone ph on pe.CellPhoneId=ph.id where p.LastName=@LastName;”;
Var people=conn.Query(sql, (person, phone) => {person.CellPhone=phone; return person;},p);
17
Q

Dapper, multiple sql queries

A
String sql=”select * from dbo.Person; select * from dbo.Phone;”
List people = null;
List phones=null;
Using(var Lists=conn.QueryMultiple(sql))
{
people=Lists.read().ToList();
phones=Lists.Read().Tolist();
}
18
Q

Dapper, multiple sql queries with parameters: last name and partial phone number

A
String sql=”select * from dbo.Person where LastName=@LastName; select * from dbo.Phone where PhoneNumber like ‘%’ + PartialPhoneNumber + ‘%’ ;”
Var p = new
{
LastName=lastName;
PartialPhoneNumber=partialPhoneNumber;
}
List people = null;
List phones=null;
Using(var Lists=conn.QueryMultiple(sql,p))
{
people=Lists.read().ToList();
phones=Lists.Read().Tolist();
}
19
Q

Transaction: Insert person then try to update all id to 1.

A

String sql=$@ “INSERT INTO dbo.Person (FirstName,LastName) VALUES (@FirstName,@LastName);”;
Var p= new DynamicParameter();
p.Add(“@FirstName”,firstName);
p.Add(“@LastName”,lastName);
conn.Open();
using (var trans =conn.BeginTransaction())
{
Int recordUpdated=conn.execute(sql,p,trans);
Try { conn.Execute(“Update dbo.Person SET id=1”); trans.Commit;}
Catch (Exception ex) { Console.WriteLine($“Error :{ex.Message}“) ; trans.RollBack(); }
}

20
Q

Define DataTable (firstName, LastName) and put data in it

A
Private Static DataTable getTroopers()
{
Var Output=new DataTable();
Output.Columns.Add(“FirstName”,typeof(string));
Output.Columns.Add(“LastName”,typeof(string));
Output.Rows.Add(“John”,”Smith”);
Output.Rows.Add(“Lea”,”Doe”);
Return Output
}
21
Q

Using get troopers and UDT to insert multiple records

A
var troopers=getTroopers();
var p =new
{ people=troopers.AsTableValuedParameter(BasicUDT) };
Int recordAffected=conn.Execute(“dbo.PersonInsert,p,commandType: commandType.StoredProcedure);