ditec - programming with c#.net
TRANSCRIPT
![Page 1: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/1.jpg)
Diploma in Information Technology
Module IX: Programming with C#.NET
Rasan SamarasingheESOFT Computer Studies (pvt) Ltd.No 68/1, Main Street, Pallegama, Embilipitiya.
![Page 2: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/2.jpg)
Contents1. Introduction to .NET Framework2. .NET Framework Platform Architecture3. Microsoft Visual Studio4. C# Language5. C#, VS and .NET Framework Versions6. Your First C# Application7. Printing Statements8. Comments in C#9. Common Type System10. Value Types and Reference Type11. Variables Declaration in C#12. Type Conversion13. Arithmetic Operators14. Assignment Operators15. Comparison Operators16. Logical Operators17. If Statement18. If… Else Statement19. If… Else if… Else Statement20. Nested If Statement21. Switch Statement22. While Loop23. Do While Loop
23. For Loop24. Arrays25. Accessing Arrays using foreach Loop26. Two Dimensional Arrays27. Classes and Objects in C#28. Inheritance in C#29. Partial Classes30. Namespaces31. Windows Forms Applications32. Using Buttons, Labels and Text Boxes33. Displaying Message Boxes34. Error Handling with Try… Catch… finally…35. Using Radio Buttons36. Using Check Boxes37. Using List Boxes38. Creating Menus39. Creating ToolStrips40. MDI Forms41. Database Application in C#42. Creating a Simple Database Application43. SQL Insert / Update / Retrieving / Delete44. SQL Command Execute Methods45. Data Sets
![Page 3: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/3.jpg)
Introduction to .NET Framework
• The .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.
• It includes a large library and provides language interoperability across several programming languages.
• Programs written for .NET Framework execute in a software environment known as CLR.
• .NET Framework is intended to be used by most new applications created for the Windows platform.
![Page 4: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/4.jpg)
.NET Framework Platform Architecture
![Page 5: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/5.jpg)
Microsoft Visual Studio
• Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft…
• To develop Windows Forms or WPF applications, web sites, web applications, and web services…
• For Microsoft Windows, Windows Mobile, Windows CE, .NET Framework and Microsoft Silverlight.
![Page 6: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/6.jpg)
C# Language
C# is a general purpose, object oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg.
![Page 7: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/7.jpg)
C# Language Features
• Boolean Conditions • Automatic Garbage Collection • Standard Library • Assembly Versioning • Properties and Events • Delegates and Events Management • Easy-to-use Generics • Indexers • Conditional Compilation • Simple Multithreading • LINQ and Lambda Expressions • Integration with Windows
![Page 8: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/8.jpg)
C#, VS and .NET Framework Versions
C# Version Visual Studio Version .NET Framework VersionC# 1.0 Visual Studio .NET 2002 .NET Framework 1.0
C# 1.2 Visual Studio .NET 2003 .NET Framework 1.1
C# 2.0 Visual Studio 2005 .NET Framework 2.0
C# 3.0 Visual Studio 2008Visual Studio 2010
.NET Framework 2.0
.NET Framework 3.0
.NET Framework 3.5
C# 4.0 Visual Studio 2010 .NET Framework 4
C# 5.0 Visual Studio 2012Visual Studio 2013 .NET Framework 4.5
![Page 9: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/9.jpg)
Your First C# Application
using System;
namespace myspace{
class FirstApp{ static void Main() { Console.WriteLine(“Hello World!”); }}
}
![Page 10: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/10.jpg)
Printing Statements
Console.Write(“Hello World!”); //prints a text
Console.WriteLine(“Hello World!”); //prints text and starts a new line
Console.Write(“Hello \nWorld!”); //line breaks
Console.Write(“Hello {0}”, “Esoft”); //variable displaying
![Page 11: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/11.jpg)
Comments in C#
Single line comments
// this is a single line comment
Multiline comments
/* this is a multilinecomment*/
![Page 12: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/12.jpg)
Common Type System
![Page 13: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/13.jpg)
Value Types and Reference TypePredefined Value Types
sbyte -128 to 127 8 bitsbyte 0 to 255 8 bitsshort -32,768 to 32,767 16 bitsushort 0 to 65,535 16 bitsint -2,147,483,648 to 2,147,483,647 32 bitsuint 0 to 4,294,967,295 32 bitslong -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 64 bitsulong 0 to 18,446,744,073,709,551,615 64 bitschar 16 bit Unicode character 16 bitsfloat -3.4 x 1038 to + 3.4 x 1038 32 bitsdouble (+/-)5.0 x 10-324 to (+/-) 1.7 x 10308 64 bitsdecimal (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 128 bitsbool Holds true or false 1 bit
Predefined Reference Typesstring Represents a string of Unicode characters 20+ bitsobject Represents a general purpose type 8+ bits
![Page 14: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/14.jpg)
Variable Declaration in C#
Variable declarationtype variable_list;
Variable declaration and initializationtype variable_name = value;
![Page 15: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/15.jpg)
Variable Declaration in C#
int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing d and f.
bool z = 5>2; // declares and initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
![Page 16: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/16.jpg)
Type Conversion
• Implicit Conversion – No special syntax is required because the
conversion is type safe and no data will be lost.
• Explicit Conversion– Require a cast operator, and information might be
lost in the conversion.
• Conversions with helper classes– To convert between non-compatible types.
![Page 17: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/17.jpg)
Implicit Conversion
int x = 123;double y = x;
![Page 18: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/18.jpg)
Explicit Conversion
double y = 123.5;int x = (int)y;
![Page 19: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/19.jpg)
Conversions with helper classes
String x = "123";int y = Int.Parse(x);int z = Convert.ToInt32(x);
![Page 20: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/20.jpg)
Arithmetic Operators
Operator Description Example+ Addition X + Y will give 60
- Subtraction X - Y will give -20
* Multiplication X * Y will give 800
/ Division Y / X will give 2
% Modulus Y % X will give 0
++ Increment Y++ gives 41
-- Decrement Y-- gives 39
X = 20, Y = 40
![Page 21: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/21.jpg)
Assignment Operators
Operator Example
= Z = X + Y will assign value of X + Y into Z
+= Z += X is equivalent to Z = Z + X
-= Z -= X is equivalent to Z = Z - X
*= Z *= X is equivalent to Z = Z * X
/= Z /= X is equivalent to Z = Z / X
%= Z %= X is equivalent to Z = Z % X
![Page 22: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/22.jpg)
Comparison Operators
Operator Example
== (X == Y) is false.
!= (X != Y) is true.
> (X > Y) is false.
< (X < Y) is true.
>= (X >= Y) is false.
<= (X <= Y) is true.
X = 50, Y = 70
![Page 23: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/23.jpg)
Logical Operators
Operator Name Example
&& AND (X && Y) is False
|| OR (X || Y) is True
! NOT !(X && Y) is True
X = True, Y = False
![Page 24: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/24.jpg)
If Statement
if(Boolean_expression){ // Statements will execute if the Boolean
expression is true}
Boolean Expression
Statements
True
False
![Page 25: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/25.jpg)
If… Else Statement
if(Boolean_expression){ // Executes when the Boolean expression is true}else{ // Executes when the Boolean
expression is false}
Boolean Expression
Statements
True
False
Statements
![Page 26: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/26.jpg)
If… Else if… Else Statementif(Boolean_expression 1){ // Executes when the Boolean expression 1 is true}else if(Boolean_expression 2){ // Executes when the Boolean expression 2 is true}else if(Boolean_expression 3){ // Executes when the Boolean expression 3 is true}else { // Executes when the none of the above condition is true.}
![Page 27: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/27.jpg)
If… Else if… Else Statement
Boolean expression 1
False
Statements
Boolean expression 2
Boolean expression 3
Statements
Statements
False
False
Statements
True
True
True
![Page 28: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/28.jpg)
Nested If Statementif(Boolean_expression 1){ // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true }}
Boolean Expression 1
True
False
StatementsBoolean Expression 2
True
False
![Page 29: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/29.jpg)
Switch Statement
switch (value) { case constant: // statements break; case constant: // statements break; default: // statements break; }
![Page 30: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/30.jpg)
While Loop
while(Boolean_expression){ // Statements}
Boolean Expression
Statements
True
False
![Page 31: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/31.jpg)
Do While Loop
do{ // Statements}while(Boolean_expression);
Boolean Expression
Statements
True
False
![Page 32: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/32.jpg)
For Loop
for(initialization; Boolean_expression; update){ // Statements}
Boolean Expression
Statements
True
False
Update
Initialization
![Page 33: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/33.jpg)
Arrays
10 30 20 50 15 35
0 1 2 3 4 5
Size = 6
Element Index No
An Array can hold many values in a same data type under a single name
A single dimensional array
![Page 34: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/34.jpg)
Building a Single Dimensional Array
// creating an ArrayDataType[] ArrayName = new DataType[size];
// assigning valuesArrayName[index] = value;ArrayName[index] = value;……..
![Page 35: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/35.jpg)
Building a Single Dimensional Array
char[] letters = new char[4];
letters[0] = ‘O’;letters[1] = ‘P’;letters[2] = ‘Q’;letters[3] = ‘R’;
0 1 2 3
O P Q R
0 1 2 3
letters
letters
Values in an Array can access by referring index number
![Page 36: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/36.jpg)
Building a Single Dimensional Array
// creating an array using array initializerDataType[] ArrayName = {element 1, element 2,
element 3, … element n};
int[] marks = {10, 20, 30, 40, 50}; 10 20 30 40
0 1 2 3
intArr
50
4
![Page 37: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/37.jpg)
Accessing Arrays using foreach Loop
// creating an Arrayint[] marks = {10, 29, 30, 40, 50, 70};
// accessing array elementsforeach(int n in marks){System.Console.WriteLine(n);}
![Page 38: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/38.jpg)
Two Dimensional Arrays
5 10 15
25 50 75
0 1 2
0
1
int[,] myMatrix = new int[2,3];
myMatrix[0,0] = 5;myMatrix[0,1] = 10;myMatrix[0,2] = 15;myMatrix[1,0] = 25;myMatrix[1,1] = 50;myMatrix[1,2] = 75;
Rows Columns
Column Index
Row Index
![Page 39: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/39.jpg)
Two Dimensional Arrays
Using array initializer…
int[,] myMatrix = new int[2,3] {{5,10,15},{25,50,75}};
int[,] myMatrix = {{5,10,15},{25,50,75}};
Or
![Page 40: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/40.jpg)
Classes and Objects in C#
Method
Student
nameage
Register()
class Student{
public String name;public int age;
public Student(){}
public void Register(){Console.WriteLine(“Registered!”);}
}
Attributes
Constructor
![Page 41: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/41.jpg)
C# Objects
Student st = new Student(); //creating an object //Assigning values to Attributesst.name = “Roshan”; st.age = 20;
//calling methodst.Register();
![Page 42: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/42.jpg)
Inheritance in C#class Animal{//attributes and methods}
class Lion : Animal{//attributes and methods}
class Cat : Animal{//attributes and methods}
Animal
Lion Cat
![Page 43: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/43.jpg)
Partial Classespartial class Student{public int age;}
partial class Student{public String name;}
partial class Student{public String address;
public void Register(){Console.Write("Student Registered");}}
Partials of the same class
![Page 44: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/44.jpg)
Namespaces
• Namespace is an organization construct.
• It’s a group of collected types.
• It’s helping to understand how the code base is arranged.
• Namespaces are not essential for C# programs.
namespace esoft{
class ditec{
}
class dise{
}
}
![Page 45: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/45.jpg)
Importing Namespaces
Importing Namespace by using “using” directiveusing System.Linq;
Importing Namespace with alias directiveusing ns = System.Linq;
Direct accessing NamespacesSystem.Console.WriteLine(“hello”);
![Page 46: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/46.jpg)
Windows Forms Applications
• A Windows Forms application is an event-driven application supported by Microsoft's .NET Framework.
• This model provides object-oriented, extensible set of classes that enable you to develop rich Windows applications.
![Page 47: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/47.jpg)
Using Button, Labels and Text Boxes
private void button1_Click(object sender, EventArgs e){ label2.Text = "Welcome " + textBox1.Text;}
![Page 48: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/48.jpg)
Displaying Message Boxes
MessageBox.Show("Hello World","MessageBox Demo",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
MessageBox.Show("Hello World“);
![Page 49: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/49.jpg)
Error Handling with Try… Catch… finally…
try{//Protected code }catch (Exception ex){//Catch block}finally{//The finally block always executes}
![Page 50: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/50.jpg)
Using Radio Buttons
private void button1_Click(object sender, EventArgs e){ if (radioButton1.Checked) { MessageBox.Show("You have chosen Black"); } else { MessageBox.Show("You have chosen White"); } }
![Page 51: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/51.jpg)
Using Check Boxes
private void button1_Click(object sender, EventArgs e){ if (checkBox1.Checked) { MessageBox.Show("You have accepted Terms of Agreement"); } else { MessageBox.Show("You haven't accepted Terms of Agreement"); } }
![Page 52: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/52.jpg)
Using List BoxesStatements for each button click events
listBox1.Items.Add(textBox1.Text);
listBox1.Items.AddRange(new String[] { “America", “Japan", “India" });
listBox1.Items.Insert(0, textBox1.Text);
listBox1.Items.Remove(listBox1.SelectedItem);
listBox1.Items.RemoveAt(int.Parse(textBox1.Text));
listBox1.Items.Clear();
listBox1.Sorted = true;
![Page 53: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/53.jpg)
Creating Menus
private void newToolStripMenuItem_Click(object sender, EventArgs e){ MessageBox.Show("You have clicked New Menu item!");}
![Page 54: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/54.jpg)
Creating ToolStrips
private void newToolStripButton_Click(object sender, EventArgs e){ MessageBox.Show("You have clicked newToolStripButton!");}
![Page 55: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/55.jpg)
MDI Forms
private void document1ToolStripMenuItem_Click(object sender, EventArgs e){ Document_One form1 = new Document_One(); form1.MdiParent = this; form1.Show();}
![Page 56: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/56.jpg)
Database Application in C#
Application DataBase
Insert, Update, Retrieving and Delete Processes
![Page 57: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/57.jpg)
Creating a Simple Database Application
Creating a simple database application involved with following steps.
1. Import the namespaces2. Creating a SQL Connection3. Open SQL Connection4. Create a SQL Command5. Execute the SQL Command6. Extract data from SQL Data Reader7. Clean up the environment
![Page 58: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/58.jpg)
Creating a Simple Database Applicationusing System;using System.Data.SqlClient;
class Program{static void Main(string[] args) { SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=esoftdb; Integrated Security=yes"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM tblStudent", con); SqlDataReader rd = cmd.ExecuteReader(); while (rd.Read()) { Console.Write(rd["id"].ToString() + " "); Console.Write(rd["name"].ToString() + " "); Console.WriteLine(rd["address"].ToString()); } rd.Close(); con.Close(); Console.ReadLine();}}
1
2
345
6
7
![Page 59: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/59.jpg)
SQL Insert / Update / Retrieving / Delete
Inserting Data into Database
SqlCommand cmd = new SqlCommand(“INSERT tblStudent VALUES(1, ‘Roshan’, ‘Colombo’)", con);
Updating Data in Database
SqlCommand cmd = new SqlCommand(“UPDATE tblStudent SET address=‘kandy’ WHERE id=1", con);
Retrieving Data from Database
SqlCommand cmd = new SqlCommand("SELECT * FROM tblStudent", con);
Deleting Data in Database
SqlCommand cmd = new SqlCommand(“DELETE FROM tblStudent WHERE id=1", con);
![Page 60: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/60.jpg)
SQL Command Execute Methods
• ExecuteNonQuery() : Executes a Transact-SQL statement against the connection and returns the number of rows affected.
• ExecuteReader() : Sends the CommandText to the Connection and builds a SqlDataReader.
• ExecuteScalar() : Executes the query, and returns the first column of the first row in the result set returned by the query.
int n = cmd.ExecuteNonQuery();
SqlDataReader rd = cmd.ExecuteReader();
String name= cmd.ExecuteScalar();
![Page 61: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/61.jpg)
Data Sets
Represents an in-memory cache of data.
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=esoftdb; Integrated Security=yes");SqlCommand cmd = new SqlCommand("SELECT * FROM tblStudent", con);SqlDataAdapter da = new SqlDataAdapter(cmd);DataSet ds = new DataSet();da.Fill(ds, “tblStudent”);
![Page 62: DITEC - Programming with C#.NET](https://reader037.vdocuments.us/reader037/viewer/2022110109/5883fce81a28ab884b8b4ff9/html5/thumbnails/62.jpg)
The End
http://twitter.com/rasansmn