0% found this document useful (0 votes)
10 views

Ado Syntax

This document contains code examples for executing SQL commands and queries from .NET code. It shows how to: 1) Define SQL parameters and set their direction and values. 2) Execute a SQL query and retrieve the results directly as XML using the FOR XML clause. 3) Execute a SQL query and retrieve the results as a data reader, then output the values of columns. 4) Use a SQL data adapter to fill a dataset, and generate SQL insert, update and delete commands automatically using a SQL command builder.

Uploaded by

vivek85.garg2637
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Ado Syntax

This document contains code examples for executing SQL commands and queries from .NET code. It shows how to: 1) Define SQL parameters and set their direction and values. 2) Execute a SQL query and retrieve the results directly as XML using the FOR XML clause. 3) Execute a SQL query and retrieve the results as a data reader, then output the values of columns. 4) Use a SQL data adapter to fill a dataset, and generate SQL insert, update and delete commands automatically using a SQL command builder.

Uploaded by

vivek85.garg2637
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

SQL Command

Param = New SqlParameter("@CategoryName", SqlDbType.VarChar, 15)


Param.Direction = ParameterDirection.Input
Or
Param.Direction = ParameterDirection.Output

Param.Value = "Beverages"
Cmd.Parameters.Add(Param)

For XML output


Cmd = New SqlCommand("SELECT * FROM Orders WHERE OrderId=10270" & _

" FOR XML Auto", Conn)

XmlRdr = Cmd.ExecuteXmlReader
xmlRdr.MoveToContent
Console.WriteLine(xmlRdr.ReadOuterXml)
In the example above we have used the FOR XML AUTO clause in the SELECT
statement to retrieve results directly as XML from the SQL Server 2000. We
also used the ExecuteXmlReader() method of the Command object to create an
XMLReader object containing XML returned from the SQL Server 2000.

Dim DR As SqlDataReader = Cmd.ExecuteReader()


While DR.Read()
Console.WriteLine(DR("FirstName") & " " & DR("LastName"))
End While

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.XML

Module Cons

Sub Main()

Dim DA As SqlDataAdapter
Dim DS As New DataSet
Dim CmdBld As SqlCommandBuilder

DA = New SqlDataAdapter("SELECT * FROM Employees", _


"server=localhost;database=Northwind;uid=sa;pwd=;")

DA.Fill(DS, "Employees")

CmdBld = New SqlCommandBuilder(DA)

Console.WriteLine(DA.SelectCommand.CommandText)
Console.WriteLine(CmdBld.GetInsertCommand.CommandText)
Console.WriteLine(CmdBld.GetUpdateCommand.CommandText)
Console.WriteLine(CmdBld.GetDeleteCommand.CommandText)

End Sub

End Module

You might also like