<分区>
如何使用 C# 检查 MySQL 数据库中是否存在记录?
如果记录存在,表单应显示一个消息框,说明该记录已存在并且不将值保存到数据库中。否则,将新记录存入数据库。
它的 C# 语法是什么? 我使用 Visual Studio 作为我的 IDE。
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);
}
}