C Sharp Program to Connect to Database-Insert Value in Database Table
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace Connect_Databse
{
class Program
{
static void Main(string[] args)
{
string connectionstring = "data source=VISHAL- PC\\SQL2014;database=Test;uid=sa;password=ka123";
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
Console.WriteLine("Connection opened");
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Data.SqlClient;
namespace Connect_Databse
{
class Program
{
static void Main(string[] args)
{
string connectionstring = "data source=VISHAL- PC\\SQL2014;database=Test;uid=sa;password=ka123";
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
Console.WriteLine("Connection opened");
Console.ReadKey();
}
}
}
OutPut.
Connection opened.
Above we saw that how can we connect to Database using ADO.NET . Now we will try to interact with the database. There are also number of classes and objects and methods in ADO.NET which are used to perform operations on database like Creating Table , Inserting values in the Table , Deleting values, Updating values Etc. Let's write the program to insert values into database Table.
Below is the program to insert value into table using C Sharp ADO.Net properties.
using System;
using System.Data.SqlClient;
namespace insert_value
{
class Program
{
static void Main(string[] args)
{
string connection = "data source=VISHAL- PC\\SQL2014;database=Test;uid=sa;password=sa@123";
SqlConnection con=new SqlConnection(connection);
con.Open();
SqlCommand cmd=new SqlCommand("Insert into WinForm_1 (Name,Age) values('vkk','20')",con);
cmd.ExecuteNonQuery();
Console.WriteLine("Record inserted");
Console.ReadKey();
}
}
}
using System.Data.SqlClient;
namespace insert_value
{
class Program
{
static void Main(string[] args)
{
string connection = "data source=VISHAL- PC\\SQL2014;database=Test;uid=sa;password=sa@123";
SqlConnection con=new SqlConnection(connection);
con.Open();
SqlCommand cmd=new SqlCommand("Insert into WinForm_1 (Name,Age) values('vkk','20')",con);
cmd.ExecuteNonQuery();
Console.WriteLine("Record inserted");
Console.ReadKey();
}
}
}
Output.
C Sharp Program to Connect to Database-Insert Value in Database Table
Reviewed by LanguageExpert
on
August 24, 2017
Rating:
No comments