Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# data base sql

using System;
using System.Data.SqlClient;
using System.Data;

namespace AccessingDatabase
{
    class Program
    {
        static void Main(string[] args)
        {     

            //Create the object of SqlConnection class to connect with database sql server
           using( SqlConnection conn=new SqlConnection())
           {
               //prepare conectio string
               conn.ConnectionString = "server=ACER; database=BankingTransactions; Integrated Security=True";
               
               try
               {
                 
                   //Prepare SQL command that we want to query
                   SqlCommand cmd = new SqlCommand();
                   cmd.CommandType = CommandType.Text;
                   // cmd.CommandText = "SELECT * FROM MYTABLE";
                   cmd.CommandText = "SELECT id FROM UserRegistration";  
                   cmd.Connection = conn;

                   // open database connection.
                   conn.Open();
                   
                   Console.WriteLine("Connection Open ! ");

                   //Execute the query 
                 SqlDataReader sdr=  cmd.ExecuteReader();

                 ////Retrieve data from table and Display result
                   while(sdr.Read())
                   {
                       int id = (int)sdr["id"];
                       Console.WriteLine(id);
                   }
                   //Close the connection
                   conn.Close();
               }
               catch (Exception ex)
               {
                   Console.WriteLine("Can not open connection !");
                  
               }
               
           }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity create file name datetime 
Csharp :: c# printwindow 
Csharp :: asp.net core httpdelete with body 
Csharp :: Startup.cs file 
Csharp :: tmpro pageCount update 
Csharp :: .net return manual status code 
Csharp :: function documentation c# exception 
Csharp :: Display the elements in an array one at a time using getkeydown in unity 
Csharp :: move dotnet dlls to another folder 
Csharp :: unity set particle properties through script 
Csharp :: c# load a file into binary buffer 
Csharp :: Destroy(GameObject.Find("Turret_Laser_Hit"), 0.2f); 
Csharp :: asp.net disabled checkbox style 
Csharp :: c# with keyword 
Csharp :: unity prefab button not working 
Csharp :: how to export xml in linq c# 
Csharp :: VideoPlayer.isPlaying 
Csharp :: c# enum variable set to nonthing 
Csharp :: How to enumerate an enum 
Csharp :: vb.net convert int32 into boolean array stack overflow 
Csharp :: c# string is all zeros 
Csharp :: move position smoth unity 
Csharp :: Delegate parameter no return 
Csharp :: sliding window algorithm in c# 
Csharp :: c# return error status code based on exception 
Csharp :: c# same folder path 
Csharp :: C:UsersSherryDocumentssdata.dta 
Csharp :: invalid length for a base-64 char array or string. frombase64string c# 
Csharp :: c# switch two values 
Csharp :: create circumference with nettopologysuite 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =