Qlconnection

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

ADO .Net : In case of the Visual Studio .

Net the database connectivity is done with the help of ADO .Net.ADO stands for ActiveX Database Object. In case of ASP , we will make use of ADO , in which we have the concept of RecordSet , where each RecordSet will contain a single table , but here we have the concept of DataSet , a single DataSet can contain multiple tables,relationship and many more. SqlConnection This class is used for creating the connection object which will be used for connecting to the database . The general form os creating it object is , SqlConnection connectionobject=new SqlConnection(); Properites : ConnectioString : Here we will specify the connection string used for connecting to the database The general format is ,

"Data Source=servername;Initial Catalog=Database Name;Integerated Security=true;" Function : Open() :This function is used to open the connection SqlCommand : This object is used to specify the query statement and the connection object on which the query is to be executed. The general form is , SqlCommand objectname=new SqlCommand("query",connectionobject); The SqlCommand object will contain the function , ExecuteReader() which will executethe query and return the SqlDataReader object reference SqlDataReader This will contain all the rows of the table , which are return from the query.

It contains the following functions : (i) Read() This will move the record pointer to the next record. (ii) GetString() This will access the value from the particular field. The index will provided as an argument.The first field index is 0 and so on. SqlDataAdapter

SqlDataAdpater is used to fill the table with the record accessed by executing the query, so the table in the dataset will containg the records related to the actual table The general form is ,
SqlDataAdapter dataadapterobject = new SqlDataAdapter(query,connectionobject);

Function : fill() : it is used to fill the database table with the records from the query. DataSet : DataSet will contain the collection of tale and it is present in System.Data

DataRow : It is used to refer to a particular row or record of the table. DataSet contains the Tables collection which will contain all the tables. dataset.Tables["tablename"] NewRow() : It is used to insert a new blank record. The datarow object will be considered as an array as each element will refer to a field. e.g. dr is the data row object

dr[0] will refers to the first field of the table dr[1] will refer to the seond field of the table and so on DataAdpater contains the following command object InsertCommand : It is used to insert the record in the table DeleteCommand : It is used to delete the record from the table. UpdateCommand : Is used to update the record already present in the table. All of these command have the two common properties : (a) CommandText : In this we will specif the SQL Statement (b) Connection : In this we will specufy the connection object name.

using using using using using using using using using using using using using

System; System.Collections; System.Configuration; System.Data; System.Linq; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.HtmlControls; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Xml.Linq; System.Data.SqlClient;

public partial class record : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void cmd1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); string cstring; cstring="Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\aspnet430pm\\App_Data\\employee.mdf ;Integrated Security=True;Connect Timeout=30;User Instance=True"; conn.ConnectionString = cstring; conn.Open(); int empid, salary; string ename; empid = int.Parse(txt1.Text); salary = int.Parse(txt3.Text); ename = txt2.Text; SqlCommand cmd=new SqlCommand(); string sql; sql = "insert into emp values (" + empid + ",'" + ename + "'," + salary + ")"; cmd.CommandText = sql;

cmd.Connection = conn; cmd.ExecuteNonQuery(); conn.Close(); lblmsg.Text = "record save successfully"; } }

You might also like