如何检查输入的值是否存在于数据库中 [英] How to check entered value is present in database or not
问题描述
大家好
我在文本框中输入帐号。
所以我想要检查帐号是否存在于表中然后它将获取该号码的所有记录,否则它将给出错误。
请告诉我该如何做..
SELECT COUNT(*) FROM MyTable WHERE AccountNumber = 1234将返回匹配记录的数量。
SELECT * FROM MyTable WHERE AccountNumber = 1234将返回所有匹配的记录。
您可以使用后者并检查是否有任何记录返回 - 但C#代码中的确切方式取决于它当前的工作方式 - 我们无法看到它。
但这可能会有所帮助:<小时ef =http://www.codeproject.com/Articles/30989/Using-PostgreSQL-in-your-C-NET-application-An-intr>在C#(.NET)应用程序中使用PostgreSQL(简介) [ ^ ]
尝试使用LinQ to Sql方法
var container = NameOfYourEntity.YourTable.Where(x => x.AccountNumber == Int.Parse(TextBox.Text))。FirstOrDefault();
if (container!= null )
{
// 从容器变量中获取数据
}
else
{
// 如果有的话找不到记录
}
// NameOfYourEntity是edmx文件的实例使用实体框架
你也可以读取此内容以获取另一个LinQ To SQL示例
希望它会有所帮助。 。 :)
Hi all
I am entering account number in text box.
so i want to check of the account number is present in table then it will fetch all the records of that number else it will give error.
please tell how to do that..
SELECT COUNT(*) FROM MyTable WHERE AccountNumber=1234Will return the number of matching records.
SELECT * FROM MyTable WHERE AccountNumber=1234Will return all matching records.
You can just use the latter one and check if any records are returned - but exactly how in your C# code depends on how it currently works - and we can't see that.
But this might help: Using PostgreSQL in your C# (.NET) application (An introduction)[^]
try this using LinQ to Sql approach
var container=NameOfYourEntity.YourTable.Where(x=>x.AccountNumber==Int.Parse(TextBox.Text)).FirstOrDefault(); if(container!=null) { //get the data from container variable } else { //do something if no record found } //NameOfYourEntity is the instance of your edmx file using Entity Framework
you can also read this for another sample of LinQ To SQL
hope it will help. . :)
这篇关于如何检查输入的值是否存在于数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!