Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
Created February 19, 2013 15:13
Show Gist options
  • Select an option

  • Save ichiroku11/4986739 to your computer and use it in GitHub Desktop.

Select an option

Save ichiroku11/4986739 to your computer and use it in GitHub Desktop.
SQL Serverデータ型からCLRデータ型へのマッピングを試す http://msdn.microsoft.com/ja-jp/library/ms131092.aspx
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace ConsoleApp {
class Program {
private static SqlConnection Connect() {
var builder = new SqlConnectionStringBuilder {
DataSource = ".",
InitialCatalog = "tempdb",
IntegratedSecurity = true,
};
var connection = new SqlConnection(builder.ConnectionString);
connection.Open();
return connection;
}
public static void Main(string[] args) {
var query = @"
select
cast(1.1 as decimal) as [decimal],
cast(0.1 as float) as [float],
cast(1 as int) as [int],
cast(1 as bigint) as [bigint],
cast(1 as smallint) as [smallint],
cast(1 as tinyint) as [tinyint],
cast('2013-02-19' as date) as [date],
cast('2013-02-19 20:00:00.1234567' as datetime2) as [datetime2],
cast('20:00:00.1234567' as time) as [time],
cast('a' as nchar) as [nchar],
cast('abc' as nvarchar) as [nvarchar],
cast('a' as binary) as [binary],
cast('abc' as varbinary) as [varbinary],
cast(1 as bit ) as [bit],
newid() as [uniqueidentifier];";
using(var connection = Connect())
using(var command = new SqlCommand { CommandText = query, Connection = connection })
using(var reader = command.ExecuteReader()) {
Console.WriteLine("{0,-18}{1}", "SQL Server", "CLR");
while(reader.Read()) {
for(var index = 0; index < reader.FieldCount; index++) {
Console.WriteLine("{0,-18}{1}", reader.GetName(index), reader.GetValue(index).GetType());
}
}
}
/*
実行結果
SQL Server CLR
decimal System.Decimal
float System.Double
int System.Int32
bigint System.Int64
smallint System.Int16
tinyint System.Byte
date System.DateTime
datetime2 System.DateTime
time System.TimeSpan
nchar System.String
nvarchar System.String
binary System.Byte[]
varbinary System.Byte[]
bit System.Boolean
uniqueidentifier System.Guid
*/
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment