C# passing multiple oracle PL\SQL blocks gets "Encountered the symbol / " - c#

I'm trying to pass the oracle a PL\SQL script consists of a multiple blocks (begin/end).
The following example code works on SqlDev, but not with C#.
BEGIN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE()) || ' Block1');
END;
/
BEGIN
DBMS_OUTPUT.put_line(TO_CHAR(SYSDATE()) || ' Block2');
END;
C# code:
var str = #"
begin
DBMS_OUTPUT.put_line(TO_CHAR(SYSDATE()) || ' Block1');
end;
/
begin
DBMS_OUTPUT.put_line(TO_CHAR(SYSDATE()) || ' Block2');
end;
";
str = str.Replace(Environment.NewLine, "\n");
ExecuteNonQueryThis(str2)
The error I get:
ORA-06550: line 5, column 1: PLS-00103: Encountered the symbol "/"
Is it even possible to execute a multi-block-script of Oracle via the C#?

just wrap it into one begin end
var str = #"
begin
begin
DBMS_OUTPUT.put_line(TO_CHAR(SYSDATE()) || ' Block1');
end;
begin
DBMS_OUTPUT.put_line(TO_CHAR(SYSDATE()) || ' Block2');
end;
end;";

Related

Oracle 12c equivalent to C# Encoding.ASCII.GetBytes

I am trying to mimic the C# function Encoding.ASCII.GetBytes in Oracle 12c. I am nearly there but can't figure why I get the following results:
Oracle
declare
l_string varchar2(4000) := 'Test';
begin
dbms_output.put_line(utl_raw.cast_to_raw(l_string));
end;
The output from this is:
54657374
C#
internal static string ConvertTest()
{
var inputString = "Test";
Console.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(inputString)));
}
The output from this is:
54-65-73-74
So I seem to be nearly there but I can't figure out why C# has '-' between each byte and Oracle does not.
Is there an Oracle function to replicate the output from C#?
Thanks.
utl_raw.cast_to_raw does not put '-' in between byte code of each character. I dont think there is a direct way to get series of byte codes in the expected format.
One workaround can be, you loop through each character and append '-' after each iteration.
Sample code -
declare
l_string varchar2(4000) := 'Test';
l_result varchar2(4000) := '';
begin
for idx in 1 .. length(l_string)
loop
l_result := l_result || utl_raw.cast_to_raw(SUBSTR(l_string,idx,1));
if idx != length(l_string) then
l_result := l_result || '-';
end if;
end loop;
dbms_output.put_line(l_result);
end;
I think I was chasing the wrong thing!
utl_raw.cast_to_raw is the equivalent to Encoding.ASCII.GetBytes(). It is BitConverter.ToString that is actually inserting the '-' between each byte.
Thanks for your help!

fast report expression matter take part of string and replace it

im looking for a code in fast report 4 which take 2 first alphabet of string and if it was 10, write red for me, if it was 11 right blue for me
another example
{IF started with "ab..." then write "RED"
IF started with "dc..." then write "Blue"}
Something like this?
procedure Page1OnBeforePrint(Sender: TfrxComponent);
begin
if Copy(Memo2.Text,1,2) = 'ab' then
begin
Memo2.Text:= 'Red';
Memo2.Font.Color:= clRed;
end;
if Copy(Memo6.Text,1,2) = 'dc' then
begin
Memo6.Text:= 'Blue';
Memo6.Font.Color:= clBlue;
end;
end;
Try this :
Var Str : String;
begin
Str := 'Your String';
Str := Copy (Str , 1 , 2 );
If Str='Yo' then
ShowMessage('RED')
else
If Str='You' then
ShowMessage('BLUE');

Need to convert oracle function into c# code

I have one function in oracle i need to convert it into c# code
please help or tell me any links to do it.
create or replace
FUNCTION "GETSEPARATEDSTRING"
( pString IN VARCHAR2
,pSeparator IN VARCHAR2
,pReturnNumber IN PLS_INTEGER
)
RETURN VARCHAR2
IS
l_SearchString_FinPos PLS_INTEGER :=0;
l_SearchString_StartPos PLS_INTEGER :=0;
l_SearchString_Length PLS_INTEGER :=0;
l_SearchString_CurrentPos PLS_INTEGER :=0;
l_Return VARCHAR2(4000);
BEGIN
-- expecting values as String Seperator String Seperator
-- so if pReturnNumber = 2 then where are
-- looking for seperators 2 and 1. If there is no seperator
-- at the end of the string it is added before comparison,
-- Will return a null if:
-- The length of pString is > 4000
-- The pSeparator has not been specified
-- The pReturnNumber IS <= 0
-- The pReturnNumber IS greater than the number of pSeparator + 1 and therefore we can't pick up a string
-- There was an empty string at the position requested
-- Strings are returned without pSeparator
IF LENGTH( pString || pSeparator ) <= 4000
AND pSeparator IS NOT NULL
AND pReturnNumber > 0
THEN
l_SearchString_FinPos := pReturnNumber;
l_SearchString_StartPos := pReturnNumber - 1;
-- Concat a seperator at the end of the string so at least we
-- know there is one
IF INSTR( pString, pSeparator, -1, 1) != ( LENGTH( RTRIM( pString )) - LENGTH( pSeparator ) + 1 )
THEN
-- There isn't one at the end so add it
l_Return := pString || pSeparator;
--DBMS_OUTPUT.PUT_LINE('Did not find seperator - ADDING');
ELSE
l_Return := pString;
--DBMS_OUTPUT.PUT_LINE('FOUND seperator');
END IF;
-- Set the start position of where we will check to the
-- the last position we found a pSeparator value.
l_SearchString_CurrentPos := l_SearchString_FinPos;
-- Search for the next pSeparator position
l_SearchString_FinPos := INSTR( l_Return, pSeparator, 1, l_SearchString_CurrentPos );
IF l_SearchString_FinPos != 0
THEN
IF l_SearchString_StartPos != 0
THEN
l_SearchString_CurrentPos := l_SearchString_StartPos;
l_SearchString_StartPos := INSTR( l_Return, pSeparator, 1, l_SearchString_CurrentPos ) + 1;
ELSE
-- If we are looking for the first value then StartPos will = 0
-- and cause INSTR to fail
l_SearchString_CurrentPos := 1;
END IF;
l_SearchString_Length := l_SearchString_FinPos - l_SearchString_StartPos;
l_Return := RTRIM( SUBSTR( l_Return, l_SearchString_StartPos, l_SearchString_Length ), pSeparator );
ELSE
l_Return := NULL;
END IF;
END IF;
RETURN l_Return;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE( 'FUNCTION GetSeperatedString Captured Error: ' || SQLERRM );
RETURN NULL;
END;
I don't see why you can't convert it yourself? What is the problem? You even got a comment describing exactly what the function is doing.
Do something like this:
Convert the method signature and all local variables to C# ones.
Lookup the documentation of INSTR to see if uses a zero-based index as string.IndexOf
Do a straight conversion by looking at one line at a time
Test the function
Rename all variables to have C# names
Refactor to take advantage of C#

c# ip address generator to SQL

I have the following python code I found on the internet, I would like to make a table in a SQL database with every ipv4 address that there is. I dont code in python but its what I found.
My question is
1: Is there T-SQL code I can use to generate the table ? (one column ie 0.0.0.0-255.255.255.255)
2: Is how would I make this in c#? using the fastest method possible ? I know showing the results slows the console application down by 400 %
#!/usr/bin/env python
def generate_every_ip_address():
for octet_1 in range( 256 ):
for octet_2 in range( 256 ):
for octet_3 in range( 256 ):
for octet_4 in range( 256 ):
yield "%d.%d.%d.%d" % (octet_1, octet_2, octet_3, octet_4)
for ip_address in generate_every_ip_address():
print ip_address
Would this work?
DECLARE #a INTEGER
DECLARE #b INTEGER
DECLARE #c INTEGER
DECLARE #d INTEGER
DECLARE #IPADDRESS nvarchar(50)
set #a = 0
WHILE #a < 256
BEGIN
SET #b = 0
WHILE #b < 256
BEGIN
SET #c = 0
WHILE #c < 256
BEGIN
SET #d = 0
WHILE #d < 256
BEGIN
SET #IPADDRESS = CAST(#a AS nvarchar(3)) + '.' + CAST(#b AS nvarchar(3)) + '.' + CAST(#c AS nvarchar(3)) + '.' + CAST(#d AS nvarchar(3))
PRINT #IPADDRESS
SET #d = #d + 1
END
SET #c = #c + 1
END
SET #b = #b + 1
END
SET #a = #a + 1
END
To insert in batches of 16,581,375 rows would be quite straightforward using the following TSQL.
DECLARE #Counter INT
SET #Counter = 0
SET NOCOUNT ON ;
WHILE ( #Counter <= 255 )
BEGIN
RAISERROR('Procesing %d' ,0,1,#Counter) WITH NOWAIT ;
WITH Numbers ( N )
AS ( SELECT CAST(number AS VARCHAR(3))
FROM master.dbo.spt_values
WHERE type = 'P'
AND number BETWEEN 0 AND 255
)
INSERT INTO YourTable
( IPAddress
)
SELECT #Counter + '.' + N1.N + '.' + N2.N + '.' + N3.N
FROM Numbers N1 ,
Numbers N2 ,
Numbers N3
SET #Counter = #Counter + 1
END
Please just use an int IDENTITY column to store each IP address. They're only 32 bits. Fill your table up with whatever else you're storing.

How to add string that includes '=' into stringlist?

A regular expression is used to parse text that include '=' and put split string into a stringlist like key value pair.
But if value contains '=', it can not use list.add(key3+'='+'number=10');
key1 this's done
key2 that costs 10 dollars
key3 number=10 // invalid data, error prompt.
...
how to solve?
Thank you.
Edit:
Thank you all for help.
If I have to add a string that includes '=' into key, how can I solve it?
for example, the text to be parsed may be like this:
maleConsumer=john 1
maleConsumer=eric 2
femaleConsumer=mary 2
maleConsumer=john 8
...
I use regex reg='\b\S+\b' parse text and to put maleconsumer=john into key of stringlist, so that in stringlist, john's record will be:
maleConsumer=john 9 // maleconsumer=john is key, 9 is value
In such case, how can I do it?
Thank you all for your help again.
This works fine in Delphi
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Add('key1=this''s done');
sl.Add('key2=that costs 10 dollars');
sl.Add('key3=number=10');
ShowMessage(sl.Values['key3']); // Displays number=10
finally
sl.Free;
end;
end;
This is better and still works
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Values['key1'] := 'this''s done';
sl.Values['key2'] := 'that costs 10 dollars';
sl.Values['key3'] := 'number=10';
ShowMessage(sl.Values['key3']); // Displays number=10
finally
sl.Free;
end;
end;
BTW, you can specify the separator with TStringList.NameValueSeparator
Using NameValueSeparator to allow = in key
var
sl: TStringList;
begin
sl := TStringList.Create;
try
// Select a separater you are sure will never be used
sl.NameValueSeparator := '|';
sl.Values['maleConsumer=john'] := '1';
sl.Values['maleConsumer=eric'] := '2';
sl.Values['femaleConsumer=mary'] := '2';
sl.Values['maleConsumer=john'] := '8';
ShowMessage(sl.Values['maleConsumer=john']); // Displays 8
finally
sl.Free;
end;
end;
Check if the value contains '=':
if(value.indexOf('=') != -1){
//error prompt
}
Java:
you can use: String.contains() method.
For Delphi; you can set Delimiter and QuoteChar for your strings.
Example:
cars := TStringList.Create;
// Now add some cars to our list - using the DelimitedText property
// with overriden control variables
cars.Delimiter := ' '; // Each list item will be blank separated
cars.QuoteChar := '|'; // And each item will be quoted with |'s
cars.DelimitedText := '|Honda Jazz| |Ford Mondeo| |Jaguar "E-type"|';
look here for detail.
Delphi:
var
LStringList: TStringList;
LStrValue: string;
begin
LStringList := TStringList.Create;
try
// set the value of a key
LStringList.Values['a key'] := 'a value';
// get the value of a key
LStrValue := LStringList.Values['a key'];
finally
FreeAndNil(LStringList);
end;// trye
end;
If you are using Delphi 2009 or later, use TDictionary instead of TStringList. That way you avoid all these hacks required to get TStringList to work properly.

Categories

Resources