Poprawienie/Uzupełnienie kodu SQL - Tabela po dll-ce

0

Witam, mam mały problem z kodem. Mianowicie, mam za zadanie utworzenie kodu który z wygenerowanej w Visual Studio DLL-ki (na bazie kodu w C#), pobierze co ma robić. Następnie wyniki wypisze. I wszytko fajnie wypisuje. Ale chciałbym, żeby te wyniki wypisywały się w tabeli, a nie wyrzucały jeden po drugim.

Ktoś byłby w stanie naprostować ten kod?

Kod c#:

using System;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text;
using System.Globalization;

[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined, MaxByteSize=8000,
     IsByteOrdered = true, ValidationMethodName = "ValidatePesel")]

public struct Pesel : INullable, IBinarySerialize
{
    private bool is_Null;
    private string _s;
    public bool IsNull
    {
        get
        {
            return (is_Null);
        }
    }

    public static Pesel Null
    {
        get
        {
            Pesel p = new Pesel();
            p.is_Null = true;
            return p;
        }
    }

    // Use StringBuilder to provide string representation of UDT.
    public override string ToString()
    {
        if (this.IsNull)
            return "NULL";
        else
        {
            //Pesel p = new Pesel();
            return s.ToString();
        }
    }

    [SqlMethod(OnNullCall = false)]
    public static Pesel Parse(SqlString ss)
    {
        if (ss.IsNull)
        {
            return Null;
        }
       
        Pesel p = new Pesel();
        p.s = (string)ss;
        return p;
    }

    public string s
    {
        get
        {
            return this._s;
        }
        set
        {
            string temp = _s;
            _s= value;
            if (!ValidatePesel())
            {
                _s = temp;
                throw new ArgumentException("Invalid X coordinate value.");
            }
        }
    }

    public void Read(BinaryReader r)
    {
        _s = r.ReadString();
    }

    public void Write(BinaryWriter w)
    {
        string ss = _s.ToString();
        w.Write(ss);
    }

    public bool ValidatePesel()
    {
        int[] weights = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
        bool result = false;
        if (_s.Length == 11)
        {
            int controlSum = CalculateControlSum(_s.ToString(), weights);
            int controlNum = controlSum % 10;
            controlNum = 10 - controlNum;
            if (controlNum == 10)
            {
                controlNum = 0;
            }
            int lastDigit = int.Parse(_s[_s.Length - 1].ToString());
            result = controlNum == lastDigit;
        }
        return result;
    }

    public static int CalculateControlSum(string input, int[] weights, int offset = 0)
    {
        int controlSum = 0;
        for (int i = 0; i < input.Length - 1; i++)
        {
            controlSum += weights[i + offset] * int.Parse(input[i].ToString());
        }
        return controlSum;
    }
	
	public string BirthDate()
	{
		if(ValidatePesel())
		{
            DateTime today = DateTime.Today;
            DateTime birthday = new DateTime(today.Year, Int32.Parse(_s.Substring(2,2)),Int32.Parse(_s.Substring(4,2)));
            DateTime next = new DateTime(today.Year,birthday.Month,birthday.Day);

            if (next < today)
            next = next.AddYears(1);

            int numDays = (next - today).Days;
            string birth = new SqlString("Dzien: " + _s.Substring(4,2) + " Miesiąc: " + _s.Substring(2,2) + " Rok: " + _s.Substring(0,2) + " Dni_do_urodzin: " + numDays).ToString();
			return birth;
		}
		return null;
		
	}
	
}

Kod SQL:

drop type Pesel
go
drop assembly pesel 
go 
create assembly pesel 
authorization dbo 
from 'C:\Users\Logan\Desktop\Projekt\Projekt.dll' 
with permission_set = safe 
go 
create type dbo.Pesel 
external name Pesel.Pesel 
go 
declare @a Pesel 
select * from dbo.dane 
declare @b int 
set @b = 0 
while @b < (select max(P_Id) from dane) 
begin 
set @a = (select numer_pesel as pesel from dane where P_Id = 1+@b) 
set @b += 1 
if (select numer_pesel from dane) = null 
break 
create table 
select @a.ValidatePesel() 
select @a.BirthDate() 
select @a.GetGender() 
end

Pomocy :(

0

Kilka uwag, kod UDT nie jest pełny brak GetGender. Źle sprawdzasz datę urodzenia wnowych peselach do miesiąca dodaje się 20.
Do poruszania się po rekordach używaj kursorów, u ciebie problemem będzie brak ciągłości p_id.

można to ładnie zrobić bez kursora. Jeżeli pole numer_pesel nie jest typu pesel to tak:

DECLARE @P TABLE
(
  pesel Pesel
)
INSERT INTO @P
Select numer_pesel from dane where numer_pesel not is null

select
    pesel.ValidatePesel() 
    ,pesel.BirthDate() 
    ,pesel.GetGender()
from
    @p

1 użytkowników online, w tym zalogowanych: 0, gości: 1