Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
ichiroku11 / Program.cs
Created May 17, 2012 23:38
Hello gist
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp {
class Program {
static void Main( string[] args ) {
Console.WriteLine( "Hello gist!!" );
}
@ichiroku11
ichiroku11 / Program.cs
Created May 23, 2012 03:04
System.Net.Mail.SmtpClientのサンプル
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace ConsoleApp {
static class Program {
static void Main( string[] args ) {
@ichiroku11
ichiroku11 / Program.cs
Created May 23, 2012 03:06
System.Diagnostics.EventLogのサンプル
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApp {
static class Program {
static void Main( string[] args ) {
var source = "ConsoleApp"; // イベントソース名
@ichiroku11
ichiroku11 / gist:2848188
Created June 1, 2012 02:31
テーブル名、カラム名、データ型を表示するクエリ
select
sys.tables.name,
sys.columns.name,
sys.types.name
from sys.tables
inner join sys.columns
on sys.tables.object_id = sys.columns.object_id
inner join sys.types
on sys.columns.user_type_id = sys.types.user_type_id
order by
@ichiroku11
ichiroku11 / gist:2886211
Created June 7, 2012 02:37
テーブル名、カラム名からデフォルト制約名を取得するクエリ
declare @table sysname = 'table_name';
declare @column sysname = 'column_name';
select
sys.default_constraints.name, *
from sys.tables
inner join sys.columns
on sys.tables.object_id = sys.columns.object_id
inner join sys.default_constraints
on sys.tables.object_id = sys.default_constraints.parent_object_id and
@ichiroku11
ichiroku11 / gist:2892705
Created June 8, 2012 00:45
カーソルの使い方
-- cursorを宣言
declare cur cursor
for
select ...
-- cursorを開く
open cur;
-- 1行ごとにデータを取り出す
fetch next from cur into @v1, @v2, ...;
@ichiroku11
ichiroku11 / gist:2964883
Created June 21, 2012 09:44
主キー制約、ユニーク制約、外部キー制約の一覧を取得するクエリ
with cte_columns as(
select
sys.tables.object_id,
sys.columns.column_id,
sys.tables.name as table_name,
sys.columns.name as column_name
from sys.tables
inner join sys.columns
on sys.tables.object_id = sys.columns.object_id
where
@ichiroku11
ichiroku11 / gist:2987578
Created June 25, 2012 09:14
全テーブルのレコード数を取得するクエリ
-- 結果を格納するテーブル変数
declare @result table(
Name sysname not null,
Rows int not null
);
-- テーブル名を取得するカーソル
declare cur cursor
for
select
@ichiroku11
ichiroku11 / gist:3858020
Created October 9, 2012 11:11
HashSet<T>のメソッドの使い方
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp {
class Program {
static void Main( string[] args ) {
var items = new HashSet<int> { 0, 1, 2, 3, 4, };
// otherが同じ要素を含んでいるかどうか
@ichiroku11
ichiroku11 / gist:3992735
Created November 1, 2012 09:37
指定DBを指定フォルダにバックアップするクエリ
/*
指定DBを指定フォルダにバックアップ
バックアップファイル名には日付の文字列が含まれる
*/
declare @dbName sysname = N'DB名';
declare @folder nvarchar( max ) = N'バックアップフォルダ(\で終わる必要あり)';
declare @date datetime = getdate();
declare @ymd char( 8 ) = convert( char( 8 ), @date, 112 ); -- yymmdd
declare @hms char( 6 ) = replace( convert( char( 8 ), @date, 108 ), ':', '' ); -- hh:mm:ss -> hhmmss