적용 대상:SQL Server
클라이언트에서 UDT(사용자 정의 형식)를 만들려면 SQL Server 데이터베이스에서 UDT로 등록된 어셈블리를 클라이언트 애플리케이션에서 사용할 수 있어야 합니다. UDT 어셈블리는 애플리케이션과 동일한 디렉터리 또는 GAC(전역 어셈블리 캐시)에 배치할 수 있습니다. 사용자의 프로젝트에서 어셈블리에 대한 참조를 설정할 수도 있습니다.
ADO.NET UDT를 사용하기 위한 요구 사항
SQL Server에 로드된 어셈블리와 클라이언트의 어셈블리는 클라이언트에서 UDT를 만들려면 호환되어야 합니다.
Native serialization 형식으로 정의된 UDT의 경우 어셈블리는 구조적으로 호환되어야 합니다.
UserDefined 형식으로 정의된 어셈블리의 경우 클라이언트에서 어셈블리를 사용할 수 있어야 합니다.
테이블의 UDT 열에서 원시 데이터를 검색하기 위해 클라이언트에서 UDT 어셈블리의 복사본이 필요하지 않습니다.
참고 항목
SqlClient UDT 버전이 일치하지 않거나 다른 문제가 발생할 경우 UDT를 로드하지 못할 수 있습니다. 이 경우 정기적인 문제 해결 메커니즘을 사용하여 호출 애플리케이션에서 UDT를 포함하는 어셈블리를 찾을 수 없는 이유를 확인합니다. 자세한 내용은 관리되는 디버깅 도우미사용하여 오류 진단
SqlDataReader를 사용하여 UDT에 액세스
클라이언트 코드에서 System.Data.SqlClient.SqlDataReader 사용하여 개체의 인스턴스로 노출되는 UDT 열을 포함하는 결과 집합을 검색할 수 있습니다.
예시
이 예제에서는 Main 메서드를 사용하여 새 SqlDataReader 개체를 만드는 방법을 보여줍니다. 다음 작업은 코드 예제 내에서 발생합니다.
Main 메서드는 새
SqlDataReader개체를 만들고 Point라는 UDT 열이 있는 Points 테이블에서 값을 검색합니다.PointUDT는 정수로 정의된 X 및 Y 좌표를 노출합니다.UDT는
Distance메서드와GetDistanceFromXY메서드를 정의합니다.샘플 코드는 UDT의 기능을 보여주기 위해 기본 키 및 UDT 열의 값을 검색합니다.
샘플 코드는
Point.Distance및Point.GetDistanceFromXY메서드를 호출합니다.결과는 콘솔 창에 표시됩니다.
참고 항목
애플리케이션에 이미 UDT 어셈블리에 대한 참조가 있어야 합니다.
-
C#
-
Visual Basic .NET
using System;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Microsoft.Samples.SqlServer
{
class ReadPoints
{
static void Main()
{
string connectionString = GetConnectionString();
using (SqlConnection cnn = new SqlConnection(connectionString))
{
cnn.Open();
SqlCommand cmd = new SqlCommand(
"SELECT ID, Pnt FROM dbo.Points", cnn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// Retrieve the value of the Primary Key column
int id = rdr.GetInt32(0);
// Retrieve the value of the UDT
Point pnt = (Point)rdr[1];
// You can also use GetSqlValue and GetValue
// Point pnt = (Point)rdr.GetSqlValue(1);
// Point pnt = (Point)rdr.GetValue(1);
Console.WriteLine(
"ID={0} Point={1} X={2} Y={3} DistanceFromXY={4} Distance={5}",
id, pnt, pnt.X, pnt.Y, pnt.DistanceFromXY(1, 9), pnt.Distance());
}
rdr.Close();
Console.WriteLine("done");
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=AdventureWorks2022"
+ "Integrated Security=SSPI";
}
}
}
}
UDT를 바이트로 바인딩
경우에 따라 UDT 열에서 원시 데이터를 검색할 수 있습니다. 형식을 로컬에서 사용할 수 없거나 UDT 인스턴스를 인스턴스화하지 않으려는 것일 수 있습니다.
SqlDataReader
GetBytes 메서드를 사용하여 원시 바이트를 바이트 배열로 읽을 수 있습니다. 이 메서드는 지정한 열 오프셋의 바이트 스트림을 지정한 버퍼 오프셋에서 시작하는 배열의 버퍼로 읽어 들입니다. 또 다른 옵션은 GetSqlBytes 또는 GetSqlBinary 메서드 중 하나를 사용하고 단일 작업에서 모든 내용을 읽는 것입니다. 두 경우 모두 UDT 개체는 인스턴스화되지 않으므로 클라이언트 어셈블리에서 UDT에 대한 참조를 설정할 필요가 없습니다.
예시
이 예제에서는 SqlDataReader사용하여 Point 데이터를 바이트 배열로 원시 바이트로 검색하는 방법을 보여줍니다. 이 코드는 System.Text.StringBuilder 사용하여 원시 바이트를 콘솔 창에 표시할 문자열 표현으로 변환합니다.
-
C#
-
Visual Basic .NET
using System;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
class GetRawBytes
{
static void Main()
{
string connectionString = GetConnectionString();
using (SqlConnection cnn = new SqlConnection(connectionString))
{
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT ID, Pnt FROM dbo.Points", cnn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// Retrieve the value of the Primary Key column
int id = rdr.GetInt32(0);
// Retrieve the raw bytes into a byte array
byte[] buffer = new byte[32];
long byteCount = rdr.GetBytes(1, 0, buffer, 0, 32);
// Format and print bytes
StringBuilder str = new StringBuilder();
str.AppendFormat("ID={0} Point=", id);
for (int i = 0; i < byteCount; i++)
str.AppendFormat("{0:x}", buffer[i]);
Console.WriteLine(str.ToString());
}
rdr.Close();
Console.WriteLine("done");
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=AdventureWorks2022"
+ "Integrated Security=SSPI";
}
}
}
GetSqlBytes를 사용하는 예제
이 예제에서는 GetSqlBytes 메서드를 사용하여 단일 작업에서 Point 데이터를 원시 바이트로 검색하는 방법을 보여줍니다. 이 코드는 StringBuilder 사용하여 원시 바이트를 콘솔 창에 표시할 문자열 표현으로 변환합니다.
-
C#
-
Visual Basic .NET
using System;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
class GetRawBytes
{
static void Main()
{
string connectionString = GetConnectionString();
using (SqlConnection cnn = new SqlConnection(connectionString))
{
cnn.Open();
SqlCommand cmd = new SqlCommand(
"SELECT ID, Pnt FROM dbo.Points", cnn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// Retrieve the value of the Primary Key column
int id = rdr.GetInt32(0);
// Use SqlBytes to retrieve raw bytes
SqlBytes sb = rdr.GetSqlBytes(1);
long byteCount = sb.Length;
// Format and print bytes
StringBuilder str = new StringBuilder();
str.AppendFormat("ID={0} Point=", id);
for (int i = 0; i < byteCount; i++)
str.AppendFormat("{0:x}", sb[i]);
Console.WriteLine(str.ToString());
}
rdr.Close();
Console.WriteLine("done");
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=AdventureWorks2022"
+ "Integrated Security=SSPI";
}
}
}
UDT 매개 변수 작업
UDT는 ADO.NET 코드에서 입력 및 출력 매개 변수로 사용할 수 있습니다.
쿼리 매개 변수에서 UDT 사용
UDT는 System.Data.SqlClient.SqlCommand 개체에 대한 SqlParameter 설정할 때 매개 변수 값으로 사용할 수 있습니다.
SqlParameter 개체의 SqlDbType.Udt 열거형은 Parameters 컬렉션에 Add 메서드를 호출할 때 매개 변수가 UDT임을 나타내는 데 사용됩니다.
SqlCommand 개체의 UdtTypeName 속성은 <database>.<schema_name>.<object_name> 구문을 사용하여 데이터베이스에서 UDT의 정규화된 이름을 지정하는 데 사용됩니다. 코드의 모호성을 방지하려면 정규화된 이름을 사용해야 합니다.
클라이언트 프로젝트에서 UDT 어셈블리의 로컬 복사본을 사용할 수 있어야 합니다.
예시
이 예제의 코드는 테이블의 UDT 열에 데이터를 삽입하는 SqlCommand 및 SqlParameter 개체를 만듭니다. 이 코드는 SqlDbType.Udt 열거형을 사용하여 데이터 형식을 지정하고 SqlParameter 개체의 UdtTypeName 속성을 사용하여 데이터베이스에서 UDT의 정규화된 이름을 지정합니다.
-
C#
-
Visual Basic .NET
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
class Class1
{
static void Main()
{
string ConnectionString = GetConnectionString();
using (SqlConnection cnn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText =
"INSERT INTO dbo.Points (Pnt) VALUES (@Point)";
cmd.CommandType = CommandType.Text;
SqlParameter param = new SqlParameter("@Point", SqlDbType.Udt); param.UdtTypeName = "TestPoint.dbo.Point"; param.Direction = ParameterDirection.Input; param.Value = new Point(5, 6); cmd.Parameters.Add(param);
cnn.Open();
cmd.ExecuteNonQuery();
Console.WriteLine("done");
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=AdventureWorks2022"
+ "Integrated Security=SSPI";
}
}
}
관련 콘텐츠
- ADO.NET 사용자 정의 형식 액세스