Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created July 27, 2014 12:27
Show Gist options
  • Save KentaYamada/ea64b701acb3620c3f66 to your computer and use it in GitHub Desktop.
Save KentaYamada/ea64b701acb3620c3f66 to your computer and use it in GitHub Desktop.
ストアドを使ってSelect文の結果を取得するサンプル
using System;
using System.Data;
using System.Data.SqlClient;
namespace StoredProcedureSelect
{
class Program
{
static void Main(string[] args)
{
using (var conn = new SqlConnection(@"Data Source=(local)\SQLEXPRESS; Initial Catalog=sampleDB; Integrated Security=SSPI;"))
using (var comm = new SqlCommand("SearchEmployee", conn))
{
comm.CommandType = CommandType.StoredProcedure;
var empNoFrom = new SqlParameter("@empnoFrom", SqlDbType.Int);
empNoFrom.Value = 1;
comm.Parameters.Add(empNoFrom);
var empNoTo = new SqlParameter("@empnoTo", SqlDbType.Int);
empNoTo.Value = 9;
comm.Parameters.Add(empNoTo);
var table = new DataTable();
using (var adpt = new SqlDataAdapter(comm))
{
try
{
adpt.Fill(table);
}
finally
{
comm.Parameters.Clear();
conn.Close();
}
}
if (0 < table.Rows.Count)
{
foreach (DataRow row in table.Rows)
{
Console.WriteLine(row[0]);
}
}
Console.ReadKey();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment