LoginSignup
0
0

More than 1 year has passed since last update.

.Net 6: C# で MariaDB の Delete

Last updated at Posted at 2022-08-09

プロジェクトの作成

mkdir Delete01
cd Delete01
dotnet new console
dotnet add package MySql.Data --version 8.0.30

フォルダー構造

$ tree -L 1
.
├── Delete01.csproj
├── Program.cs
└── obj
Program.cs
// -------------------------------------------------------------------
/*
	Program.cs

					Aug/09/2022


*/
// -------------------------------------------------------------------
using	System;
using	System.Text;
using	MySql.Data.MySqlClient;

// -------------------------------------------------------------------
class maria_delete
{
// -------------------------------------------------------------------
static void Main (string[] args)
{
	Console.WriteLine ("*** 開始 ***");

	string	id_in = args[0];

	string server = "localhost";
	string str_db = "city";
	string user = "scott";
	string password = "tiger123";

	string str_connect = "Server=" + server + 
		";User Id=" + user + ";Password=" + password +
		";Database=" + str_db + ";";
 
	MySqlConnection connection = new MySqlConnection (str_connect);
	connection.Open ();

	mysql_delete_proc (connection,id_in);

	connection.Close ();

	Console.WriteLine ("*** 終了 ***");
}

// -------------------------------------------------------------------
static void mysql_delete_proc
	(MySqlConnection connection,string id_a)
{
	StringBuilder sb_sql = new StringBuilder
			("DELETE from cities WHERE ID = '" + id_a + "'");

	string str_sql = sb_sql.ToString ();

	Console.WriteLine (str_sql);

	MySqlCommand command = new MySqlCommand (str_sql,connection);
	int rowsAffected = command.ExecuteNonQuery ();
	Console.WriteLine ("rowsAffected = " + rowsAffected);
}

// -------------------------------------------------------------------
}

// -------------------------------------------------------------------

実行結果

$ dotnet run t3322
*** 開始 ***
DELETE from cities WHERE ID = 't3322'
rowsAffected = 1
*** 終了 ***
0
0
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
0
0