sqldataadapter چیست؟لطفا کامل توضیح دهید.
Printable View
sqldataadapter چیست؟لطفا کامل توضیح دهید.
sqldataadapter وظیفه مدیریت بازیابی اطلاعات از پایگاه داده ها و ایجاد یا نوشتن داده های جدید و تغییر آنها را بر عهده دارد برای مثال یک دستور SELECT * FROM Table1 را اجرا م کند و نتیجه حاصل را به دیتاست می فرسته
DataReader and DataAdapter are both objects of ADO.Net,which enable you to access your database.DataReader is a storage object which dedicated for read only,it has a collection of methods that support SqlDataReader like ExecuteReader which move the command text to the connection instead of using SqlDataAdapter.
Besides that we can use SqlDataAdapter class for read and write data from and to database,i represent SqlDataAdapter like the taxi which reach people from one place to another and come back.SqlDataAdapter do the same but it hold Query statement(select statement)and it will come back to fill data in DataSet or non Query statements (insert,delete and update)and it will come back with the number of the affected rows.
کد:<PRE lang=cs id=pre0 style="MARGIN-TOP: 0px">//Connecting database
con = new SqlConnection(
"Data Source=mysource;Initial Catalog=mydbname;uid=sa");
//create sql adapter for the "emp" table
SqlDataAdapter sqlDa = new SqlDataAdapter("select * from emp", con);
//create dataset instance
DataSet dSet = new DataSet();
//fill the dataset
sqlDa.Fill(dSet, "emp");
//bind the data grid with the data set
dataGrid1.DataSource=dSet.Tables["emp"];
//build select command
SqlCommand selCmd = new SqlCommand("select * from emp",con);
sqlDa.SelectCommand=selCmd;
//build insert command
SqlCommand insCmd = new SqlCommand(
"insert into emp (Name, Age) values(@Name, @Age)",con);
insCmd.Parameters.Add("@Name", SqlDbType.NChar, 10, "Name");
insCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
sqlDa.InsertCommand = insCmd;
//build update command
SqlCommand upCmd = new SqlCommand(
"update emp set Name=@Name, Age=@Age where No=@No",con);
upCmd.Parameters.Add("@Name", SqlDbType.NChar, 10, "Name");
upCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
upCmd.Parameters.Add("@No", SqlDbType.Int, 4, "No");
sqlDa.UpdateCommand = upCmd;
//build delete command
SqlCommand delCmd = new SqlCommand(
"delete from emp where No=@No",con);
delCmd.Parameters.Add("@No", SqlDbType.Int, 4, "No");
sqlDa.DeleteCommand = delCmd;
//now update the data adapter with dataset.
sqlDa.Update(dSet,"emp");
</PRE>