【发布时间】:2016-08-28 11:34:46
【问题描述】:
我正在尝试创建一个布尔返回函数,该函数将检查文本框中的值,使用表中的数据读取器检查它是否找到匹配项,然后返回一个布尔值。我一直现在使用这个功能很长时间了,它工作正常。
public Boolean testname()
{
Boolean test=false;
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\noewayout\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand("select CNAME from CUSTOMER", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
string cname = "";
while (dr.Read())
{
cname = Convert.ToString(dr["CNAME"]);
if (cname == textBox1.Text.ToString())
{
test = true;
return test;
}
}
conn.Close();
return test;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{ MessageBox.Show("Please type in Customer Name"); }
else
{
Boolean boo = testname();
if (boo == false)
{
MessageBox.Show("Invalid Customer Name");
}
else if (boo == true)
{
try
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\noewayout\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand("select ONAME,PRICE,QTY from OIL_TYPE,SALEOIL,CUSTOMER where OIL_TYPE.OID=SALEOIL.OID and CUSTOMER.CID=SALEOIL.CID and CNAME='" + textBox1.Text.ToString() + "'", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
int qty = 0;
int price = 0;
string oname = "";
while (dr.Read())
{
oname = Convert.ToString(dr["ONAME"]);
qty = Convert.ToInt32(dr["QTY"]);
price = Convert.ToInt32(dr["PRICE"]);
int tcost = qty * price;
MessageBox.Show("Oil Name\t\tQuantity\tPrice\tTotal Cost\n" + oname + " \t" + qty + "\t" + price + "\t" + tcost);
}
conn.Close();
}
catch (SqlException) { MessageBox.Show("Error!!"); }
}
}
}
但是在过去的几个小时里,我在使用这种简单的检查方法时遇到了问题。
public Boolean testname()
{
Boolean test = false;
SqlConnection conn = new SqlConnection(dbsource);
SqlCommand cmd = new SqlCommand("select Fid from Firearm", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
string tfid = "";
while (dr.Read())
{
tfid = Convert.ToString(dr["Fid"]);
if (tfid == textBox6.Text.ToString())
{
test = true;
return test;
}
}
conn.Close();
return test;
}
private void searchbtn_Click(object sender, EventArgs e)
{
string mysql = "select * from Firearm where Fid= '" + textBox6.Text + "'";
if (textBox6.Text == "")
{ MessageBox.Show("Please fill the text field"); }
else
{
Boolean boo = testname();
if (boo == false)
{
MessageBox.Show("Invalid Fid");
}
else if (boo == true)
{
try
{
SqlConnection conn = new SqlConnection(dbsource);
SqlCommand cmd = new SqlCommand(mysql, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
dataGridView1.Show();
}
catch (SqlException) { MessageBox.Show("Error!!!"); }
}
}
}
即使我输入了正确的 id 值,它仍然返回 false。我已经尝试了所有我知道的但仍然没有成功。任何帮助或建议都会很棒。谢谢
【问题讨论】:
-
您的 Fid 列数据类型是
char或nchar而不是varchar? -
您不需要在 txtBox.Text 属性上调用 .ToString(),因为 Text 属性已经是字符串类型
-
@Manish Dalal 是的,Fid 是 varchar(20),我也尝试过放弃 .ToString() 但它仍然返回 false。
-
下个断点,看看你的代码是否进入
while(dr.Read())循环。 -
要检查的另一件事是,您是否指的是同一数据源
dbsource,因为在您的第一个函数中,您正在附加要使用的数据库,而在第二个函数中您正在使用dbsource变量跨度>
标签: c#