从表中检索数据的一些错误 [英] Some bug in retrieving data from table
问题描述
大家好!
我正在尝试检查我的表中是否存在某些数据,因此请求检索数据。代码如下。但是,似乎有一些问题,并且数据不会返回,尽管它已经存在!有人可以指出我哪里出错吗?
免责声明:我是一个菜鸟...所以请耐心等待...抱歉
Hi everyone!
I am trying to check if some data exists in my table and thus, request for the data to be retrieved if it does. The code is as follows. However, there seems to be some problem and the data is not returned ALTHOUGH it EXISTS! can someone please point out where I am going wrong ??
disclaimer : i am a noob ... so please bear with me ... sorry
string coName = TextBox8.Text;//textbox containing the company name
string type = "LENDER";
SqlConnection connCheck = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;");
connCheck.Open();
string query = " SELECT name,coType FROM client WHERE name = '@coNam1' and coType = '@typeCo' ";
using (SqlCommand cmd1 = new SqlCommand(query, connCheck))
{//Not Null Parameters
cmd1.Parameters.AddWithValue("@coNam1", coName);
cmd1.Parameters.AddWithValue("@typeCo", type);
SqlDataReader dr = cmd1.ExecuteReader();
Label2.Text = "doesn't yet";
if (dr.RecordsAffected > 0)
{
string treatment = dr[0].ToString();
Label2.Text = "works";
}
connCheck.Close();
}
}
我表中的列是''名称''和coType
先谢谢!!
p.s.当单击相同的按钮时,我设法将数据插入到同一个表中..但不知何故无法检索它...:(
The columns in my table are ''name'' and coType
Thanks in advance!!
p.s. when the same button is clicked, I manage to insert data INTO the same table .. but am somehow not able to retrieve it ... :(
推荐答案
我的坏大家......就像我说的那样......我是菜鸟 - 得到了答案!
string query =SELECT name,coType FROM client WHERE name = @ coNam1和coType = @typeCo;
@ coNam1和@typeCo不会出现在''''...
现在可以使用。
My bad everyone ... like I said .. i''m a noob - got the answer!
string query = " SELECT name,coType FROM client WHERE name = @coNam1 and coType = @typeCo ";
The @coNam1 and @typeCo do NOT appear in '' '' ...
It works now.
这篇关于从表中检索数据的一些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!