且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查输入的值是否存在于数据库中

更新时间:2023-11-25 23:53:58

  SELECT  COUNT(*) FROM  MyTable  WHERE  AccountNumber = 1234 

将返回匹配记录的数量。

  SELECT  *  FROM  MyTable  WHERE  AccountNumber = 1234 

将返回所有匹配的记录。



您可以使用后者并检查是否有任何记录返回 - 但C#代码中的确切方式取决于它当前的工作方式 - 我们无法看到它。

但这可能会有所帮助:在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=1234

Will return the number of matching records.

SELECT * FROM MyTable WHERE AccountNumber=1234

Will 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. . :)