c# ip address generator to SQL - c#

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.

Related

TSQL MD5 generation with UTF8

I have a .NET function MD5 that when run on "146.185.59.178acu-cell.com" it returns f36674ed3dbcb151e1c0dfe4acdbb9f5
public static String MD5(String s)
{
using (var provider = System.Security.Cryptography.MD5.Create())
{
StringBuilder builder = new StringBuilder();
foreach (Byte b in provider.ComputeHash(Encoding.UTF8.GetBytes(s)))
builder.Append(b.ToString("x2").ToLower());
return builder.ToString();
}
}
I wrote the same code in TSQL, but for some reason only the varchar returns the expected result. The nvarchar returns a different md5 : f04b83328560f1bd1c08104b83bc30ea
declare #v varchar(150) = '146.185.59.178acu-cell.com'
declare #nv nvarchar(150) = '146.185.59.178acu-cell.com'
select LOWER(CONVERT(VARCHAR(32), HashBytes('MD5', #v), 2))
--f36674ed3dbcb151e1c0dfe4acdbb9f5
select LOWER(CONVERT(VARCHAR(32), HashBytes('MD5',#nv), 2))
--f04b83328560f1bd1c08104b83bc30ea
Not sure what is going on here because I do expect for the nvarchar to return f36674ed3dbcb151e1c0dfe4acdbb9f5 as it does in .NET
You get different hashes because the binary representation of the text is different. The following query demonstrates this:
declare #v varchar(150) = '146.185.59.178acu-cell.com'
declare #nv nvarchar(150) = '146.185.59.178acu-cell.com'
select convert(varbinary(max), #v) -- 0x3134362E3138352E35392E3137386163752D63656C6C2E636F6D
select convert(varbinary(max), #nv) -- 0x3100340036002E003100380035002E00350039002E003100370038006100630075002D00630065006C006C002E0063006F006D00
The extra 0 bytes for the nvarchar are due to the fact that it's a 2-byte Unicode datatype. Refer to MSDN for more information on Unicode in SQL Server.
Turns out I need to explicitly convert NVarChar to UTF8
Found this code on the net:
CREATE FUNCTION [dbo].[fnUTF8] (
#String NVarChar(max)
) RETURNS VarChar(max) AS BEGIN
DECLARE #Result VarChar(max)
,#Counter Int
,#Len Int
SELECT #Result = ''
,#Counter = 1
,#Len = Len(#String)
WHILE (##RowCount > 0)
SELECT #Result = #Result
+ CASE WHEN Code < 128 THEN ''
WHEN Code < 2048 THEN Char(192 + Code / 64)
ELSE Char(224 + Code / 4096)
END
+ CASE WHEN Code < 128 THEN Char(Code)
WHEN Code < 2048 THEN Char(128 + Code % 64)
ELSE Char(128 + Code / 64 % 64)
END
,#Counter = #Counter + 1
FROM (SELECT UniCode(SubString(#String,#Counter,1)) AS Code) C
WHERE #Counter <= #Len
RETURN #Result
END
GO
And now I use it like this:
select LOWER(CONVERT(VARCHAR(32), HashBytes('MD5', [dbo].[fnUTF8](#nv)), 2))

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

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;";

Implementing function within a specific column in an sql table

I have a student's table where there are 7
columns: Reg_No(i.e.Register number of
student),Mark1,Mark2,Mark3,Best1,Best2,Total.
The data Reg_No, Mark1, Mark2 and Mark3 are retrieved from database.
Am just looking for a way to select the maximum 2 marks from Mark1, Mark2 and Mark3 and fill
them in Best1 and Best2 columns.
Finally i shoud produce the added result of Mark1 and Mark2 in Total column. Pls suggest me a way.
I am going to assume you would like an SQL answer.
SELECT Reg_No, Mark1, Mark2, MAX(Mark1) AS Best1, MAX(Mark2)
AS Best2, SUM(Mark1 + Mark2) AS Total FROM Students GROUP BY Reg_No,
Mark1, Mark2
This query probably isn't very useful, though, since it mixes aggregated data with the data to be aggregated. If you only need to see each unique student's best and total grades, a better query would be:
SELECT Reg_No, MAX(Mark1) AS Best1, MAX(Mark2) AS Best2,
SUM(Mark1 + Mark2) AS Total FROM Students GROUP BY Reg_No
You need to use greatest and least functions applied on the field values of a row.
Example:
select
#m1 := 55 m1, #m2 := 42 m2, #m3 := 66 m3,
#b1 := greatest( #m1, #m2, #m3 ) b1,
#b2 := ( ( #total := #m1 + #m2 + #m3 )
- ( #b1 + least( #m1, #m2, #m3 ) )
) b2,
#total total;
+----+----+----+------+------+-------+
| m1 | m2 | m3 | b1 | b2 | total |
+----+----+----+------+------+-------+
| 55 | 42 | 66 | 66 | 55 | 163 |
+----+----+----+------+------+-------+
Try this on your students table:
select
Reg_No, Mark1, Mark2, Mark3,
#b1 := greatest( Mark1, Mark2, Mark3 ) Best1,
#b2 := ( ( #total := Mark1 + Mark2 + Mark3 )
- ( #b1 + least( Mark1, Mark2, Mark3 ) )
) Best2,
#total Total
from students
Refer to:
MySQL: GREATEST(value1,value2,...)
Return the largest argument
MySQL: LEAST(value1,value2,...)
Return the smallest argument

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#

LINQ-to-SQL orderby question

I have a LINQ-to-SQL query, and I order on an nvarchar field called CustomerReference. The problem is, reference's that start with a capital letter seem to be after ones without capitals, when I need this the other way around. For example, if I have the following rows:
d93838
D98484
It is currently ordered in that sequence right now, however I need it reversed - so it'd be like this
D98484
d93838
Any ideas guys? Thanks
This assumes the Format [A-Za-z]\d+ and will put b3432 before C1234 but after B9999
list.OrderBy (l => l.CustomerReference.Substring(0,1).ToLower())
.ThenByDescending(l =>l.CustomerReference.Substring(0,1).ToUpper()==l.CustomerReference.Substring(0,1))
.ThenBy (l =>l.CustomerReference )
EDIT: I was asked for the SQL too so this is what LINQPad does
-- Region Parameters
DECLARE #p0 Int SET #p0 = 0
DECLARE #p1 Int SET #p1 = 1
DECLARE #p2 Int SET #p2 = 0
DECLARE #p3 Int SET #p3 = 1
DECLARE #p4 Int SET #p4 = 0
DECLARE #p5 Int SET #p5 = 1
-- EndRegion
SELECT [T0].CustomerReference FROM [dbo].[test] AS [t0]
ORDER BY LOWER(SUBSTRING([t0].[CustomerReference], #p0 + 1, #p1)),
(CASE
WHEN UPPER(SUBSTRING([t0].[CustomerReference], #p2 + 1, #p3)) = SUBSTRING([t0].[CustomerReference], #p4 + 1, #p5) THEN 1
WHEN NOT (UPPER(SUBSTRING([t0].[CustomerReference], #p2 + 1, #p3)) = SUBSTRING([t0].[CustomerReference], #p4 + 1, #p5)) THEN 0
ELSE NULL
END) DESC, [t0].[CustomerReference]
In most implementations, lower-case comes first (not least, that is how code-points are arranged ordinally). You won't be able to get SQL server to change that, so the next best thing is to bring it back unsorted, and write a custom comparer. Note that the inbuilt .NET comparers will also treat lower-case as either first or equal (compared to their upper-case equivalent), depending on the comparer.
However! Unless you limit yourself to very simple examples (ASCII etc), ordering "alike" characters is a very non-trivial exercise. Even if we ignore the Turkish I / İ / ı / i, accented characters are going to cause you problems).

Categories

Resources