Net-informations.com
SiteMap  | About    

ASP.NET DataAdapter Commands

DataAdapter is used to retrieve data from a data source and populate tables within a DataSet. The DataAdapter also resolves changes made to the DataSet back to the data source

InsertCommand , UpdateCommand , and DeleteCommand 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.

DataAdapter.Insert Command

vb.net
  Dim adapter As New SqlDataAdapter()
  adapter.InsertCommand = New SqlCommand(insertSql, connection)

adapter.InsertCommand.ExecuteNonQuery()

C#
  SqlDataAdapter adapter = new SqlDataAdapter();
  adapter.InsertCommand = new SqlCommand(insertSql, connection);

adapter.InsertCommand.ExecuteNonQuery();

DataAdapter.UpdateCommand Command

vb.net
  Dim adapter As New SqlDataAdapter()
  adapter.UpdateCommand = New SqlCommand(updateSql, connection)

adapter.UpdateCommand.ExecuteNonQuery()

C#
  SqlDataAdapter adapter = new SqlDataAdapter();
  adapter.UpdateCommand = new SqlCommand(updateSql, connection);

adapter.UpdateCommand.ExecuteNonQuery();

DataAdapter.DeleteCommand Command

vb.net
  Dim adapter As New SqlDataAdapter()
  adapter.DeleteCommand = New SqlCommand(deleteSql, connection)

adapter.DeleteCommand.ExecuteNonQuery()

C#
  SqlDataAdapter adapter = new SqlDataAdapter();
  adapter.DeleteCommand = new SqlCommand(deleteSql, connection);

adapter.DeleteCommand.ExecuteNonQuery();

The following ASP.NET program shows how to insert data in the Data Source using SqlDataAdapter and SqlCommand object. Open a connection to the Data Source with the help of SqlConnection object and create a SqlCommand object with insert SQL statement, and assign the SqlCommand to the SqlDataAdapters InsertCommand.

Default.aspx

  

	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head id="Head1" runat="server">
		<title>Untitled Page</title>
	</head>
	<body>
		<form id="form1" runat="server">
		<div>
			<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
			<br />
			<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
		</div>
		</form>
	</body>
	</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb





net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.