SqlStatementCollection Class
Represents collection of statements that constitute DB2 script.
This sample shows how to retrieve collection of statements that make up the script and execute them one by one.
|
|---|
string script =
"INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');"+
"INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');"+
"INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');"+
"INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');"+
"SELECT * FROM DEPT";
DB2Connection dB2Conn = new DB2Connection(
"user id=db2admin;server=db2;database=SAMPLE;");
DB2Script dB2Script = new DB2Script(script,dB2Conn);
SqlStatementCollection dB2Coll = dB2Script.Statements;
IDataReader dB2Reader;
dB2Conn.Open();
foreach (SqlStatement dB2Statement in dB2Coll)
{
Console.WriteLine(dB2Statement.Text);
try
{
dB2Reader = dB2Statement.Execute();
Console.WriteLine(" Records affected " + dB2Reader.RecordsAffected);
while (dB2Reader.Read())
{
Console.WriteLine(dB2Reader.GetString(0));
}
dB2Reader.Close();
Console.WriteLine();
}
catch
{
Console.WriteLine(" Failed");
}
}
dB2Conn.Close(); |
|
|---|
Dim script As String = _
"INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');" & _
"INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');" & _
"INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');" & _
"INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');" & _
"SELECT * FROM DEPT"
Dim dB2Conn As DB2Connection = New DB2Connection( _
"user id=db2admin;server=db2;database=SAMPLE;")
Dim dB2Script As DB2Script = New DB2Script(script, dB2Conn)
Dim dB2Coll As SqlStatementCollection = dB2Script.Statements
Dim dB2Reader As IDataReader
dB2Conn.Open()
For Each dB2Statement As SqlStatement In dB2Coll
Console.WriteLine(dB2Statement.Text)
Try
dB2Reader = dB2Statement.Execute()
Console.WriteLine(" Records affected " & dB2Reader.RecordsAffected)
While dB2Reader.Read()
Console.WriteLine(dB2Reader.GetString(0))
End While
dB2Reader.Close()
Console.WriteLine()
Catch ex As Exception
Console.WriteLine(" Failed")
End Try
Next dB2Statement
dB2Conn.Close() |