Advertisement
stoianpp

SQL connection and store procedure invocation

Jul 17th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. //CREATE PROC uspGetCity (@id nvarchar(5))
  2. //AS
  3. //SELECT City from Customers WHERE CustomerID = @id
  4. //GO
  5.  
  6. using System;
  7. using System.Data.SqlClient;
  8.  
  9. namespace DBTest
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             SqlConnection cnn;
  16.             string connectionString = "Server=.;Database=Northwind;Integrated Security=true";
  17.             cnn = new SqlConnection(connectionString);
  18.             try
  19.             {
  20.                 cnn.Open();
  21.                 Console.WriteLine("Connection Open ! ");
  22.                 //string sql = "Select * from customers";
  23.                 //string inputVal = Console.ReadLine();
  24.                 //string sql = "SELECT City from Customers WHERE CustomerID="+"'"+ Console.ReadLine()+ "'";
  25.                 string sql = "EXEC uspGetCity" + "'" + Console.ReadLine() + "'";
  26.                 var command = new SqlCommand(sql, cnn);
  27.                 var dataReader = command.ExecuteReader();
  28.                 while (dataReader.Read())
  29.                 {
  30.                     Console.WriteLine(dataReader.GetValue(0));
  31.                 }
  32.                 dataReader.Close();
  33.                 command.Dispose();
  34.                 cnn.Close();
  35.             }
  36.             catch (Exception ex)
  37.             {
  38.                 Console.WriteLine("Can not open connection ! "+ex.Message);
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement