C# Artificial Intelligence Login Details - Application Based On SQL Store Procedure

Introduction

 
Artificial Intelligence is built to perform smart tasks. This application shows the login detail record values based on a SQL store procedure. Voice recognition receives the input string value showing the output parameters. First, we must create a simple SQL database. Then create a simple table and insert some sample values.
 

SQL Database Process 

 
Step 1 - SQL Database Creation
 
The database name is MobileContact. 
 
  1. CREATE DATABASE MobileContact;   
Step 2 - Create a sample table
 
The Table name is Contact.
  1. CREATE TABLE Contact (      
  2. ID int NOT NULL,          
  3. Username nchar(10),            
  4. Mobile nchar(10)       
  5. PRIMARY KEY (ID),    
  6. pword nchar(10),    
  7. MailID nchar(20)    
  8. );       
Step 3 - Inserting Sample Records
  1. insert into Contact(Username,Mobile, pword, MailID) Values('Barath','12345', ‘test123 ’,’ BarathID’);     
Step 4 - Create the Random select Procedure
 
The randomly selected procedure name is the login.
  1. Create PROCEDURE  login @ID nchar(10)      
  2. AS          
  3.     Select * from Contact where ID=@ID      
  4. go           
Step 5
 
Declare the Class Headers. (Speech.Recognition, Speech.Synthesis, System.Data, System.SqlClient)
  1. using System.Speech.Recognition;      
  2. using System.Speech.Synthesis;     
  1. using System.Data;      
  2. using System.Data.SqlClient;      
SQL Database connection string,
  1. public static string sqlDataSource = "Data Source=DESKTOP-UG1HSPR\\SQLEXPRESS;Initial Catalog=MobileContact;Integrated Security=True";     
Sample Code of Console Application
  • The console application is suitable for C# AI development.
  • In an open-source operating system and in the testing process it's easy to add more reference edition classes.
  • It has a low build time.
Step 1
 
Create a new project
 
 
Step 2
 
Select Console Application
 
 
Step 3
 
Open Program.cs to type the below code
 
Explanation of RecognizeSpeechAndWriteToConsole()  
 
After creating a Recognize speech and write to console, recognize that the string value has loaded grammar. If speech is recognized, call the specified method. Then Set the input to the default audio device to Recognize speech asynchronously. 
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6. using System.Threading;    
  7. using System.Speech.Recognition;    
  8. using System.Speech.Synthesis;    
  9. using System.Data;    
  10. using System.Data.SqlClient;    
  11.     
  12. namespace AI1    
  13. {    
  14.     class Program    
  15.     {    
  16.         static SpeechRecognitionEngine _recognizer = null;    
  17.         static ManualResetEvent manualResetEvent = null;    
  18.         static void Main(string[] args)    
  19.         {    
  20.             manualResetEvent = new ManualResetEvent(false);    
  21.             Console.WriteLine("To recognize speech, and Show 'Login Details" +    
  22.                 "' to the console, press 0");    
  23.             ConsoleKeyInfo pressedKey = Console.ReadKey(true);    
  24.             char keychar = pressedKey.KeyChar;    
  25.             Console.WriteLine("Please Tell me your username '{0}'", keychar);    
  26.     
  27.             switch (keychar)    
  28.             {    
  29.                 case '0':    
  30.                     RecognizeSpeechAndWriteToConsole();    
  31.                     break;    
  32.             }    
  33.             if (keychar != '5')    
  34.             {    
  35.                 manualResetEvent.WaitOne();    
  36.             }    
  37.             if (_recognizer != null)    
  38.             {    
  39.                 _recognizer.Dispose();    
  40.             }    
  41.     
  42.             Console.WriteLine("Press any key to continue . . .");    
  43.             Console.ReadKey(true);    
  44.         }    
  45.         static void RecognizeSpeechAndWriteToConsole()    
  46.         {    
  47.             _recognizer = new SpeechRecognitionEngine();    
  48.             _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("Barath")));     
  49.             _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("Logout")));    
  50.             _recognizer.SpeechRecognized += _recognizeSpeechAndWriteToConsole_SpeechRecognized;     
  51.             _recognizer.SpeechRecognitionRejected += _recognizeSpeechAndWriteToConsole_SpeechRecognitionRejected;     
  52.             _recognizer.SetInputToDefaultAudioDevice();     
  53.             _recognizer.RecognizeAsync(RecognizeMode.Multiple);     
  54.     
  55.         }    
  56.         static void _recognizeSpeechAndWriteToConsole_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)    
  57.         {    
  58.             if (e.Result.Text == "Barath")    
  59.             {    
  60.                 string val = "Barath";                      
  61.                              
  62.                 SqlConnection dc = new SqlConnection("Data Source=DESKTOP-UG1HSPR\\SQLEXPRESS;Initial Catalog=MobileContact;Integrated Security=True");    
  63.     
  64.                 using (SqlCommand cmd = new SqlCommand("login", dc))    
  65.                 {    
  66.                     cmd.CommandType = CommandType.StoredProcedure;    
  67.                     cmd.Parameters.AddWithValue("@username", val.ToString());    
  68.                     dc.Open();    
  69.                     SqlDataReader dr;    
  70.                     dr = cmd.ExecuteReader();    
  71.                     if (!dr.Read())    
  72.                     {    
  73.                         Console.Write("Record not found ");    
  74.                     }    
  75.                     else    
  76.                     {    
  77.                         string b = Convert.ToString(val);    
  78.                         b = dr[1].ToString();    
  79.                         string c = Convert.ToString(val);    
  80.                         c = dr[2].ToString();    
  81.                         string d = Convert.ToString(val);    
  82.                         d = dr[3].ToString();    
  83.                         string f = Convert.ToString(val);    
  84.                         f = dr[4].ToString();    
  85.                         Console.WriteLine("Your User name is: {0}", b.ToString());    
  86.                         Console.WriteLine("Your Mobile No is: {0}", c.ToString());    
  87.                         Console.WriteLine("Your Password is: {0}", d.ToString());    
  88.                         Console.WriteLine("Your Mail ID is: {0}", f.ToString());    
  89.                         Console.ReadLine();    
  90.                     }    
  91.                     dc.Close();    
  92.                 }                  
  93.             }    
  94.             else if (e.Result.Text == "Logout")    
  95.             {    
  96.                 manualResetEvent.Set();    
  97.             }    
  98.         }    
  99.         static void _recognizeSpeechAndWriteToConsole_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)    
  100.         {    
  101.             Console.WriteLine("Speech rejected. Did you mean:");    
  102.             foreach (RecognizedPhrase r in e.Result.Alternates)    
  103.             {    
  104.                 Console.WriteLine("    " + r.Text);    
  105.             }    
  106.         }    
  107.     }    
  108. }    
Sample Output
 
 

Summary

 
More Artificial Intelligence references are available in C#. For developers, they're easy to download and add a reference file to build large-scale applications. 
 
This application is the basic structure of future development endeavors. The grammar builder functionality is recognizing static speech. Try to implement this on dynamic based speech.
 
I hope this article helps you with your future development.


Similar Articles