Friday, 29 April 2016

Connection with SQL Server Part 7

Insert Data using Connection Class:-

on the submit button click event code for insert data using sql class connection

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Insert : System.Web.UI.Page
{

    Class1 obj = new Class1();


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string str="insert into Datas(firstname,lastname,mobile) values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"')";
        obj.savedata(str);
    }
}

In this coding the Class1 obj = new Class1();
 create new object of that class and we can use that class method , in our case we create two method the calldata(), and the savedata();

The calldata(); method is used to retrieve  data from database and 
  
The savadata(); method is used to execute the query

using the object of that class . 

Connection with SQL Server Part 6

SQLCommand

  • SqlCommand deals with databases. It executes SQL commands on a database. It sends an SQL command to a database that is specified by an SqlConnection object. We then call instance methods to physically apply the command to the database.

  • It is Responsible to execute SQL Query 
It is Required to Open Connection with the SqlCommand Class
using Open() Method and after, We Use the ExecuteNonQuery() method for Execute Query and after all we must close the connection using close() method

Connection with SQL Server Part 5

SQLDataAdapter

  • DataAdapter serves as a bridge between a DataSet and SQL Server for retrieving and saving data. We can use SqlDataAdapter Object in combination with Dataset Object. DataAdapter provides this combination by mapping Fill method, which changes the data in the DataSet to match the data in the data source, and Update, which changes the data in the data source to match the data in the DataSet.


C#
  SqlDataAdapter adapter = new SqlDataAdapter(sql,connection );
adapter.Fill(ds);
  • The SqlDataAdapter Object and DataSet objects are combine to perform both data access and data manipulation operations in the SQL Server Database. When the user perform the SQL operations like Select , Insert etc. in the data containing in the Dataset Object , it wont directly affect the Database, until the user invoke the Update method in the SqlDataAdapter.
  • The DataAdapter can perform Select , Insert , Update and Delete SQL operations in the Data Source. The SelectCommand property of the DataAdapter is a Command Object that retrieves data from the data source. Other command properties of the DataAdapter are Command objects that manage updates to the data in the data source according to modifications made to the data in the DataSet.
  • The following ASP.NET program shows a select operation using SqlDataAdapter.

Connection with SQL Server Part 4

Dataset  Description:-

The DataSet contains DataTableCollection and theirDataRelationCollection . It represents a complete set of data including the tables that contain, order, and constrain the data, as well as the relationships between the tables.

We can use Dataset in combination with DataAdapter Class . Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter. The DataSet object offers a disconnected data source architecture. The following link gives you in details about Dataset Class with C# Source Code 


Properties



Initializes a new instance of the DataSet class.

System_CAPS_protmethodDataSet(SerializationInfo, StreamingContext)

This API supports the product infrastructure and is not intended to be used directly from your code. Initializes a new instance of a DataSet class that has the given serialization information and context.

System_CAPS_protmethodDataSet(SerializationInfo, StreamingContext, Boolean)

This API supports the product infrastructure and is not intended to be used directly from your code. Initializes a new instance of the DataSet class.

System_CAPS_pubmethodDataSet(String)
Initializes a new instance of a DataSet class with the given name.

Connection with SQL Server Part 3

The Connection String is used to Open Connection to the specified Sql Database

you can Copy it from Here
In Server Explore ,
Database Connection, You can see your Database

Right click on database and Click Properties


copy the connection string text:-
Past it to the con.connection string class Remove the C:etc\etc from Text And Fill Like this For Run Your application In Any Server

con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|datadirectory|\\Database.mdf;Integrated Security=True;User Instance=True";
        

Connection with SQL Server Part 2

As You can see in code there are some header file which is added for connecting with the sql server

the header file
Using System.Data;
And
Using System.Data.SqlClient;
Is must required so don't forget it

Sqlconnection Description:-


SqlConnection()

Initializes a new instance of the SqlConnection class.

System_CAPS_pubmethodSqlConnection(String)

Initializes a new instance of the SqlConnection class when given a string that contains the connection string.

System_CAPS_pubmethodSqlConnection(String, SqlCredential)

Initializes a new instance of the SqlConnection class given a connection string, that does not useIntegrated Security = true and a SqlCredential object that contains the user ID and password.


Connection with SQL Server Part 1


In Add New Item Select Class And Language C#

Code for it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Class2
/// </summary>
public class Class2
{
    SqlConnection con = new SqlConnection();
public Class2()
{

        con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|datadirectory|\\Database.mdf;Integrated Security=True;User Instance=True";
        
}
    public DataSet calldata(string str)
    {
        SqlDataAdapter adp = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        return ds;
    }
    public void savedata(string str)
    {
        SqlCommand cmd = new SqlCommand(str,con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

}

Part 20 consuming rest api in ionic

in this video, I will show you that how you can consume the rest API. in the previous video we have implemented the asp.net web API projec...