Event-Driven Programming Syntax PDF
Document Details
Uploaded by SuitableOnyx9176
Tags
Related
Summary
This document provides C# code examples and SQL syntax for database interactions. It details code for tasks such as retrieving, setting, and performing operations on streams.
Full Transcript
Retrieve/Set Value of Read Timeout // Retrieve int timeout = myHttpClient.Timeout.Milliseconds; // Set myHttpClient.Timeout = TimeSpan.FromMilliseconds(1000); StreamWriter Initialization using (StreamWriter writer = new StreamWriter("filePath.txt")) { writer.WriteLine("Your text here");...
Retrieve/Set Value of Read Timeout // Retrieve int timeout = myHttpClient.Timeout.Milliseconds; // Set myHttpClient.Timeout = TimeSpan.FromMilliseconds(1000); StreamWriter Initialization using (StreamWriter writer = new StreamWriter("filePath.txt")) { writer.WriteLine("Your text here"); } Retrieve Value if Stream Can Read if (stream.CanRead) { // Perform read operations } Import Class to Access MS SQL Server using System.Data; using System.Data.SqlClient; Thread Instantiation Thread thread = new Thread(new ThreadStart(MethodName)); thread.Start(); Database-Specific Syntax Connecting to a Database string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=Username;Password=Password;"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Perform database operations } SQLCommand Syntax 1. Insert string query = "INSERT INTO TableName (Column1, Column2) VALUES (@Value1, @Value2)"; using (SqlCommand cmd = new SqlCommand(query, connection)) { cmd.Parameters.AddWithValue("@Value1", value1); cmd.Parameters.AddWithValue("@Value2", value2); cmd.ExecuteNonQuery(); } 2. Update string query = "UPDATE TableName SET Column1 = @Value WHERE Column2 = @Condition"; using (SqlCommand cmd = new SqlCommand(query, connection)) { cmd.Parameters.AddWithValue("@Value", value); cmd.Parameters.AddWithValue("@Condition", condition); cmd.ExecuteNonQuery(); } 3. Delete string query = "DELETE FROM TableName WHERE Column = @Condition"; using (SqlCommand cmd = new SqlCommand(query, connection)) { cmd.Parameters.AddWithValue("@Condition", condition); cmd.ExecuteNonQuery(); } 4. Select string query = "SELECT * FROM TableName WHERE Column = @Condition"; using (SqlCommand cmd = new SqlCommand(query, connection)) { cmd.Parameters.AddWithValue("@Condition", condition); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // Process each row } } } SQLParameter Instantiation SqlParameter parameter = new SqlParameter("@ParamName", SqlDbType.Int); parameter.Value = 123; Dequeue/Enqueue in Queue Queue queue = new Queue(); // Enqueue queue.Enqueue(10); // Dequeue int item = queue.Dequeue(); Stack Instantiation Stack stack = new Stack(); // Push stack.Push(10); // Pop int item = stack.Pop(); Calling Stored Procedure string storedProc = "StoredProcedureName"; using (SqlCommand cmd = new SqlCommand(storedProc, connection)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ParamName", paramValue); cmd.ExecuteNonQuery(); } Thread and Exception Handling Syntax Assigning Thread Thread thread = new Thread(() => { // Code to run in the thread }); thread.Start(); Try/Catch Syntax try { // Code that might throw an exception } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } SQL Server Database Management Delete Database DROP DATABASE DatabaseName; Create Database CREATE DATABASE DatabaseName;