C#を使用してMySQLデータベースにレコードが存在するかどうかを確認するにはどうすればよいですか?挿入前にMySQLデータベースにレコードが存在するか確認する
レコードが存在する場合、フォームにはレコードが既に存在することを示すメッセージボックスが表示され、値はデータベースに保存されません。それ以外の場合は、新しいレコードがデータベースに保存されます。
C#の構文は何ですか? 私はIDEとしてVisual Studioを使用しています。
private void btnSave_Click(object sender, EventArgs e)
{
string constring = "datasource=localhost;port=3306;username=root;password=root";
string QueryAdd = "insert into assetmanagement.assets (`AssetId`,`AssetType`,`AssetDescription`,`SerialNumber`,`Barcode`,`Quantity`,`Manufacturer`,`Model`,`Category`,`Condition`,`Location`,`Department`,`DateAcquired`,`InService`,`Supplier`,`Notes`,`AddedBy`,`DateAdded`) values ('" + this.txtAssetId.Text + "','" + this.txtAssetType.Text + "','" + this.txtAssetDesc.Text + "','" + this.txtSerial.Text + "','" + this.txtBarcode.Text + "','" + this.txtQuantity.Text + "','" + this.txtManufacturer.Text + "','" + this.txtModel.Text + "','" + this.txtCategory.Text + "','" + this.txtCondition.Text + "','" + this.txtLocation.Text + "','" + this.txtDepartment.Text + "','" + this.txtDateAcquired.Text + "','" + this.txtInService.Text + "','" + this.txtSupplier.Text + "','" + this.txtNotes.Text + "','" + this.txtAddedBy.Text + "','" + this.txtDateAdded.Text + "') ;";
MySqlConnection connDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(QueryAdd, connDataBase);
MySqlDataReader myReader;
try
{
connDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Saved");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
ON DUPLICATEステートメントを使用します – Steve
標準のMERGEステートメントが存在するSql Serverデータベースでも、複製の回答が悪いですが、この質問はMySqlに関するものであることを忘れましょう。 – Steve
「Visual Basicを私のIDEとして」とはどういう意味ですか? Visual Basic Express([Visual Basic 2005 Express Edition](https://en.wikipedia.org/wiki/Microsoft_Visual_Studio_Express#Visual_Basic_Express))のように)? –