Skip to content

C Programming Intermediate Lecture 3 How to do Database Operations in NET Programmatically

FurkanGozukara edited this page Nov 16, 2026 · 1 revision

C# Programming (Intermediate) - Lecture 3: How to do Database Operations in .NET (Programmatically)

C# Programming (Intermediate) - Lecture 3: How to do Database Operations in .NET (Programmatically)

image Hits Patreon BuyMeACoffee Furkan Gözükara Medium Codio Furkan Gözükara Medium

YouTube Channel Furkan Gözükara LinkedIn Udemy Twitter Follow Furkan Gözükara

C# Programming (Advanced/Intermediate) Lectures (3/14) will teach you #CSharp #programming from beginner to intermediate/expert level. This course is an excellent source to learn advanced concepts in C# #coding. If you are a beginner to C#, watch our previous course lectures. The playlist link of the previous course is below.

Full playlist of the previous course: C# For Beginners ⤵️

https://www.youtube.com/playlist?list=PL_pbwdIyffskoSXySh0MdiayPJsBZ7m2o

Discord ➡️ https://bit.ly/SECoursesDiscord

Patreon ➡️ https://www.patreon.com/SECourses

Technology Playlist (30+ Tutorial / Guide / News / Review Videos) ⤵️

https://www.youtube.com/playlist?list=PL_pbwdIyffsnkay6X91BWb9rrfLATUMr3

Stable Diffusion Playlist (30+ Tutorial / Guide Videos) ⤵️

https://www.youtube.com/playlist?list=PL_pbwdIyffsmclLl0O144nQRnezKlNdx3

Please join discord, mention me and ask me any questions. Thank you for like, subscribe, share and Patreon support. I am open to private consulting with Patreon subscription.

Full playlist of this course ⤵️

https://www.youtube.com/playlist?list=PL_pbwdIyffslHaBdS3RUW26RKzSjkl8m4

You can watch for free our C# Programming, ASP .NET Core MVC programming, Machine Learning and Software Engineering related courses on our playlists page ⤵️

https://www.youtube.com/@SECourses/playlists

GitHub code repository of this course (source codes of the lectures) ⤵️

https://github.com/FurkanGozukara/Introduction-to-Programming-2-2021

The topics that have been covered in Lecture 3:

  • How to build query string of .NET SQL connection

  • How to get server path with select @@servername

  • How to properly construct SQL queries and SQL connections in C#

  • The importance of using statements and how to use using statements and what using statements do

  • How to write update / delete / insert / select query with SQL connection class in .NET

  • How to see executed queries in SQL server using SQL server profiler - the trace of SQL server management studio

  • How to sort select query results

  • What is the difference between permanent connection usage and SQL server pooling with SQL connection opening and closing each time

  • How to measure a method performance with StopWatch

  • How to write multi-line string with @ and how to use inline variables with $

  • How to compose a database class to handle database operations efficiently and properly

  • How to use the "where" predicate to filter update / delete / select queries

  • How to write "like" filtering

  • How to use "or" and "and" predicates when filtering database operation

  • Where predicate applies to all select, update, and delete queries

Introduction

Developing applications that interact with databases is a fundamental aspect of software development. For .NET developers, it is essential to know how to properly construct SQL queries and SQL connections in C# to optimize performance and ensure efficient use of resources.

Building Query String of .NET SQL Connection

A query string is a part of a URI (Uniform Resource Identifier) that contains information to be passed to the server. In .NET, building a query string of an SQL connection involves specifying the connection string, which is a string that contains information about the data source, database name, user ID, and password.

The SELECT @@SERVERNAME command in SQL Server returns the name of the server that is currently being used. This command can be useful when you need to obtain the server path to connect to the database.

Properly Constructing SQL Queries and SQL Connections in C#

When constructing SQL queries and SQL connections in C#, it is important to follow best practices to optimize performance and ensure efficient use of resources. Properly constructing SQL queries involves using parameterized queries instead of concatenating user input to avoid SQL injection attacks. Parameterized queries allow you to separate the query logic from the user input and improve the security of the application.

Properly constructing SQL connections involves using connection pooling to avoid the overhead of establishing a new connection for each request.

The Importance of Using Statements and How to Use Them

Using statements are used to ensure that an object is disposed of properly after it is no longer needed. In the context of database connectivity, using statements are used to ensure that an SQL connection is closed after it is no longer needed.

To use an SQL connection with a using statement, create a new connection object, open the connection, execute the query, and close the connection within the using statement. The using statement will ensure that the connection is disposed of properly.

Writing Update / Delete / Insert / Select Queries with SQL Connection Class in .NET

To write SQL queries with the SQL connection class in .NET, create a new connection object, open the connection, create a command object, set the command text, execute the query, and close the connection.

Video Transcription

  • 00:00:01 Hello dear students, welcome to the Advanced Programming with C-Sharp Course Lecture 3.

  • 00:00:07 You know our name is also known as Introduction to Programming 2 as well.

  • 00:00:12 So today we will be doing database operations with using C-Sharp rather than using the SQL

  • 00:00:22 Server Management Studio.

  • 00:00:24 Like we have shown in Lecture 2.

  • 00:00:27 I am starting with a new project and I will compose a WPF app .NET project.

  • 00:00:35 And let's pick our folder, okay, Lecture 3.

  • 00:00:48 Okay database manipulation is one of the most important aspects of programming in the today's

  • 00:00:59 world because almost all of the applications that you are going to develop will use a kind

  • 00:01:06 of database.

  • 00:01:07 And if you know the science of TSQL which is common in many of the different database

  • 00:01:15 systems, relational database systems such as Microsoft SQL Server or SQL Lite or Oracle

  • 00:01:24 or MySQL and such, you won't have hard time to write code for each of them, any of them.

  • 00:01:34 Okay so let's also open our SQL Management Server because usually I design my database

  • 00:01:45 and tables from the SQL Server Management Studio and then do the operations with my

  • 00:01:54 program.

  • 00:01:57 So for designing tables, structures, indexes, columns, basically the database using the

  • 00:02:12 SQL Server Management Studio is better.

  • 00:02:14 So you see I can either log in with SA, System Administrator or Windows Authentication.

  • 00:02:20 This is the more common way.

  • 00:02:22 So either way works, let's connect with Windows Authentication.

  • 00:02:28 Okay we had a school database if you remember which had students table.

  • 00:02:42 And here are records that we have generated the previous lecture.

  • 00:02:49 So let's start with adding a button to delete all the records in the students table.

  • 00:03:04 Okay so I will name it as read all students in students table.

  • 00:03:13 Okay let's name it as, okay.

  • 00:03:25 So basically, we can write the same statement and execute it in C sharp.

  • 00:03:39 So there are several types of commands and I will show you each of them.

  • 00:03:51 So this is update command.

  • 00:03:54 Therefore, let's use this code.

  • 00:04:02 I will start from simple version and then I will explain you up to advanced version.

  • 00:04:10 So we use SQL connection class which is not available in our references right now.

  • 00:04:18 Therefore I will add the necessary class.

  • 00:04:26 Okay SQL connection uses, yes SQL data using stem data.

  • 00:04:38 So let's edit.

  • 00:04:46 Okay once I edit it, it is still not available.

  • 00:04:54 Okay looks like I have to install its package because right now I am using .NET Core.

  • 00:05:01 Therefore it is not available so find and install with latest version.

  • 00:05:07 I will click it.

  • 00:05:08 I think it will use NuGet packages.

  • 00:05:13 Let's see it.

  • 00:05:17 Installing system data SQL client completed.

  • 00:05:23 You see at the left bottom of the screen and then we click the frameworks and packages.

  • 00:05:30 Okay we see the package and we can see its version.

  • 00:05:34 It is here.

  • 00:05:36 I wonder if it is available in the NuGet.

  • 00:05:43 Yes you see from NuGet it has installed system data SQL client by Microsoft.

  • 00:05:49 It has been installed how many times?

  • 00:05:59 Interesting it is not displaying how many times it has been installed.

  • 00:06:06 But anyway, probably millions of times.

  • 00:06:11 Okay and then we need a connection string.

  • 00:06:16 Okay so let's define our connection string here equal to okay, so this is a kind of tricky

  • 00:06:26 part and therefore watch this part closely.

  • 00:06:34 So I can use either Windows authentication or system admin authentication which you have

  • 00:06:41 to set a system password when you are installing.

  • 00:06:45 So for the easy for easily solving it.

  • 00:06:51 I will show the Windows authentication.

  • 00:06:54 So the server is localhost by default and database.

  • 00:06:59 Okay so this is important which database we will use to get your full path of your database.

  • 00:07:10 You need to execute a command as select add server name.

  • 00:07:14 So this is important.

  • 00:07:17 Our server name is this one for this computer.

  • 00:07:21 This changes according to your computer.

  • 00:07:24 So this one and then the database name by default.

  • 00:07:29 You see previous lecture I have shown you that you have to either write the full path,

  • 00:07:36 dbo, and table name or if you have selected the database from here you can just write

  • 00:07:46 it as like this.

  • 00:07:51 Okay so this is important and I have to put from we will define our default database name.

  • 00:08:03 Okay, therefore we won't be needed to write full path of the queries.

  • 00:08:12 This is the easier way I think this is the correct.

  • 00:08:17 Okay now we are going to compose an SQL connection object from SQL connection class and then

  • 00:08:26 we have to open the connection.

  • 00:08:28 By the way, this is not appropriate way of doing it, but I will show you from the simplest

  • 00:08:36 way to do advanced and proper way.

  • 00:08:40 So I open my connection and okay I will remove the using for as a beginning it will execute

  • 00:08:52 a command.

  • 00:08:53 Okay, so the command let's say will be the command of the SQL query.

  • 00:09:02 Okay delete from the table name which is here.

  • 00:09:09 We have students and that's all that's it.

  • 00:09:14 And then this one.

  • 00:09:16 Okay, so this command will return us okay.

  • 00:09:22 After command is initialized, we will execute with okay I have to remove unnecessary okay,

  • 00:09:30 non query.

  • 00:09:31 Why non query because it will not return any results to us okay.

  • 00:09:46 Uses for update, lead, and insert.

  • 00:09:48 Okay and this will return us integer.

  • 00:09:52 Why?

  • 00:09:53 Because it will return the number of rows affected and let's say deleted.

  • 00:10:00 We can say as rows deleted and we can show with deleted.

  • 00:10:12 Let's say equal to this one and we can format it okay,

  • 00:10:26 let's first add our dollar sign.

  • 00:10:33 Okay, now let's go step by step.

  • 00:10:41 So I have put a break point here.

  • 00:10:42 I am running my application all right.

  • 00:10:50 Okay the application is live.

  • 00:11:08 When I click the button, it is starting from here.

  • 00:11:10 Step over step over.

  • 00:11:12 Okay, the connection is defined.

  • 00:11:14 When we hover our mouse, we can see the default parameters you see data source, database name,

  • 00:11:22 and other things.

  • 00:11:25 Access token client connection ID connection string connection timeout you see the connection

  • 00:11:32 timeout is by default 15 seconds and you may want to change that for longer taking queries.

  • 00:11:40 When I step over it will open connection and if there is an error in my string, it will

  • 00:11:49 throw an error.

  • 00:11:52 Okay, so it says this cannot open database.

  • 00:11:58 Okay, the login failed for users.

  • 00:12:04 Okay, let's change the syntax like this since this is a special character I escape it with

  • 00:12:15 another one I wasn't sure with the syntax.

  • 00:12:20 So I may be writing the server name incorrect

  • 00:12:31 as well.

  • 00:12:33 Let's continue.

  • 00:12:39 Yeah, probably we are writing the name incorrectly.

  • 00:12:46 Okay, yes, okay.

  • 00:12:57 I think for server we will type like this and for the database we will type like this.

  • 00:13:05 So I'm writing the full path of the server because when we do a select server name, we

  • 00:13:12 get this.

  • 00:13:14 Okay, let's try this way.

  • 00:13:18 This is integrated security SSPI security personal security information post trust connection.

  • 00:13:24 Yes, these are some default parameters.

  • 00:13:28 Okay, now my connection is working.

  • 00:13:32 You see the connection open.

  • 00:13:33 Didn't throw any error then I define my command with command, query and connection object.

  • 00:13:42 Okay, then I set the rows deleted.

  • 00:13:46 You see it has deleted 73 rows.

  • 00:13:49 Then it will show message.

  • 00:13:51 Okay, however, there are some big problems in here.

  • 00:13:58 I didn't close this connection so you may think that what error it may cause.

  • 00:14:05 Okay, so let me illustrate you to that if you leave.

  • 00:14:10 If you open connections and do not close them properly, do not dispose them properly.

  • 00:14:16 Then you have a big problem.

  • 00:14:18 What kind of big problem, big problem you may have.

  • 00:14:23 Let's test the test connection.

  • 00:14:26 Leave open.

  • 00:14:31 So I will put a void.

  • 00:14:36 Okay.

  • 00:14:37 I will copy and paste this.

  • 00:14:40 By the way, you should never have any copy paste code in your software.

  • 00:14:45 Okay, so how am I going to test that for testing it?

  • 00:14:52 Let's add another button to illustrate and name.

  • 00:15:10 Okay, okay or let's name it as test not closing connection problem.

  • 00:15:25 Okay name.

  • 00:15:30 And by the way you see, it puts an x here.

  • 00:15:35 It doesn't make any difference in practice so you can ignore it or you can check the

  • 00:15:40 internet for what it is.

  • 00:15:42 Okay.

  • 00:15:43 I will do a for loop I will make it run whatever and here I will just call this method.

  • 00:15:52 So whenever this method is called, it will open a connection but it will not close it.

  • 00:16:00 Okay, so these objects will remain in the garbage collection and these connections to

  • 00:16:06 the SQL server will remain open.

  • 00:16:09 Okay, even if this connection object gets let's say disposed of, the connection will

  • 00:16:17 still remain open with SQL server.

  • 00:16:20 So let's see what happens when we run this code.

  • 00:16:30 In the early versions it was throwing an error I think it is not changed so it should throw

  • 00:16:37 an error but I am not sure.

  • 00:16:42 And meanwhile, let's see the number of open connections from the SQL server.

  • 00:16:47 Okay, we got our error.

  • 00:16:50 So you see timeout expired.

  • 00:16:51 The timeout period elapsed prior to the obtaining a connection from the pool.

  • 00:16:56 This may have occurred because all pooled connections were in use and max pool size

  • 00:17:02 reached.

  • 00:17:03 So you see after a while you cannot open any more connections.

  • 00:17:07 Let's see our loop count where we are.

  • 00:17:11 Okay, it doesn't show because it happened inside another method, but I would expect

  • 00:17:21 to see it.

  • 00:17:22 Okay anyway, so let's see.

  • 00:17:32 Okay, there is a total number of connections here.

  • 00:17:51 Okay, it says 41 and it should get reduced.

  • 00:17:58 It is not getting reduced to reset them.

  • 00:18:01 You can do task manager and services and here is Microsoft Microsoft SQL server and you

  • 00:18:11 can do restart.

  • 00:18:12 It will restart the services.

  • 00:18:14 Okay running again when I do.

  • 00:18:19 Okay now 18 connections, 28 connections.

  • 00:18:22 Okay, so let's rerun our application and watch the number of connections getting opened and

  • 00:18:32 then we can observe.

  • 00:18:34 Okay, 124.

  • 00:18:35 Okay, after 124 connections, we are not able to open any more connections.

  • 00:18:42 So the pool size is about 100 I suppose.

  • 00:18:46 Okay, therefore what we need to do.

  • 00:18:49 Okay, what can we do?

  • 00:18:52 Either you have to close all connections after you have opened them or use the same connection

  • 00:19:01 object.

  • 00:19:02 But in my applications what I do is after the command is executed I close the connection

  • 00:19:09 like this.

  • 00:19:10 Okay, so let's retry and see what happens if any problem will occur or not.

  • 00:19:29 Okay 41 okay, you see 42 41, 42 or 41.

  • 00:19:32 Why?

  • 00:19:33 Because it is opening a connection, executing a query, and closing it.

  • 00:19:36 By the way, we can also watch the executed queries with tools SQL server profiler and

  • 00:19:47 here: Windows authentication desktop here I will pick tuning so we can see, run and

  • 00:19:57 okay here the query is getting executed.

  • 00:20:00 Okay, okay, you see delete from.

  • 00:20:02 By the way, let's pause it for a while.

  • 00:20:07 Delete from TBS students these are the queries that is getting executed.

  • 00:20:13 This is the server query SV reset connection and delete from students reset connection

  • 00:20:20 because we are opening connections and closing it right now.

  • 00:20:24 Okay, so let's close this.

  • 00:20:27 Okay, the all 10,000 query has executed.

  • 00:20:33 You see 100,000 actually, it is even bigger than that.

  • 00:20:37 Okay, what I want to do is I will do another test so we can test.

  • 00:20:47 Okay, something like this.

  • 00:20:49 Okay, I will remove this.

  • 00:20:53 Okay, test connection, open, close here, and test single connection.

  • 00:21:11 So here what I'm going to do is I will have an instance of an object connection permanent

  • 00:21:24 for example.

  • 00:21:27 Okay, the connection string is defined here.

  • 00:21:31 Therefore, it should work.

  • 00:21:33 Okay, this is non-static oh I see I have to make this static.

  • 00:21:46 Okay, okay, so in the test single connection, we will test.

  • 00:21:52 This is connection open state equal to connection state dot open yeah I think we can do that

  • 00:22:10 and if it is not equal to open what we are going to do is we will define a new connection

  • 00:22:20 instance and connection permanent dot open.

  • 00:22:24 Okay, so this should solve all our initialization problem.

  • 00:22:30 I have the connection I have opened it, then I can use connection permanent here and I

  • 00:22:39 will leave it open.

  • 00:22:42 However, I won't be opening it all the time.

  • 00:22:46 Okay, so what I'm going to do is I will use it a stopwatch.

  • 00:22:52 Okay, stopwatch.

  • 00:22:55 I think we need to add reference of it.

  • 00:23:00 Okay, using system diagnostics, stop timer equal to need stopwatch.

  • 00:23:09 Okay, first, what I'm going to do is simply I will test the performance of both approaches

  • 00:23:19 and see if there is any difference.

  • 00:23:24 Let's first let's define the first on a stop timer open close and stop timer.

  • 00:23:35 Or let's say watch timer better naming watch timer parent connection.

  • 00:23:47 Okay, so let's run one 10 10k connection first.

  • 00:23:58 I will start this okay and then stop this and then okay another one this time.

  • 00:24:11 I will start permanent, stop it here and I will call the test single connection.

  • 00:24:20 Then I will copy and paste them or we can do a force query.

  • 00:24:28 I will run the queries two times so it will be more.

  • 00:24:34 Let's say precise or correct and at the final I will message box show open, close approach.

  • 00:24:49 Total time will be equal to okay.

  • 00:24:55 Watch timer open close.

  • 00:25:04 Total elapsed elapsed okay, elapsed milliseconds and over 1000 or less.

  • 00:25:22 Let's keep it that way and zero and milliseconds or let's say ms and permanent connection approach:

  • 00:25:39 total time equal to other one permanent.

  • 00:25:51 Okay, so okay and let's add a new line here like this.

  • 00:26:10 Okay, so now we are going to conduct an experiment of approaches I prefer open and close.

  • 00:26:22 However, I am not sure which one is more suitable.

  • 00:26:28 So let's test it.

  • 00:26:31 Okay, we have 123.

  • 00:26:36 Oh, because we have used incorrect methods.

  • 00:26:45 What we need to do is call these methods.

  • 00:26:50 Okay, so okay we have 26 connection and let's see our cpu usage with which processes.

  • 00:27:12 Okay, so let's see.

  • 00:27:14 Okay, now it is testing.

  • 00:27:17 You see, sql server is using about 20 percent of cpu because it's keeping it's keep executing

  • 00:27:27 queries against the database.

  • 00:27:35 Okay, so the open close approach took 6300 ms and the permanent connection approach took

  • 00:27:51 around 4000 ms.

  • 00:27:52 I wonder if this is very correct?

  • 00:27:57 So let's increase the number of loop to test it more.

  • 00:28:03 By the way, if you don't reset the stopwatch, it will keep the elapsed time.

  • 00:28:08 So if you want to reset the stopwatch, you have to reset it with reset method call.

  • 00:28:15 Okay, let's retest.

  • 00:28:35 If we use this approach, leave one connection permanent.

  • 00:28:43 It has a drawback.

  • 00:28:45 Why?

  • 00:28:46 Because if you are doing a multi-threaded programming, then if multiple threads try

  • 00:28:53 to access your connection at the same time, it may cause problems.

  • 00:29:01 I am not sure if it is thread safe so let's check it out.

  • 00:29:12 I am not sure if it is thread safe or not.

  • 00:29:26 Okay, so they suggest you to open and close it and therefore use the pooling.

  • 00:29:44 However, that is not the answer I am looking for.

  • 00:29:54 Okay, so the preferred way is opening a connection and closing it each time when you are doing

  • 00:30:09 a query.

  • 00:30:10 That is the answer.

  • 00:30:12 So it is probably not thread safe.

  • 00:30:14 Okay, so usually they have close time.

  • 00:30:19 However, opening and closing the connection is preferred method.

  • 00:30:24 They are very close in terms of performance.

  • 00:30:28 On the contrary.

  • 00:30:30 That's absolutely what you should do.

  • 00:30:32 That's the behavior sql connection was designed for.

  • 00:30:35 You should use a using statement to automatically close the connection at the end of the block

  • 00:30:40 you're using it for and the connection pool mechanism will automatically handle the real

  • 00:30:44 underlying connections to the database.

  • 00:30:47 Okay, so what else we need to do to make our connections proper?

  • 00:30:54 Okay, first of all, you have to use using what does that mean?

  • 00:31:02 That means that instead of calling connection close because if an error happens when you're

  • 00:31:12 executing your command here, this will not get called.

  • 00:31:17 So the proper way is encapsulating any statement with using if it is supporting that like this.

  • 00:31:29 So what does this means?

  • 00:31:32 This means that whatever happens inside here after this block of code is executed, even

  • 00:31:40 if it even if it, let's say throws an uncalled error, the connection object will be disposed

  • 00:31:56 of okay so therefore you have to use it this way.

  • 00:32:09 And if you do this way, you don't have to execute the command close and here sql command.

  • 00:32:17 It also supports using I believe.

  • 00:32:21 Okay, it's and then like this.

  • 00:32:28 So whatever inside using statement will be get automatically disposed of after the using

  • 00:32:35 statement.

  • 00:32:36 And okay, so this is the proper way with using encapsulation.

  • 00:32:49 Okay, and let's rerun and see if there is any difference.

  • 00:32:54 Probably there won't be any difference, but by the way, this is still not the proper way.

  • 00:33:04 Why?

  • 00:33:07 Because whenever you are going to do an sql query execution, you would be needed to do

  • 00:33:17 a copy paste code.

  • 00:33:18 Okay, like this.

  • 00:33:21 I see that in some of the students' projects and that is absolutely not the correct way

  • 00:33:29 in your programming.

  • 00:33:31 You should never write the same code twice.

  • 00:33:34 Okay, what you need to do is do turn your programming into methods and do method calls

  • 00:33:46 or classes and do do inheritance to avoid duplicate code writing.

  • 00:33:53 Okay, I will show them to you.

  • 00:33:56 Don't worry.

  • 00:33:57 Okay, so open, close approach and permanent.

  • 00:34:01 Okay, they are pretty close I can.

  • 00:34:04 I would say that the performance difference can be ignored because they are almost same.

  • 00:34:11 However, this is the way it is in 10 intended to be used.

  • 00:34:18 Okay, so now this is the way we execute: update, delete, or insert.

  • 00:34:28 Let's say let's write a method to insert 100 random students.

  • 00:34:46 Okay

  • 00:35:10 okay so for inserting students what we need to do, it is same first first write our base

  • 00:35:19 query as insert into the table name.

  • 00:35:26 It is cool as you know I mean tbs students.

  • 00:35:30 By the way, I don't write the query like this.

  • 00:35:33 Why?

  • 00:35:35 Because in my connection string here I have defined the database name.

  • 00:35:40 Therefore, I don't need to write the database name, I can just write the table name like

  • 00:35:47 this.

  • 00:35:48 But with this connection you can also access different databases with providing full path.

  • 00:35:56 Okay so inserting the tbs students then I will write the each column name that I want

  • 00:36:04 to insert to easily do it.

  • 00:36:06 You can right click tbs students script, create and I think like this insert to new query.

  • 00:36:16 Okay it has generated me the column names and like actually some default way so I can

  • 00:36:30 use this for my leverage.

  • 00:36:41 Okay for this to be valid I need to add add character.

  • 00:36:47 Okay now my string supports multi-line and with dollar value it supports inline object

  • 00:36:56 placement.

  • 00:36:58 Okay, I will insert student id, student name, student email email nickname and then we will

  • 00:37:08 insert values with the first value.

  • 00:37:13 So the first value will be student id.

  • 00:37:16 So let's generate a random id and let's put next and the second value will be okay.

  • 00:37:36 Inside values we have we have a parenthesis opening and close.

  • 00:37:40 The first value is integer.

  • 00:37:42 Therefore, I am just writing it like this.

  • 00:37:44 Then the second value is student name.

  • 00:37:49 I think we had student names that we have randomly generated and let's find a random

  • 00:38:00 string generating.

  • 00:38:05 Okay, I will find a ready function.

  • 00:38:15 Okay, something like this.

  • 00:38:19 Okay, okay it requires me to define a length and any length it will generate.

  • 00:38:29 So okay for inserting a string, I write a string beginning and string ending.

  • 00:38:38 This is the format that the sql takes and in front of it.

  • 00:38:44 I put n to support turkish characters as well.

  • 00:38:51 So let's we can add turkish characters, something or any other language specific character that

  • 00:38:58 you want.

  • 00:39:02 Okay and here I will call this random string method with length and I will define the length

  • 00:39:15 with my rent next let's say minimum three characters and maximum 15 characters.

  • 00:39:23 Okay and then what else I will provide as values, student email and same.

  • 00:39:36 Let's do it something like this.

  • 00:39:39 Okay and let's add gmail to end of it.

  • 00:39:51 Okay okay you see the add character right now.

  • 00:39:59 Breaking my code.

  • 00:40:04 Okay, I have somewhere a code breaking thing and okay and here interesting.

  • 00:40:47 What is breaking my code?

  • 00:40:52 Yeah here here I need another parentheses and parentheses.

  • 00:40:58 Okay, so add character is not breaking and then what else.

  • 00:41:03 Value is male.

  • 00:41:04 This is bullying.

  • 00:41:06 Therefore I will use my rent again so it will be zero or one therefore like this and a nickname.

  • 00:41:17 Okay let's define a static nickname, static length.

  • 00:41:24 So maybe something like this.

  • 00:41:29 Okay, and you can tell the sql server this is the end of the query with semicolon.

  • 00:41:39 It is not necessary, but it's a good practice.

  • 00:41:44 So let's say the length will be equal to eight character.

  • 00:41:50 Okay, maximum eight, minimum eight.

  • 00:41:55 All right.

  • 00:41:56 So this is our query.

  • 00:41:57 Then what we are going to do is we will execute this query same as delete query.

  • 00:42:03 Okay, so I will copy and paste the code for now like this and then the base query here

  • 00:42:23 and number of rows.

  • 00:42:26 Let's edit here like this and plus equal.

  • 00:42:38 Okay, then this is interesting.

  • 00:42:48 This is by default zero.

  • 00:42:56 Okay, okay then we can show.

  • 00:43:04 Okay, by the way, this will be rows inserted.

  • 00:43:14 So when you change a variable name, the microsoft visual studio will tell you rename this to

  • 00:43:23 this and it will be renamed everywhere where it is referenced.

  • 00:43:26 Okay and okay.

  • 00:43:27 Student records added to the database and let's try.

  • 00:43:42 Let's run the application.

  • 00:43:46 This is execute non-query and let's select top 5k rows.

  • 00:43:53 Okay, there is nothing here and it's empty.

  • 00:43:58 We can also use sql server provider to see our queries with new trace connect.

  • 00:44:06 You can pick standard tsql, whatever.

  • 00:44:10 Let's pick the standard.

  • 00:44:13 Okay and when I click okay once to the records edit so our query was correct and here I can

  • 00:44:21 see the query.

  • 00:44:22 So this is the executed query as you can see with values it is including and when I refresh.

  • 00:44:31 Okay, I can see our randomly generated data when I click again.

  • 00:44:40 Okay, it is also added.

  • 00:44:41 I can also see the executed scripts so it also shows which application has executed

  • 00:44:52 the query.

  • 00:44:53 I think I can filter this with set as to sql server management but not from here probably.

  • 00:45:08 So this is the query I have executed here.

  • 00:45:11 Even this is shown here.

  • 00:45:14 So whatever the query executed is displayed here and you can see also some system queries

  • 00:45:24 as well.

  • 00:45:25 Okay so this is the query that that we execute with core .NET sql client so let's open a

  • 00:45:35 new trace and I want to see only my project uh query.

  • 00:45:46 So here column filters and here application name.

  • 00:45:52 As you can see there is not like.

  • 00:45:55 So sql server profile queries are are excluded and here I think I can add not like okay,

  • 00:46:09 here I click it double.

  • 00:46:11 Oh, I will add like and I will add .NET core I don't know if it is case sensitive or not

  • 00:46:20 but we will see okay like .NET core when I run.

  • 00:46:24 Okay, there is nothing.

  • 00:46:26 Let's run it okay so it is probably case sensitive.

  • 00:46:31 Therefore it is not displaying.

  • 00:46:33 So let's open new trace standard and here oh core .NET okay, I see core .NET so let's

  • 00:46:50 stop it.

  • 00:46:53 New event selection column filters core .NET let's see if it is case sensitive or not run

  • 00:47:08 and insert.

  • 00:47:10 Okay, still not working.

  • 00:47:16 So okay.

  • 00:47:19 Can I copy and paste this?

  • 00:47:28 okay, yeah, I am able to copy and paste it here.

  • 00:47:35 I will copy paste exactly as it is close.

  • 00:47:37 This: okay, okay.

  • 00:47:38 Run.

  • 00:47:39 Interesting, it is not working.

  • 00:47:51 Why?

  • 00:47:57 Anyway, you can, uh, work on this to fix it.

  • 00:48:25 I will not, uh, spend time with it and then uh, now, uh.

  • 00:48:33 We can put this inside a record.

  • 00:48:36 Okay, such as this inside the method.

  • 00:48:41 Actually okay, multiple.

  • 00:48:44 Okay, let's just edit here.

  • 00:48:48 Don't need it.

  • 00:48:51 So let's add it here here and let's run it like 500 times.

  • 00:49:11 So this will generate 500 random records and let's click here.

  • 00:49:18 Tuning: okay, when we do tuning, it will automatically filter more of the system queries I think.

  • 00:49:29 And when we run the application.

  • 00:49:43 Okay, okay, now we can see our insert queries.

  • 00:49:47 Okay, it's already done.

  • 00:49:50 500 student records added to the database.

  • 00:49:54 Okay, when we select okay, we can see them.

  • 00:50:00 You see random ids, student name, student email.

  • 00:50:05 However, normally we would expect it to have all different student ids, but we are not

  • 00:50:15 doing that check right now.

  • 00:50:17 We are just learning how to do sql command.

  • 00:50:21 So this was insert command you can also write.

  • 00:50:24 I would suggest you to write the command here first and test it if it is working such as

  • 00:50:29 insert into tbs students and the column names student id, student name okay, student email

  • 00:50:43 and his mail and nickname and then values.

  • 00:50:50 You can also encapsulate integer with a string and then test and test tests at gmail.com

  • 00:51:03 and then his mail one nickname that's great and run it and if it is working as you can

  • 00:51:13 see it is working then apply it to your program.

  • 00:51:18 So this way you will be less likely to make a mistake.

  • 00:51:24 So we have seen how to make insert query as well.

  • 00:51:34 Okay let's note what we have seen so far and let's note them.

  • 00:51:49 Okay so what we have seen we have learned how to build a query string of .NET sql connection.

  • 00:52:15 So let's solve our name and how to write updates in search query with sql connection class

  • 00:52:44 in .NET and how to execute queries in sql server with using sql server profiler trace

  • 00:53:32 feature of okay okay, okay so in using statement, let me show you an example.

  • 00:54:33 For example, I have defined a variable like this: can I use it here?

  • 00:54:44 yes here I can use it.

  • 00:54:46 However, can I use it here equal to here?

  • 00:54:52 I cannot use it because this object will get disposed when you are out of scope of the

  • 00:55:03 using statement and when using statement ends, it will automatically close the connection

  • 00:55:09 of sql server and dispose it off.

  • 00:55:12 So this is about being using statement.

  • 00:55:16 It is important.

  • 00:55:17 Whatever you define in using statement inside the scope of using statement will be

  • 00:55:31 immediately disposed of when using statement ends.

  • 00:55:40 Okay, this is important and let's say how to calculate or let's say okay permit connection

  • 00:56:04 usage and sql server pooling with sql server.

  • 00:56:44 Okay, stopwatch.

  • 00:56:57 Okay and let's see.

  • 00:57:02 Okay, so we have seen delete command and I will modify this delete command for deleting

  • 00:57:14 a certain student and then I will also add update student and select student as well.

  • 00:57:25 So before we do delete, let's add a select command to display students.

  • 00:57:38 Okay, here I have added a list box.

  • 00:57:44 I will display them inside the list box.

  • 00:57:50 You can also use grid but let's see the grid later and here I will add a button to select

  • 00:58:09 the select and list the students okay, list the students okay and name okay so what we

  • 00:58:31 are going to do is we will select top 100 students from database and add them to our

  • 00:58:42 list box.

  • 00:58:45 And to do that I will use the using command but it will be a little bit different this

  • 00:58:55 time.

  • 00:58:56 So our base query will be select top 100 and all so start from tbl students.

  • 00:59:12 First, let's check our query whether it is working or not in sql server management studio.

  • 00:59:21 And yes, it is working.

  • 00:59:25 By the way, when we select top use, how does it sort it currently it is not sorted with

  • 00:59:30 anything so whenever we run the query it may change.

  • 00:59:34 However, do we have a primary key?

  • 00:59:37 no, we don't have a primary key.

  • 00:59:40 Therefore, the results may change whenever you do the query, but it is bringing the same

  • 00:59:48 results at the moment and then what we are going to do is this time we will execute command

  • 00:59:58 with select and it has a kind of different syntax here.

  • 01:00:08 Okay, and here instead of sql command, we will use sql data adapter okay, and it will

  • 01:00:22 take also a query, a connection and it will fill a data set.

  • 01:00:31 So what is a data set?

  • 01:00:33 Data set means a single table with multiple or single rows.

  • 01:00:43 Okay, so this is the data set type and dset.

  • 01:00:52 Let's name it as dset so it will fill the data set.

  • 01:01:07 However, we have to initialize it to be used so it will be an empty data set and then we

  • 01:01:17 will return the data set.

  • 01:01:20 Okay once it is filled with.

  • 01:01:24 So by the way, we don't have a return right now because we are not writing our method

  • 01:01:30 for database queries.

  • 01:01:34 Okay, so what is this going to do is it will execute this query and here the data set with

  • 01:01:46 the returned results.

  • 01:01:47 Currently data set support only one table you see.

  • 01:01:50 Currently there is only one table result.

  • 01:01:52 However, if you execute two queries like this, you see there will be two tables.

  • 01:01:59 And to support two tables you have to use data table.

  • 01:02:10 Data table or data set.

  • 01:02:13 Support multiple tables by the way.

  • 01:02:17 So I think I will write some.

  • 01:02:22 I will write two queries and the second one will select only top 10.

  • 01:02:28 Okay, so let's execute this query and see how it is working.

  • 01:02:33 Data set data table.

  • 01:02:37 Yes, data set supports multiple tables.

  • 01:02:40 Therefore, therefore it is like a database in our table.

  • 01:02:45 Same as database.

  • 01:02:49 Okay, okay one moment.

  • 01:02:55 Okay, so let's see our code is working or not.

  • 01:03:02 Then we fill it.

  • 01:03:04 Okay, when we hover our mouse cursor over the dset object and click the magnifier icon,

  • 01:03:15 we can see what's inside it.

  • 01:03:16 So you see there is table at the top and there is table and table one.

  • 01:03:22 So this is table zero.

  • 01:03:23 This is table one and you see table one.

  • 01:03:26 I table zero has 100 records like this and the table one has only 10 records.

  • 01:03:34 So this is what is about data set.

  • 01:03:40 Now since I have obtained my data set, I can add that to my listbox.

  • 01:03:49 So I will do a for loop inside data set tables and which table.

  • 01:03:57 I can iterate tables as well.

  • 01:04:00 So let's iterate tables and inside each table.

  • 01:04:04 Such as this, I will do a for each dset tables.

  • 01:04:09 Okay, and here it will be.

  • 01:04:12 Each one will be a table.

  • 01:04:15 So let's uh um.

  • 01:04:19 Okay.

  • 01:04:20 Like this table.

  • 01:04:23 Uh, let's say table this or my table and inside each table there will be different columns

  • 01:04:37 and records.

  • 01:04:38 So these table rows.

  • 01:04:42 Uh, counts.

  • 01:04:44 Okay, so where is the rows?

  • 01:04:53 Interesting data table collection.

  • 01:05:00 Data set tables: okay, I think it will be data table not table here.

  • 01:05:16 And now rows should appear okay here.

  • 01:05:20 And rows count.

  • 01:05:24 So for each count and before beginning.

  • 01:05:29 Um, oh anyway, let's just ignore it.

  • 01:05:33 And did we give a name to our listbox?

  • 01:05:39 Okay, let's give it a name so we can access from code behind.

  • 01:05:46 Okay one.

  • 01:05:50 Okay items at and we can add items like this.

  • 01:05:57 So uh, my table.

  • 01:06:00 Uh, rows.

  • 01:06:03 Let's say student id equal to my table rows I and here I can either access it with index

  • 01:06:19 or uh column name and here student id it is the column name and then I have to convert

  • 01:06:30 it to two string okay and then I will access the student name as okay.

  • 01:06:41 Okay, this will be the first table values by the way.

  • 01:06:47 Zero.

  • 01:06:48 Uh.

  • 01:06:49 First table is index zero and the name will be my table rows and one index one.

  • 01:07:04 Okay, I will do different ways of doing this and here I will continue this time.

  • 01:07:13 I will access them like this.

  • 01:07:19 These tables and count.

  • 01:07:24 Okay and there will be two four.

  • 01:07:27 So let's name it as mm.

  • 01:07:28 Okay, and these set tables.

  • 01:07:31 Um okay.

  • 01:07:33 Mm rows count.

  • 01:07:36 By the way, it will be I not mm here and inside these rows here we will access them like this.

  • 01:07:53 So this time let's write only the email as uh, what's the name student email yeah, this

  • 01:08:04 time we have to access it as like this and student email okay, okay how what else we

  • 01:08:20 can do and okay so let's write another for each and here another for each.

  • 01:08:31 This time I will pour each the rows inside uh data table drw and let's add um nickname

  • 01:08:51 okay equal to a nickname.

  • 01:08:59 Okay, you see uh I have uh right I have shown you the different ways to access the results

  • 01:09:10 obtained from uh data table.

  • 01:09:13 We can also do uh something like this inside uh they said tables and the second table row

  • 01:09:27 something like this and let's write this time is male.

  • 01:09:33 Okay, I think like this.

  • 01:09:38 Yeah yeah you see I can access the table with index.

  • 01:09:43 I can iterate them like this.

  • 01:09:46 I can for them for loop them like this.

  • 01:09:49 All of the methods are displayed each time.

  • 01:09:54 We have to do a two string because it is a database uh object.

  • 01:09:59 Therefore if you don't do two two string you won't get the value of the object and that's

  • 01:10:07 right.

  • 01:10:09 Okay so you see student id student name, these are the first 110, then uh their email 110.

  • 01:10:16 Okay you see we see the all same email.

  • 01:10:20 So we have a problem with emails and which is the error is here because the counter for

  • 01:10:30 uh email loop is mm, not I so let's rerun the application.

  • 01:10:37 Okay you see we can see the email, we can see the nickname and we can see the ismail.

  • 01:10:56 They are all false.

  • 01:10:58 This is weird.

  • 01:11:02 So yeah, looks like our uh I see so when we insert the data as with minimum zero and then

  • 01:11:20 the maximum one, one is exclusive.

  • 01:11:23 Uh.

  • 01:11:24 Therefore, we have to set it as two.

  • 01:11:26 Okay, so let's delete all the records in the database.

  • 01:11:36 Okay, okay you see all deleted and let's insert some we have inserted.

  • 01:11:47 Now we can see okay, true false true false.

  • 01:11:51 Okay, okay we have uh now, uh seen all.

  • 01:11:56 Uh.

  • 01:11:57 Update delete oh, we haven't seen the update yet so let's add an update feature as well.

  • 01:12:05 So I will make the update kind of uh dynamic.

  • 01:12:09 By the way, like um let's add two text box okay, so the first text box will be the predicate

  • 01:12:23 okay, the predicate to uh either list the results or um update or delete the record.

  • 01:12:38 So let's name this as predicate okay and name txt predicate.

  • 01:12:50 Then I will add another text box for a new value.

  • 01:12:59 Okay, so this will be txt new value okay and uh here let's write inside new value and let's

  • 01:13:17 add an update button okay and name btn update okay and I will add list all button with predicate.

  • 01:13:39 Okay first do the update, then we can move to the next okay however for update um, I

  • 01:13:58 think we also need to set the uh column name uh because otherwise we cannot update new

  • 01:14:07 value without column.

  • 01:14:11 So let's uh get columns names as well inside the drop box combo box here.

  • 01:14:25 Okay so um this screen is getting crowded therefore uh I will add um as top control

  • 01:14:43 this okay okay and let's move items inside to the top control.

  • 01:14:53 Okay this is the first grid and inside the first grid.

  • 01:15:08 I am putting them like this and yeah and here's another grid we have seen grid uh previous

  • 01:15:24 semester so you know them already.

  • 01:15:28 Okay and this is the second grid we have now it is easier to see them.

  • 01:15:35 Okay, new value update and let's add uh this box another list box here.

  • 01:15:52 Okay okay this is good and let's add a button to list the list box.

  • 01:16:05 Let's add here okay name as list all records, list records.

  • 01:16:17 Okay, whatever we can name or define it as we want.

  • 01:16:25 Okay so uh.

  • 01:16:26 First we need to uh populate this combo box when the application starts.

  • 01:16:32 So okay cmb column names.

  • 01:16:40 Okay so let's uh populate the combo box when uh the main window starts.

  • 01:16:47 What we are going to do is uh we will select top one all from tbl students like this and

  • 01:17:00 to do that you see I have to do a copy paste code and for now I will do it uh to just to

  • 01:17:09 show you okay and let's do it.

  • 01:17:26 Okay okay then what we are going to do is for each I think we can do for I am not sure

  • 01:17:35 if we can do for each or not so I will just test and tables and columns no tables yes

  • 01:17:47 first tables and this will be data table dt and inside dt dot columns.

  • 01:17:57 Yeah here and we are column and then I will populate my combo box items at your column

  • 01:18:10 to string.

  • 01:18:12 Okay so let's see if it is working or not when I run the application.

  • 01:18:17 Since it is inside my constructor inside main window it will get populated with the column

  • 01:18:25 names of my students table.

  • 01:18:30 I click okay they are here.

  • 01:18:32 You see my column names great now I can write my list first.

  • 01:18:39 Let's write our list.

  • 01:18:44 Let's say list button by the way um to I think I have to remove this and add a label so it

  • 01:19:02 will be oh no I can keep that as well.

  • 01:19:07 It is.

  • 01:19:08 It is not a problem.

  • 01:19:09 Okay so what is going to do is uh this will list the results.

  • 01:19:17 However you see I keep writing um same uh code again and again and this is a big no

  • 01:19:28 no.

  • 01:19:29 Okay so what else I can do?

  • 01:19:34 What would be the good solution to solve my copy paste of code?

  • 01:19:42 I can do uh add a class a static class more for my database operations and I will name

  • 01:19:52 it as cs db connection.

  • 01:19:58 Okay and it is added.

  • 01:20:01 I will name it as public static class, cs db connection and I will write some methods

  • 01:20:11 public static data set and in order to return data set I have to add here using uh system

  • 01:20:22 data and let's add select data set method and this will take a string as our query and

  • 01:20:36 this will use my connection string.

  • 01:20:41 So i'm going to move this here as public static string and now it will be broken here and

  • 01:20:51 to be able to use it here.

  • 01:20:53 I will edit here as a reference using static and lecture.

  • 01:21:02 Three cs db connection: once you add a class as static like this, all the uh defined properties

  • 01:21:15 fields here will be directly available.

  • 01:21:18 Therefore you see I don't get any error.

  • 01:21:21 Why?

  • 01:21:22 Because wherever it is referenced to sr connection string it is coming from field static string

  • 01:21:28 of cs db connection sr connection string as you can see and uh what this is going to do

  • 01:21:38 is uh, it will simply do this okay and return reset.

  • 01:21:50 Okay now we still have some uh reference issues uh libraries.

  • 01:21:55 I am adding sql data sql client and here it will be like this.

  • 01:22:01 Now I have written a simple method to do uh select statements okay and it will return

  • 01:22:12 a data set.

  • 01:22:13 However if I want to oh I don't know, I don't need that.

  • 01:22:17 Uh something came to my mind but it is not important.

  • 01:22:21 So here uh I will return a data set or I can also say var or let's say dt records equal

  • 01:22:32 to from db connection.

  • 01:22:36 I can uh reference it or I can directly access the method because I have added cs db connection

  • 01:22:46 as a reference to here.

  • 01:22:49 Okay, using and the query will be select all from tbl students.

  • 01:22:59 Okay, this will be a base query and what I am going to do is I will do a for each inside

  • 01:23:12 the first table.

  • 01:23:15 However, if my query fails, my application would crash but I am not considering that.

  • 01:23:24 Uh, uncut errors right now.

  • 01:23:32 Okay here I have a for each in and then um let's make an append like this.

  • 01:23:47 So here I will do uh for each the column name and I will compose a string sr.

  • 01:24:00 Let's say record base equal to um oh this is not the way I want but I can do that.

  • 01:24:23 It is fine.

  • 01:24:24 It is fine.

  • 01:24:25 So it will be plus equal to column name.

  • 01:24:29 By the way, this is object yes therefore I have to make a string plus and let's make

  • 01:24:38 it like this.

  • 01:24:41 And here uh to string to string and I will add a top character to the end.

  • 01:24:55 Okay, then I will add it to my list box which is named as name two.

  • 01:25:07 This box two okay items at sr record base.

  • 01:25:14 Okay, okay let's test if it is working or not.

  • 01:25:28 By the way, a string builder would be more appropriate to use because when you do a string

  • 01:25:35 concatenation uh, it is a big operation for uh computer to do.

  • 01:25:45 Each time it allocates a new string object.

  • 01:25:48 However, this is not that big.

  • 01:25:50 So we have used a string.

  • 01:25:52 But let's use a string building builder that is a better so we learn the better option.

  • 01:25:59 A string builder is coming from system class text sr build new uh string builder and here

  • 01:26:10 we will use uh, append and it will append like this and then um yeah it should work

  • 01:26:26 each time because we will initialize a new string to string like this.

  • 01:26:33 Okay, let's test it.

  • 01:26:36 Initialize new string builder by the way.

  • 01:26:47 Okay, let's go to second tab list records: okay so uh you see we have added all records

  • 01:26:56 to our list box.

  • 01:26:58 Okay, it is working as expected.

  • 01:27:02 Now what I am going to do is I will dynamically use these: uh predicates.

  • 01:27:10 Uh so so this is our base query called like this.

  • 01:27:24 However, if combo box uh selected okay, selected index is uh bigger than zero.

  • 01:27:40 I will modify my query as okay, uh equal to uh select okay, selected item to string.

  • 01:28:05 Okay, so it will select a certain column.

  • 01:28:09 So let's uh, test it.

  • 01:28:12 You see when you are writing a program you should go step by step, not write everything

  • 01:28:17 at once and try whether it is working or not.

  • 01:28:21 Do step by step.

  • 01:28:22 So uh I want to see only student name and when I click record okay it is not working

  • 01:28:30 at the moment.

  • 01:28:31 Oh, I see the reason because we didn't provide the base query into our record and since we

  • 01:28:39 are using a dynamic approach here, I don't need to change anything because I didn't write

  • 01:28:46 anything.

  • 01:28:48 Uh, how do we call it um constantly I have used dynamic iterations so I will record student

  • 01:29:02 name list okay, you see I only see the student name so this is the selecting single column

  • 01:29:10 and I can also write a verb predicate how it is like this.

  • 01:29:19 So here we have predicate inside tx predicate.

  • 01:29:23 So if if tx text not equal to predicate, what am I going to do is sr base query is equal

  • 01:29:40 to I will put a uh empty character here.

  • 01:29:45 By the way, if you have multiple empty characters it is not important.

  • 01:29:49 They are ignored uh text.

  • 01:29:54 So this will be a predicate.

  • 01:29:56 Okay, so let me show you how we are going to use our predicate.

  • 01:30:07 Okay, so let's list them all.

  • 01:30:12 Okay, I want to list uh, student id bigger than or smaller than 100k.

  • 01:30:21 So where okay, student id smaller than?

  • 01:30:27 By the way, the column names are not case sensitive and let's write like this.

  • 01:30:36 Okay, we have a problem somewhere.

  • 01:30:40 Oh oh, we have a problem.

  • 01:30:44 The problem is uh, the student id is not.

  • 01:30:52 I mean the column names are not case sensitive.

  • 01:30:55 However, when uh, the sql lowers, uh the big I into the smaller I it has converted it into

  • 01:31:05 I think this, let's try.

  • 01:31:09 Yeah, uh, maybe I don't know because of my system so I have to write it like this.

  • 01:31:16 Okay, you see, I can also write it like this.

  • 01:31:21 Uh, student id?

  • 01:31:23 It doesn't matter.

  • 01:31:25 Yeah, uh.

  • 01:31:26 However, there was a uh big I conversion problem.

  • 01:31:31 So let's rerun it.

  • 01:31:34 You see when an error happens, it causes a crash.

  • 01:31:37 Therefore, I have to add try and catch inside my um connection like here and if an error

  • 01:31:53 happens, I can log it here like this, log error or exception a so the application will

  • 01:32:06 not crash.

  • 01:32:08 Okay, however, it will crash at the other side because we didn't check that where either

  • 01:32:16 there is a table or not.

  • 01:32:18 So we will see that as well.

  • 01:32:21 So I write something like this.

  • 01:32:22 This is not valid and interesting.

  • 01:32:28 Okay, this is a pendant.

  • 01:32:44 Okay so now we are having a problem here because we didn't check whether this table has index

  • 01:32:51 zero or not and I wonder if we can check it like this.

  • 01:32:58 Yeah, let's try it.

  • 01:33:07 Okay, and where student id equal to like this?

  • 01:33:21 okay, so this is not working.

  • 01:33:25 Uh, I think it was checking the rows so yeah there is no easier way.

  • 01:33:32 Uh, therefore I have to add a check to here if count bigger than zero.

  • 01:33:48 I think this wouldn't work as well because it will try to access index and it will throw

  • 01:33:55 an error.

  • 01:33:56 Yeah, now it should work.

  • 01:34:04 Yeah and now I will fix the query and I will write it as smaller than this.

  • 01:34:23 Nothing like this, nothing and another nothing at another.

  • 01:34:30 Okay, now I see you see I have listed only one okay and now time to write update query

  • 01:34:44 for update queries, we will generate another method inside our cs db connection class because

  • 01:34:53 here here we keep copy pasting our code which is a big no and no.

  • 01:35:01 Therefore I will take our proper query here and public static int why?

  • 01:35:14 Int because we will return the number of affected rows okay and let's say update delete, insert

  • 01:35:29 okay and we have command and we will return this result of this.

  • 01:35:42 However this may also cause an error therefore we have to take it inside, try and catch and

  • 01:35:54 here log exception and now not all the paths are returning and therefore we will return

  • 01:36:05 zero.

  • 01:36:06 Okay and now I can call it and so I will write update click here.

  • 01:36:16 So let's first write our update here.

  • 01:36:20 Update tbs students set which column x column for example student id like this where I also

  • 01:36:28 need a verb predicate or it would update entire table so for example equal to like this okay

  • 01:36:38 so this is my base query base query like this.

  • 01:36:50 Okay now I need at least one parameter which is the updated column name.

  • 01:37:02 Okay therefore if combo box column name selected index is smaller than zero, I will return.

  • 01:37:13 Else here I will use it to string and then I will append a text to it.

  • 01:37:29 Okay here I will add txt, new value text and the rest will be like this.

  • 01:37:38 I think we can append it here and let's name as like this and let's box show db connection

  • 01:37:54 or let's just call our method updated at insert as our base query and let's say affected number

  • 01:38:10 of rows.

  • 01:38:13 Okay, now it should work.

  • 01:38:21 By the way, our text boxes are pretty small right now so I will expand them, make them

  • 01:38:35 bigger.

  • 01:38:36 Okay, so let's organize them a little bit like this here and here.

  • 01:39:00 Okay, this wasn't necessary but I just wanted to use it with more efficiency.

  • 01:39:11 Okay, so let's list the records.

  • 01:39:15 Okay, we can see the listed records here so let's update this record and if it has a unique

  • 01:39:25 name it will be only updated so the new value will be full count.

  • 01:39:34 However, I didn't add an important thing here which is here I have to encapsulate it in

  • 01:39:50 as a string like this.

  • 01:39:54 And additionally, if the new text has uh, single, uh, what was the english of that?

  • 01:40:07 I can't remember and like this because if you're uh, adding this literal into your string

  • 01:40:18 in sql query, you have to replace it with uh, double single quota?

  • 01:40:25 Okay, single quota.

  • 01:40:27 Okay, now it should work as expected.

  • 01:40:33 Okay, let's uh.

  • 01:40:37 This record.

  • 01:40:39 So I will update this student name so the new value will be full count and I will write

  • 01:40:46 where predicate a student name equal to za3vf.

  • 01:40:54 So let's click, update and re-list okay, it didn't return anything to the string.

  • 01:41:08 Okay, let's add a.

  • 01:41:19 Oh, I know the reason because we didn't select which column to be updated.

  • 01:41:28 So the column will be updated is student name and okay, one row affected when we re-list

  • 01:41:36 so this is the student id.

  • 01:41:38 Let's note that as well.

  • 01:41:47 Which is okay.

  • 01:41:55 Re-list uh oh, we didn't clear the all the records.

  • 01:42:01 So let's change this a bit.

  • 01:42:06 Okay, items clear and re-list the records.

  • 01:42:24 Okay, so do we see hurkan here?

  • 01:42:30 yes, the student name hurkan is updated.

  • 01:42:33 If I don't write any predicate, it will update all columns hurkan.

  • 01:42:39 So let's see 500 rows affected.

  • 01:42:42 This records all student names hurkan I can.

  • 01:42:46 Oh, there is no.

  • 01:42:49 Uh, non-index by the way.

  • 01:42:59 So you see all student names are now hurkan and um, we are not able to select index zero

  • 01:43:15 by the way.

  • 01:43:17 Also, let's restart the application.

  • 01:43:26 Let's fix that as well so we can return back to all records.

  • 01:43:30 And how are we gonna do that is items at all and this, uh, index zero will be as something

  • 01:43:45 like this.

  • 01:43:51 Yeah, oh, by the way, this was already erroneous.

  • 01:44:04 Okay, uh.

  • 01:44:06 Selected index bigger than zero and selected index is lesser than one?

  • 01:44:13 Yeah, and by default, I will assign the selected index as equal to zero.

  • 01:44:23 Okay, now it should work.

  • 01:44:27 Okay, let's list all records.

  • 01:44:39 Let's list only student email and oh so let's add a predicate as where is mail equal to

  • 01:44:52 one list records?

  • 01:44:55 Okay, so one is not working so I will set as true.

  • 01:45:00 Okay, oh, I know because this has to be big.

  • 01:45:05 Okay, now it is working only true mail if I do it like this.

  • 01:45:12 Okay, still working.

  • 01:45:13 So both way working.

  • 01:45:17 So this is how we do.

  • 01:45:19 Uh, select update, delete, insert uh with um, uh.

  • 01:45:26 Programmatically with c sharp in microsoft sql database, I have composed a connection

  • 01:45:35 class and I have now.

  • 01:45:37 I don't have to each time write this.

  • 01:45:41 Okay, I will also fix this and uh, let's fix it as dset is equal to uh, select data sets

  • 01:45:57 and sr base query.

  • 01:45:59 Now it should work.

  • 01:46:03 Also, you see how much cleaner our code becomes.

  • 01:46:09 Uh, I won't.

  • 01:46:11 We could also, uh, remove all the duplicate codes we have written here at the previously,

  • 01:46:21 but I won't touch them so you can see them as well.

  • 01:46:24 But at home you can remove these duplicate queries and use our methods we have generated.

  • 01:46:34 Okay, it is working.

  • 01:46:38 Okay, uh, let's note what we have seen uh so far.

  • 01:46:47 Okay, so we have seen an update, delete, insert, and okay and select.

  • 01:46:57 Okay, uh, how to compose a um database class to handle database operations efficiently

  • 01:47:19 and properly.

  • 01:47:20 Okay, and uh, what else?

  • 01:47:30 Okay, how to uh, dedicate to filter update?

  • 01:47:50 delete select uh queries.

  • 01:48:02 Okay, okay, uh, what if if we want to select single row currently is our method is efficient

  • 01:48:20 for that, we know that our method will return on a single row and currently we are using

  • 01:48:27 um these methods.

  • 01:48:32 But or we want to pick only single uh data table, not multiple data tables in our query.

  • 01:48:41 Then we can expand our methods.

  • 01:48:46 For example, this is a method that I have written myself.

  • 01:48:48 Okay, so uh, what does this uh set commit means set commit means that isolation level

  • 01:49:07 read uncommitted.

  • 01:49:08 This is something that you should not, uh, bother at the moment, care about at the moment.

  • 01:49:15 And I will also comment that so what is the different thing that is made here you see

  • 01:49:25 we are returning a data table, not a data set.

  • 01:49:29 The difference is uh, we are using fill with data table instead of a data set.

  • 01:49:41 That's just that.

  • 01:49:43 Nothing else.

  • 01:49:44 So it automatically handles and fills with data table instead of a data set and what

  • 01:49:54 else there is we can do.

  • 01:49:55 We can also return a data row and if we know that our method will return only single results

  • 01:50:02 single row result, we can use data row which is an efficient way of returning here.

  • 01:50:12 What we are doing is alternatively is we are returning the first row if it does exist.

  • 01:50:20 Okay, it is actually not much different because we are still have to.

  • 01:50:25 We still have to use sql adapter and data table for filling.

  • 01:50:33 However, it is still better than other methods.

  • 01:50:39 Okay, and the most important thing is when we write our queries like here, our program

  • 01:50:52 is vulnerable to hacking.

  • 01:50:56 Okay with sql injection.

  • 01:51:02 So I don't know if I should show you sql injection this week or not.

  • 01:51:11 But I want you to search for what is sql injection and I will show you how to prevent it next

  • 01:51:21 week.

  • 01:51:22 So I want you to look for what is sql injection and how to prevent it.

  • 01:51:28 Okay, I want you to search this topic, get an idea about it, and hopefully next lecture.

  • 01:51:37 I will show you how to prevent sql injection as well.

  • 01:51:45 And so let's add some other options such as order by because currently our let's say results

  • 01:52:01 are not ordered by anything.

  • 01:52:03 It is ordered by the sql server itself.

  • 01:52:11 Okay, okay so to order by I will add another combo box.

  • 01:52:18 So let's name this combo box as column and I will add another text box to define my order

  • 01:52:34 by.

  • 01:52:39 And let's give it a name as name, txt order I because we can order by multiple things.

  • 01:52:47 If there are same values, I will show you.

  • 01:52:52 So let's say or order, predicate or order comment.

  • 01:53:02 Let's say okay what i'm going to do is I will add more records to my database with more

  • 01:53:10 same ids and names.

  • 01:53:16 To do that, I need to modify my methods here.

  • 01:53:24 Okay, so I will reduce this to five character and I will reduce this to between one and

  • 01:53:37 let's say 10k.

  • 01:53:39 Okay, and let's add 50k records.

  • 01:53:44 Okay, and let's run the application.

  • 01:53:52 Oh, and then I will also add order by to my method here.

  • 01:54:01 My list method: if txt order is not equal to order command, I will append it to my query.

  • 01:54:18 That's it.

  • 01:54:20 Okay, because order by comes at the end of the query.

  • 01:54:27 We can also log our executed queries as file.

  • 01:54:33 Okay, to use file I have to append edit to my references.

  • 01:54:39 Or alternatively, I can write the full path as system io file and append all right all

  • 01:54:51 text.

  • 01:54:55 So what i'm going to do is I will date time now plus newline and my base query plus environment.

  • 01:55:06 Newline.

  • 01:55:07 Okay, these are both equal newline and environment newline and the file name will be sqlqueries.txt

  • 01:55:22 okay, so now let's delete all records and repopulate our database.

  • 01:55:45 Okay and I will delete all students okay, all deleted and now I will add 50k records.

  • 01:55:53 It will take some time.

  • 01:55:56 Obviously, you see both sql server and profiler is working.

  • 01:56:02 By the way, we can close profilers because you see it is showing everything so it is

  • 01:56:12 taking extra cpu okay and I will make another edition.

  • 01:56:21 You see, I am not able to see how many records added and my interface is frozen.

  • 01:56:28 I think I will show that next week.

  • 01:56:31 So let me add a comment here.

  • 01:56:37 Okay, next week show how to prevent ui freezing when adding new line, number of edit records

  • 01:56:58 show how to prevent sql injection.

  • 01:57:03 I am taking notes so let's see how many records we have added so far to get that numbers quickly,

  • 01:57:14 select count and start from tbr students: okay, we have added 34k records and therefore

  • 01:57:28 I also need to list my result with I think select top x.

  • 01:57:39 So let's make it as top 500 because otherwise it would be too much data to select and add

  • 01:57:48 to this box which would make our system slower.

  • 01:57:58 Okay, and let's list records.

  • 01:58:05 Okay, so you see now the names are four characters or three characters and student id is between

  • 01:58:13 one and 10k.

  • 01:58:16 So let's order the list with student id, order by student id and ascending.

  • 01:58:28 Okay, so you see now they are ordered with ascending order.

  • 01:58:35 However, there are multiple records with same student id and therefore they are not sorted

  • 01:58:44 as I want.

  • 01:58:45 Then I can add another ordering parameter to the end as student name descending.

  • 01:58:55 Okay, so now let's check the record on seconds 18.

  • 01:59:01 Okay, you see now we have a problem.

  • 01:59:07 Okay, because I have written this column name incorrectly.

  • 01:59:14 Okay, now let's see.

  • 01:59:17 Okay, we come to the first ordering by 18 and then you see the name is ordered by descending

  • 01:59:26 so you can define multiple orderings.

  • 01:59:29 It will order, sort with first value, then by the second value.

  • 01:59:35 Okay then I can even sort by third value if there are same student name.

  • 01:59:40 So let's make first order by student name.

  • 01:59:44 Okay so do we have same student name?

  • 01:59:48 yes here we have same student name.

  • 01:59:52 Then I can order by uh, email, student email ascending or by default maybe ascending scanning.

  • 02:00:04 Okay, it says this email.

  • 02:00:07 Yeah it has done.

  • 02:00:10 You see it has taken some time.

  • 02:00:12 Why?

  • 02:00:13 Because currently we have no index.

  • 02:00:16 Therefore the query is taking a lot of time to execute.

  • 02:00:22 How can we know how much time it is taking or it suggests us any index or not.

  • 02:00:32 So we have saved it the queries in our text file.

  • 02:00:40 I am opening it uh from open for uh folder of the project but in debug .NET core and

  • 02:00:57 here sql queries.

  • 02:01:02 Okay so oh since we have used write all text uh it is erasing the previous records so this

  • 02:01:11 has to be append all text and okay now it won't delete and I will execute it here.

  • 02:01:25 Okay it is executed now if you clicked actual include actual execution plan here it will

  • 02:01:34 show you here how the query is executed so you see which one of the things are taking,

  • 02:01:40 how much time the sorting of top 500 taking 90 of the time, and then it does a table scan

  • 02:01:50 and if I add a predicate here a student id equal to let's say 32.

  • 02:01:58 Now when I run it, it will show me the execution plan.

  • 02:02:04 Okay, currently you see it has taken so um, much lesser time for sorting.

  • 02:02:13 Uh, however, this is still not very effective because it doesn't have an index.

  • 02:02:20 If if we had an index it would be much faster, but I won't get that right now.

  • 02:02:27 Just uh.

  • 02:02:29 You should have an idea about how to use here and if we had much higher number of uh, queries,

  • 02:02:37 uh I mean records, it will take uh much, um more time.

  • 02:02:47 I think.

  • 02:02:48 Maybe I can show indexes but it is then getting out of our course context so I won't get delve

  • 02:02:57 into indexes I think.

  • 02:03:01 But you get the idea.

  • 02:03:05 Okay, okay

  • 02:03:18 okay

  • 02:03:29 what else I can show you?

  • 02:03:32 Um, if if I more get into the sql queries then the course uh, context shifts into databases.

  • 02:03:46 Course databases course is another course but i'm teaching you the fundamentals of database

  • 02:03:56 as well and how to do database operations with c sharp as programmatically okay and

  • 02:04:09 I think for today, this is enough.

  • 02:04:11 I i am hoping that you will understand every part of this lecture because this lecture

  • 02:04:19 is so important that you to understand how to do database operations.

  • 02:04:27 How to uh, record uh, data in database, retrieve that record from database uh with uh var query

  • 02:04:40 sort query.

  • 02:04:41 Such as let's do another var uh filtering.

  • 02:04:49 Okay, I list all records, then I want to uh list uh names that starts with b okay so here

  • 02:05:07 where uh student name like okay and the beginning letter will be b and the rest will be this.

  • 02:05:18 This means that it will retrieve all student names that starts with b letter and the rest

  • 02:05:25 is not important.

  • 02:05:27 Okay, you see all the students are listed with like and then I can tell the software

  • 02:05:38 that it should end with a.

  • 02:05:42 So here it can be any number of uh, string, numeric, whatever.

  • 02:05:48 Let's list it.

  • 02:05:49 Okay, you see they are starting with b and ending with a.

  • 02:05:54 You can also add or so let's say student name like.

  • 02:06:02 This time I will tell it to end with a okay, now you see all records ending with a or starting

  • 02:06:12 with b and ending with a abdcee.

  • 02:06:18 I am having a hard time to pronounce letters single by single, but you are understanding

  • 02:06:24 the concept.

  • 02:06:25 So this is how you write or uh, query or like query.

  • 02:06:31 I can also add an end here and if I add and let's say I will say that where you don't

  • 02:06:40 write the where again student id equal to 9944.

  • 02:06:45 Okay, so this will list me uh, these many records.

  • 02:06:52 Uh, you see it is ending with a and 9944.

  • 02:06:59 So this is equal to this actually and if you put parentheses it is better to write that

  • 02:07:09 way.

  • 02:07:10 And now our query file should be updated.

  • 02:07:13 Okay you see my all queries are getting logged like this.

  • 02:07:16 I can copy them and run them in here and it would work same and when I click actual execution

  • 02:07:25 plan now it will show me okay, it doesn't make any suggest any index suggestions right

  • 02:07:31 now.

  • 02:07:32 However, if we add more records then it may start uh showing us suggestions but I will

  • 02:07:40 leave it next week.

  • 02:07:53 So by the way, uh these were and or keyword can be used in update or delete queries as

  • 02:08:13 well.

  • 02:08:14 So where uh predicate applies to uh where predicate applies to all select update and

  • 02:08:28 delete queries.

  • 02:08:29 Okay, this is important to understand whatever we select.

  • 02:08:34 We can do it for update or delete as well.

  • 02:08:37 So let's say let's update student name of all student id where student id equal to by

  • 02:08:50 the way.

  • 02:08:52 Okay um where student id equal to uh 5965 or student id equal to or let's say in uh

  • 02:09:11 8907 uh then comma uh this and uh 60, uh 66, uh 86.

  • 02:09:26 Okay, this should update three rows and student name and the new value will be let's say furkan

  • 02:09:34 okay, then update.

  • 02:09:36 Okay, nine rows affected.

  • 02:09:40 Okay and then let's sort them by uh, student name or filter with student name furkan.

  • 02:09:49 So student name equal to furkan and list records.

  • 02:09:55 Okay, so this is the listing and let's select all.

  • 02:10:01 Okay you see, uh, the ids that we have defined in the var clause are here.

  • 02:10:09 Okay, so this way you can also delete them.

  • 02:10:13 However, we haven't added a delete button here and you can try to add delete button

  • 02:10:19 at home.

  • 02:10:20 It is same instead of update.

  • 02:10:21 Then you will, uh, write delete.

  • 02:10:25 Okay, so I will edit next week, but I will leave it to you to do at your home so you

  • 02:10:34 will understand the system better.

  • 02:10:37 Okay now.

  • 02:10:39 Uh, the queries are saved here as well.

  • 02:10:43 You see, so it's a good thing to keep as a history.

  • 02:10:49 And let's update our uh these lectures code to our GitHub repository as usual and okay

  • 02:11:06 here and okay.

  • 02:11:15 Lecture three source code and let's go to our GitHub repository again.

  • 02:11:33 Okay, by the way, I suggest you to watch introduction to programming if you are not sure and object

  • 02:11:43 oriented programming, lectures, and uh, responsive web design.

  • 02:11:49 These are all very good lectures.

  • 02:11:52 Okay, here it has arrived.

  • 02:11:54 You know our uh course channel.

  • 02:11:56 Oh, I should put our course video channel to here.

  • 02:12:00 I haven't but I will update.

  • 02:12:03 You know our channel name.

  • 02:12:04 It is um, ca courses.

  • 02:12:11 Okay, I hope we get more subscribers because our channel is extremely useful and you can

  • 02:12:21 watch the uh playlist of um lectures courses from here.

  • 02:12:30 Okay, okay end of the lecture.

  • 02:12:34 Hopefully see you next week.

Clone this wiki locally