检查是否在数据库中找到了字段内容 [英] Check if field content been found in the database
本文介绍了检查是否在数据库中找到了字段内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将检查或我的标签的内容仅进行一次进入数据库。现在出了问题,因为我总是陷入ELSE。我的代码中有问题吗?
private void RUN_PCID()
{
这 .Cursor = Cursors.WaitCursor;
使用(connection = new SqlConnection(connectionstring))
使用(SqlDataAdapter adapter = new SqlDataAdapter( 从Toegang选择PCID,其中PCID =' + LB_UNIC.Text + ',connection))
{
DataTable dt = new DataTable();
尝试
{
adapter.Fill(dt);
if (dt.Rows [ 0 ]。ToString()== 1)
{
Progres_Start.Visible = 真跨度>;
TimerStart.Enabled = true ;
TimerStart.Interval = 150 ;
TimerStart.Start();
this .Cursor = Cursors.Default;
}
其他
{
此 .Cursor = Cursors.Default;
MessageBox.Show( Licentie是niet bekent。, ** | Melding - M200,MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
this .Hide();
}
}
catch (例外)
{
这 .Cursor = Cursors.Default;
MessageBox.Show( Server niet bereikt,controleer de internetverbinding。, ** |错误 - E110,MessageBoxButtons.OK,MessageBoxIcon.Hand);
this .Hide();
}
}
}
什么我试过了:
不同的SQL字符串像whit或者参数一样; command.Parameters.AddWithValue(@ PCID,%+ LB_UNIC.Text +%);
解决方案
首先,您要检查是否特定值是否在您的数据库中 - 所以不要使用DataAdapter,使用SELECT COUNT(*)...
作为查询并使用ExecuteScalar代替 - 它返回单个值,对于无匹配为零,对于匹配存在为非零。永远不要检索你不需要的数据...
其次,是什么让你认为DataRow.ToString调用会将所有单元格的计数作为字符串返回?它不会。它将始终返回相同的字符串:System.Data.DataRow。
因此更改为ExecuteScalar,它将开始工作...
I try to carry out a check or the contents of my label only state once into the database. Now something goes wrong, because I always fall in the ELSE. I stand something wrong in my code?
private void RUN_PCID()
{
this.Cursor = Cursors.WaitCursor;
using (connection = new SqlConnection(connectionstring))
using (SqlDataAdapter adapter = new SqlDataAdapter("Select PCID From Toegang Where PCID = '" + LB_UNIC.Text + "'", connection))
{
DataTable dt = new DataTable();
try
{
adapter.Fill(dt);
if (dt.Rows[0].ToString() == "1")
{
Progres_Start.Visible = true;
TimerStart.Enabled = true;
TimerStart.Interval = 150;
TimerStart.Start();
this.Cursor = Cursors.Default;
}
else
{
this.Cursor = Cursors.Default;
MessageBox.Show("Licentie is niet bekent.", "** | Melding - M200", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Hide();
}
}
catch (Exception)
{
this.Cursor = Cursors.Default;
MessageBox.Show("Server niet bereikt, controleer de internetverbinding.", "** | Error - E110", MessageBoxButtons.OK, MessageBoxIcon.Hand);
this.Hide();
}
}
}
What I have tried:
Different SQL strings whit like, or whit a parameters; command.Parameters.AddWithValue("@PCID", "%" + LB_UNIC.Text + "%");
解决方案
First off, you are trying to check if a particular value is in your DB or not - so don;t use a DataAdapter, use aSELECT COUNT(*) ...
as your query and use ExecuteScalar instead - it returns a single value which is zero for "no matches" and non-zero for "matches exist". Never retrieve data you don't need to...
Second, what makes you think that a DataRow.ToString call will return the count of all the cells as a string? It won't. It will always return the same string: "System.Data.DataRow".
So change to ExecuteScalar, and it'll start to work...
这篇关于检查是否在数据库中找到了字段内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文