参数化查询问题 [英] Parameterized query issue

查看:72
本文介绍了参数化查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我对参数化查询有疑问.

在这种情况下,我需要从数据库中获取值并显示在gridview中,但始终都没有.的记录为零.

你能帮我吗?

我所做的是:

Hi,

I have a issue with parameterized query.

Here the case is I need to fetch the values from DB and display in gridview, but in all the times the no. of records are zero.

Can you help me?

What I did was:

MySqlCommand cmd=new MySqlCommand("SELECT PRD.PRD_IDY,BAT_NME,bat_idy FROM BRD, PRD WHERE BRD.PRD_IDY = PRD.PRD_IDY AND PRD.PRD_IDY=@prdidy and bat_idy=@batchidy ",con);

cmd.Parameters.AddWithValue("@prdidy", prdidy);
cmd.Parameters.AddWithValue("@batchidy", batchidy);

reader = cmd.ExecuteReader();
if(reader.HasRows)
{
    reader.Read();
    MessageBox.Show(reader.GetValue(0).ToString());
}

推荐答案

是否可以检入数据库并确保您具有要发送到数据库的prdidy和batchidy值的记录.

否则查询看起来还可以.
Can you check in the database and make sure you have records available for the prdidy and batchidy values you are sending into the database.

Your query otherwise looks ok.


MySqlCommand cmd=new MySqlCommand("SELECT PRD.PRD_IDY,BAT_NME,bat_idy FROM BRD, PRD WHERE BRD.PRD_IDY = PRD.PRD_IDY AND PRD.PRD_IDY=@prdidy and bat_idy=@batchidy ",con);

cmd.Parameters.AddWithValue("@prdidy", prdidy);
cmd.Parameters.AddWithValue("@batchidy", batchidy);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read)
{
GridView1.Datasource=reader ;
GridView1.DataBind();

reader .Close();
con.Close();
}


你好奎师那,

因为我看到您正在将表BRD和PRD连接到它们的公共字段PRD_ID(我猜BRD.PRD_ID是对表PRD的外键引用),所以您真的确定联接将返回值吗? br/> 也许两个表的联接已经为空.

请尝试以下操作:

Hello Krishna,

since I see that you''re joining tables BRD and PRD on their common field PRD_ID (I''m guessing that BRD.PRD_ID is a foreign key reference to table PRD), are you really sure that the join would return values?
Maybe the join of both tables is already empty.

Please try this:

MySqlCommand cmd=new MySqlCommand("SELECT PRD.PRD_IDY,BAT_NME,bat_idy FROM BRD, PRD WHERE BRD.PRD_IDY = PRD.PRD_IDY", con);
//cmd.Parameters.AddWithValue("@prdidy", prdidy);
//cmd.Parameters.AddWithValue("@batchidy", batchidy);
reader = cmd.ExecuteReader();
if(reader.HasRows)
{
    reader.Read();
    MessageBox.Show(reader.GetValue(0).ToString());
}



这会返回结果吗?

修改:

好的.您说我上面粘贴的代码也不会返回任何记录.这意味着两个表在其公共列PRD_ID上的联接为空.
这实际上意味着两个表在PRD_ID列中没有公共值.

最终修改

干杯

曼弗雷德(Manfred)



Does this return results?

Modification:

Ok. You said code I pasted above does also return no records. This means the join of both tables on their common column PRD_ID is empty.
This means in effect that both tables do not have a common value in column PRD_ID.

End Modification

Cheers

Manfred


这篇关于参数化查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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