检查数据库特定列中是否存在值 [英] check if value exists in database particular column

查看:59
本文介绍了检查数据库特定列中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在Windows窗体中有一个文本框,其名称为txtbx,数据库表名称为table1,其中3列和1列名称为column1. '找到',否则'未找到'.请举一个例子

从答案中移出:

谢谢你的答复,我对下面的代码感到困惑

ReadData r = new ReadData();
if (r.FindString("Shahingg"))//? (my question is below)
  MessageBox.Show("I Found it!");
else 
  MessageBox.Show("I can''t Find it!");




?是因为我使用文本框,所以如何包含文本框值而不是"Shahingg".

解决方案



您已经在这里找到了一些解决方案:
如何检查数据库中是否存在值 [ ^ ]
您是否尝试过其中一些?如果是,请向我们显示您的代码!如果您不告诉我们您面临的问题,我们将无济于事.

更新问题后进行

解决方法如下:

 ReadData r =  ReadData();
如果(r.FindString(textBox1.Text))
  MessageBox.Show(" );
其他
  MessageBox.Show(" ); 


ks ravi.

您需要先整理几件事.

您需要:
a)连接
b)查询数据库的方法
c)封装和评估结果的方法

首先,您需要使正在开发的应用程序了解数据库.配置用连接字符串表示,通常通过VS工具为您构造和存储.

做您要寻找的最简单的方法可能是使用LINQ to SQL.这是有据可查的.这些步骤遵循以下步骤:
1)使用服务器窗口向有问题的数据库添加数据连接
2)将LINQ to SQL文件添加到数据库中
3)将表从服务器资源管理器拖到LINQ to SQL设计器上
4)编译您的应用程序

此时,您的应用程序已经完全了解数据库,并且连接位已构建并可以使用.配置存储在您的app.config文件中.设计器的结果是一个DataContext及其相关类.

现在,在您的代码中,您需要执行以下操作:
1)创建datacontext的实例
2)如果您的表包含数据,则询问"

它可能看起来像这样:

DesignerNameDataContext dc = new DesignerNameDataContext();
var matchingRecord = dc.table1.Where(c => c.column1 == txtbx.Text).FirstOrDefault();
if(matchingRecord == null)
{
  // there was no match
}



我在这里给了你很多工具和词汇. Google搜索将使您走得更远.如果您有任何特定问题或需要有关特定概念的帮助,请发布另一个问题!

希望这会有所帮助.

干杯.


Hi all,


I''ve a text box in windows form and its name is txtbx and database table name is table1 in which 3 columns and 1 column name is column1.My question is how to compare textbox value with column1 existed value,if same should say ''Found'' else ''Not Found''. please reply with an example

MOVED FROM ANSWER:

hi thank u for reply, i got confused at below code

ReadData r = new ReadData();
if (r.FindString("Shahingg"))//? (my question is below)
  MessageBox.Show("I Found it!");
else 
  MessageBox.Show("I can''t Find it!");




? is since i using textbox,how to include textbox value instead "Shahingg".

解决方案

Hi,

you already got some solutions here:
How to check if value exists in database, [^]
Have you tried some of those? If yes, show us your code! If you don''t tell us what problems you face we can''t help you any further.

Edited after updated question:

Here is the solution:

ReadData r = new ReadData();
if (r.FindString(textBox1.Text))
  MessageBox.Show("I Found it!");
else
  MessageBox.Show("I can't Find it!");


Hi ks ravi.

You will need to get a couple of things in order.

You need:
a) a connection
b) a way to query the database
c) a way to encapsulate and evaluate the results

First, you need to make the application you''re developing aware of the database. The configuration is expressed as a connection string, which is often constructed and stored for you through the VS tools.

The easiest way to do what you''re looking for might be to use LINQ to SQL. This is very well documented. The steps are along the lines of:
1) Use the server window to add a data connection to the database in question
2) Add a LINQ to SQL file to the database
3) Drag a table from the server explorer onto the LINQ to SQL designer
4) Compile your application

At this point, your application is fully aware of the database and the connection bits are constructed and ready to use. The configuration is stored in your app.config file. The result of the designer is a DataContext and its related classes.

Now, in your code, you''re going to need to do the following:
1) Create an instance of the datacontext
2) "Ask" if your table contains the data

It might look something like this:

DesignerNameDataContext dc = new DesignerNameDataContext();
var matchingRecord = dc.table1.Where(c => c.column1 == txtbx.Text).FirstOrDefault();
if(matchingRecord == null)
{
  // there was no match
}



I''ve given you a bunch of tools and vocabulary here. A Google search will get you further along the way. If you have any specific questions or need help with a particular concept, please post another question!

Hope this helps.

Cheers.


这篇关于检查数据库特定列中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆