Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- namespace CallStoreProcedure
- {
- class StartUp
- {
- static void Main(string[] args)
- {
- string connectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=Geography;Integrated Security=True";
- string procedure = "usp_ReturnMountainsWithCountryCurrencyCode";
- DataTable table = new DataTable();
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- connection.Open();
- SqlCommand command = connection.CreateCommand();
- SqlTransaction transaction;
- transaction = connection.BeginTransaction("ExecuteProcedure");
- command.CommandType = CommandType.StoredProcedure;
- command.Connection = connection;
- command.Transaction = transaction;
- try
- {
- command.CommandText = procedure;
- using (var reader = command.ExecuteReader())
- {
- table.Load(reader);
- }
- transaction.Commit();
- }
- catch (Exception ex)
- {
- Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
- Console.WriteLine("Message: {0}", ex.Message);
- try
- {
- transaction.Rollback();
- }
- catch (Exception ex2)
- {
- Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
- Console.WriteLine("Message: {0}", ex2.Message);
- }
- }
- }
- var result = table
- .AsEnumerable()
- .Select(r => $"{r["MountainRange"]} - {r["Currency Code"]}")
- .ToList();
- Console.WriteLine("MountainRange - Currency Code");
- Console.WriteLine(string.Join(Environment.NewLine, result));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement