如何检查记录存在 [英] how to check record exists
本文介绍了如何检查记录存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在插入记录之前检查记录存在然后插入并在其他情况下更新数据库中的记录。
for that怎么样我在asp.net中使用c#。
Before inserting the record check record exists then insert and in else case update the record in database.
for that how can i do in asp.net using c#.
推荐答案
1。如果你不熟悉C#和sql,那么你应该通过使用参数化的SQL [ ^ ]首先。
2.完成第1点后,调整以下代码片段进行检查如果数据库中存在某些记录。
1. If you are not familiar with C# and sql, then you should go through Using Parameterized SQL[^] first.
2. Once you have completed Point 1, adapt the following code snippet to check if certain record exists in the database.
string selectSQL = "select count(*) from tablename where somefieldname = @somevalue";
SqlCommand cmd = new SqlCommand(selectSQL);
cmd.Parameters.AddWithValue("@somevalue", TextBox1.Text.Trim());
Int32 rowCount = (Int32) cmd.ExecuteScalar();
3.点3之后,
3. After Point 3,
if (rowCount == 0){
// execute sql insert, adapt example in point 1
}
else
{
// execute sql update, adapt example in point 1
}
中调整示例
课程结束。
End of lesson.
如果是SQL Server,那么请寻求MERGE语句。
If SQL Server, then seek thee the MERGE statement.
这篇关于如何检查记录存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文