gpt4 book ai didi

c# - Window Form C# MS Access 检查是否已经具有该数据的值

转载 作者:行者123 更新时间:2023-11-30 23:00:27 26 4
gpt4 key购买 nike

如何检查数据是否已存在于 MS Access 中?例如,如果名字是 blouse 是 Bijan,那么当我要发布时,消息框将显示在告诉我们 Bijan 已经在表中。

//Button Post
private void buttonPost_Click(object sender, EventArgs e)
{
//Connect to database
connection.Open();
command.Connection = connection;
//Password need to use [] because it is reserved keyword
command.CommandText = "INSERT INTO BlouseTable (blouseName,blouseBrand,blouseColor,blouseSize,blouseQuantity) "
+ "VALUES ('" + blouse.BlouseName + "','" + blouse.BlouseBrand
+ "','" + blouse.BlouseColor + "','" + blouse.BlouseSize
+ "','" + blouse.BlouseQuantity + "')";

command.ExecuteNonQuery();
//Display the message result
MessageBox.Show("Done post new Blouse!");
//Close the connection after done post new blouse
connection.Close();
}

这是我窗口表单中的按钮帖子。所以,当我按下它时,它应该是 MS Access 中的数据库表。

最佳答案

试试这个。

OleDbCommand commandBlouseName = new OleDbCommand();
commandBlouseName.Connection = connection;
commandBlouseName.CommandText = "SELECT * FROM BlouseTable WHERE blouseName='" + blouse.BlouseName + "'";
OleDbDataReader readerBlouseName = commandBlouseName.ExecuteReader();
int count = 0;
while (readerBlouseName.Read())
{
count++;
}
if (count == 1)
{
//Has 1 same name registered
}
if (count < 1)
{
//Create new one
//There is no Blouse Name registered with same name
}
if (count > 1)
{
//More than 1 that Blouse Name registered
}

关于c# - Window Form C# MS Access 检查是否已经具有该数据的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51554746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com