C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ADO.NET SQL Server ConnectionTo connect with SQL Server, we must have it installed in our system. We are using Microsoft SQL Server Management Tool to connect with the SQL Server. We can use this tool to handle database. Now, follow the following steps to connect with SQL Server.
It will prompt for database connection. Provide the server name and authentication. After successful connection, it displays the following window. Now, create database by selecting database option then right click on it. It pops up an option menu and provides couple of options. Click on the New Database then it will ask for the database name. Here, we have created a Student database. Click on the Ok button then it will create a database that we can see in the left window of the below screenshot. After creating database, now, let's create a table by using the following C# code. In this source code, we are using created student database to connect. In visual studio 2017, we created a .NET console application project that contains the following C# code. // Program.cs using System; using System.Data.SqlClient; namespace AdoNetConsoleApplication { class Program { static void Main(string[] args) { new Program().CreateTable(); } public void CreateTable() { SqlConnection con = null; try { // Creating Connection con = new SqlConnection("data source=.; database=student; integrated security=SSPI"); // writing sql query SqlCommand cm = new SqlCommand("create table student(id int not null, name varchar(100), email varchar(50), join_date date)", con); // Opening Connection con.Open(); // Executing the SQL query cm.ExecuteNonQuery(); // Displaying a message Console.WriteLine("Table created Successfully"); } catch (Exception e) { Console.WriteLine("OOPs, something went wrong."+e); } // Closing the connection finally { con.Close(); } } } } Execute this code using Ctrl+F5. After executing, it displays a message to the console as below. We can see the created table in Microsoft SQL Server Management Studio also. It shows the created table as shown below. See, we have a table here. Initially, this table is empty so we need to insert data into it. // Program.cs using System; using System.Data.SqlClient; namespace AdoNetConsoleApplication { class Program { static void Main(string[] args) { new Program().CreateTable(); } public void CreateTable() { SqlConnection con = null; try { // Creating Connection con = new SqlConnection("data source=.; database=student; integrated security=SSPI"); // writing sql query SqlCommand cm = new SqlCommand("insert into student (id, name, email, join_date)values('101','Ronald Trump','ronald@example.com','1/12/2017')", con); // Opening Connection con.Open(); // Executing the SQL query cm.ExecuteNonQuery(); // Displaying a message Console.WriteLine("Record Inserted Successfully"); } catch (Exception e) { Console.WriteLine("OOPs, something went wrong."+e); } // Closing the connection finally { con.Close(); } } } } Execute this code by using Ctrl+F5 and it will display the following output. Here, we will retrieve the inserted data. Look at the following C# code. // Program.cs using System; using System.Data.SqlClient; namespace AdoNetConsoleApplication { class Program { static void Main(string[] args) { new Program().CreateTable(); } public void CreateTable() { SqlConnection con = null; try { // Creating Connection con = new SqlConnection("data source=.; database=student; integrated security=SSPI"); // writing sql query SqlCommand cm = new SqlCommand("Select * from student", con); // Opening Connection con.Open(); // Executing the SQL query SqlDataReader sdr = cm.ExecuteReader(); // Iterating Data while (sdr.Read()) { Console.WriteLine(sdr["id"] + " " + sdr["name"]+" "+sdr["email"]); // Displaying Record } } catch (Exception e) { Console.WriteLine("OOPs, something went wrong.\n"+e); } // Closing the connection finally { con.Close(); } } } } Execute this code by Ctrl+F5 and it will produce the following result. This displays two records, one we inserted manually. Output: This time student table contains two records. The following C# code delete one row from the table. // Program.cs using System; using System.Data.SqlClient; namespace AdoNetConsoleApplication { class Program { static void Main(string[] args) { new Program().CreateTable(); } public void CreateTable() { SqlConnection con = null; try { // Creating Connection con = new SqlConnection("data source=.; database=student; integrated security=SSPI"); // writing sql query SqlCommand cm = new SqlCommand("delete from student where id = '101'", con); // Opening Connection con.Open(); // Executing the SQL query cm.ExecuteNonQuery(); Console.WriteLine("Record Deleted Successfully"); } catch (Exception e) { Console.WriteLine("OOPs, something went wrong.\n"+e); } // Closing the connection finally { con.Close(); } } } } Output: It displays the following output. We can verify it by retrieving data back by using SqlDataReader.
Next TopicADO.NET Connection
|