Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.IO;
using Microsoft.SqlServer.Server;

namespace test
{
public class Class1 : IBinarySerialize
{
[STAThread]
static void Main(string[] args)
{
string fileName = "info.dat";
Class1 temp = new Class1();

FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);

temp.Write(w);

w.Close();
fs.Close();

fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);

temp.Read(r);

Console.WriteLine("String value: " + temp.StringValue);
Console.WriteLine("Double value: " + temp.DoubleValue);

r.Close();
fs.Close();
}

public string StringValue;
public double DoubleValue;

//<Snippet1>
// The binary layout is as follows:
// Bytes 0 - 19: string text, padded to the right with null characters
// Bytes 20+: Double value

// using Microsoft.SqlServer.Server;
public void Read(System.IO.BinaryReader r)
{

int maxStringSize = 20;
char[] chars;
int stringEnd;
string stringValue;
double doubleValue;

// Read the characters from the binary stream.
chars = r.ReadChars(maxStringSize);

// Find the start of the null character padding.
stringEnd = Array.IndexOf(chars, '\0');

if (stringEnd == 0)
{
stringValue = null;
return;
}

// Build the string from the array of characters.
stringValue = new String(chars, 0, stringEnd);

// Read the double value from the binary stream.
doubleValue = r.ReadDouble();

// Set the object's properties equal to the values.
this.StringValue = stringValue;
this.DoubleValue = doubleValue;
}
//</Snippet1>

//<Snippet2>
// The binary layout is as follows:
// Bytes 0 - 19: string text, padded to the right with null characters
// Bytes 20+: Double value

// using Microsoft.SqlServer.Server;
public void Write(System.IO.BinaryWriter w)
{
int maxStringSize = 20;
string stringValue = "The value of PI: ";
string paddedString;
double value = 3.14159;

// Pad the string from the right with null characters.
paddedString = stringValue.PadRight(maxStringSize, '\0');

// Write the string value one byte at a time.
for (int i = 0; i < paddedString.Length; i++)
{
w.Write(paddedString[i]);
}

// Write the double value.
w.Write(value);
}
//</Snippet2>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.IO;
using System.Collections;
using Microsoft.SqlServer.Server;

public class Class1
{

//<Snippet1>
[SqlFunctionAttribute(FillRowMethodName = "FillFileRow")]
public static IEnumerable GetFileDetails(string directoryPath)
{
try
{
DirectoryInfo di = new DirectoryInfo(directoryPath);
return di.GetFiles();
}
catch (DirectoryNotFoundException dnf)
{
return new string[1] { dnf.ToString() };
}
}
//</Snippet1>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//<Snippet1>
using System;
using System.IO;
using Microsoft.SqlServer.Server;

[Serializable]
[SqlUserDefinedAggregate(Microsoft.SqlServer.Server.Format.UserDefined,
IsInvariantToNulls = true,
IsInvariantToDuplicates = false,
IsInvariantToOrder = false,
MaxByteSize = 8000)
]
public class Concatenate : Microsoft.SqlServer.Server.IBinarySerialize
{
public void Read(BinaryReader r)
{
}

public void Write(BinaryWriter w)
{
}
}
//</Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text;

// <Snippet1>
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.Native,
IsByteOrdered=true,
Name="Point",ValidationMethodName = "ValidatePoint")]
public struct Point : INullable
{
//</Snippet1>
private bool is_Null;
private int _x;
private int _y;

public bool IsNull
{
get
{
return (is_Null);
}
}

public static Point Null
{
get
{
Point pt = new Point();
pt.is_Null = true;
return pt;
}
}

// Use StringBuilder to provide string representation of UDT.
public override string ToString()
{
// Since InvokeIfReceiverIsNull defaults to 'true'
// this test is unnecessary if Point is only being called
// from SQL.
if (this.IsNull)
{
return "NULL";
}
else
{
StringBuilder builder = new StringBuilder();
builder.Append(_x);
builder.Append(",");
builder.Append(_y);
return builder.ToString();
}
}

[SqlMethod(OnNullCall = false)]
public static Point Parse(SqlString s)
{
// With OnNullCall=false, this check is unnecessary if
// Point only called from SQL.
if (s.IsNull)
return Null;

// Parse input string to separate out points.
Point pt = new Point();
string[] xy = s.Value.Split(",".ToCharArray());
pt.X = int.Parse(xy[0]);
pt.Y = int.Parse(xy[1]);

// Call ValidatePoint to enforce validation
// for string conversions.
if (!pt.ValidatePoint())
throw new ArgumentException("Invalid XY coordinate values.");
return pt;
}

// X and Y coordinates exposed as properties.
public int X
{
get
{
return this._x;
}
// Call ValidatePoint to ensure valid range of Point values.
set
{
int temp = _x;
_x = value;
if (!ValidatePoint())
{
_x = temp;
throw new ArgumentException("Invalid X coordinate value.");
}
}
}

public int Y
{
get
{
return this._y;
}
set
{
int temp = _y;
_y = value;
if (!ValidatePoint())
{
_y = temp;
throw new ArgumentException("Invalid Y coordinate value.");
}
}
}

// Validation method to enforce valid X and Y values.
private bool ValidatePoint()
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections;
//-----------------------------------------------------------------------------
//<Snippet4>
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
public const double SALES_TAX = .086;

[SqlFunction()]
public static SqlDouble addTax(SqlDouble originalAmount)
{
SqlDouble taxAmount = originalAmount * SALES_TAX;

return originalAmount + taxAmount;
}
}
//</Snippet4>

//-----------------------------------------------------------------------------
//<Snippet10>
public partial class UserDefinedFunctions
{
[SqlFunction(Name="sp_scalarFunc")]
public static SqlString SampleScalarFunction(SqlString s)
{
//...
return "";
}
}
//</Snippet10>

//-----------------------------------------------------------------------------
//<Snippet11>
public partial class UserDefinedFunctions
{
[SqlFunction(Name="sp_tableFunc", TableDefinition="letter nchar(1)")]
public static IEnumerable SampleTableFunction(SqlString s)
{
//...
return new ArrayList(new char[3] {'a', 'b', 'c'});
}
}
//</Snippet11>
Loading