检查sql查询中的两个文本框? [英] Checking Two textboxes in sql queries?

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

问题描述

 字符串 selQuery =   SELECT Id FROM MapDataImage WHERE Source =' + TextBox1.Text +  'Destination ='  + TextBox2.Text +;  ;  





i只是为检查两个文本框值做了一个代码?

可以吗?

解决方案

如果你的数据库是SQL,那么试试

 String selQuery =SELECT Id FROM MapDataImage WHERE Source = @ t1  AND  Destination = @ t2 ; 
SqlCommand cmd = new SqlCommand(selQuery);
cmd.Parameters.Add(@ t1,TextBox1.Text);
cmd.Parameters.Add(@ t2,TextBox2 .Text);





其他数据库做类似的事情 - 你只需要更改 SqlCommand 到适当的对象,并注意并非所有DBMS都允许对于命名参数


如果要精确匹配两个值,请使用以下



 字符串 selQuery =   SELECT Id FROM MapDataImage WHERE Source ='  + TextBox1.Text +  'AND Destination =' + TextBox2.Text +  '; 







但是如果你想匹配任何一个文本框的值,那就用这个





 字符串 selQuery =   SELECT Id FROM MapDataImage WHERE(Source =' + TextBox1.Text +  'OR Destination =' + TextBox2.Text +  '); 


请修改您的查询

请参阅

字符串 selQuery = SELECT Id FROM MapDataImage WHERE Source =' + TextBox1.Text + '和Destination =' + TextBox2.Text + ';


String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "' Destination='"  +TextBox2.Text + ;";



i just made a code for check two textbox values?
is it okay?

解决方案

If your database is SQL then try

String selQuery = "SELECT Id FROM MapDataImage WHERE Source = @t1 AND Destination = @t2";
SqlCommand cmd = new SqlCommand(selQuery);
cmd.Parameters.Add("@t1", TextBox1.Text);
cmd.Parameters.Add("@t2", TextBox2.Text);



Other databases do similar things - you just need to change the SqlCommand to the appropriate object, and be aware that not all DBMS' allow for named parameters


If you want to match both values exactly, then use following

String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "' AND Destination='"  +TextBox2.Text + "'";




but if you want to match value of any one text box, then use this


String selQuery = "SELECT Id FROM MapDataImage WHERE (Source='" + TextBox1.Text + "' OR Destination='"  +TextBox2.Text + "')";


please modify your query 

see this 

String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "' and Destination='"  +TextBox2.Text + "'";


这篇关于检查sql查询中的两个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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