2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VisualStudio C# から SQLServer への接続

Last updated at Posted at 2019-07-25

概要

地味に接続で時間喰ったので掲載

環境

VisualStudio 2017 (C#)
SQLServer 2012

コード

DBAccess.cs

void Test02()
{
 SqlConnection connection = new SqlConnection();
 SqlCommand command = new SqlCommand();
 var dt = new DataTable();


 try
 {
  command.Connection = connection;
  connection.ConnectionString = @"Data Source=[server];Persist Security Info=True;Initial 
  Catalog=[dataBase];User ID=[id];Password=[pass];";
 
  // データベースの接続開始
  connection.Open();

  // SELECT文を設定します。
  command.CommandText = "SELECT * FROM [tableName] " ;

  // SQLの実行
  var adapter = new SqlDataAdapter(command);
  adapter.Fill(dt);

  }
  catch (Exception exception)
  {
  Console.WriteLine(exception.Message);
  throw;
  }
  finally
  {
  // データベースの接続終了
  connection.Close();
  }
 } 
}

かかったとこ

テンプレは見つかるけど結局コネクトのとこで躓いてた

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?