2012-12-26 26 views
0

我有SQL表,我想顯示它們的Name列包含用戶輸入文本的所有行。怎麼做?如何顯示包含文本的所有字段?

我與C#中使用此查詢

sqlcommand cmd=new sqlcommand(); 
cmd="Select * from tablename where Name ='" + textboxid.text + "'"; 

這裏2010

+3

[你嘗試過什麼(http://whathaveyoutried.com)?請張貼您的嘗試並解釋您卡在哪裏。有很多方法可以做到這一點,我們需要看看你是如何接近它的,所以我們可以提供幫助。 – Oded

回答

0
select * from Table where Name is not null 
select * from Table where Name is not null and Name<>'' 
0

嘗試做它在Visual Studio中,tablename是u必須創建表和文本框標識的名稱是id你將在其中輸入姓名的文本框。

+0

構建使用原始用戶輸入的查詢時,應始終使用參數來確保您的代碼不容易受到SQL注入攻擊。請參閱示例[here](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.prepare.aspx)瞭解如何完成此操作。 – ig0774

+0

我的意思是顯示包含textboxid.text的字符串,不等於.. – Billie

0
Select * from tablename where DATALENGTH(Name) > 0 

嘗試在你的SqlCommand對象這個查詢..

0

試試這個,它使用的查詢參數:

SqlCommand myCommand = new SqlCommand(
    string.Format("Select * from Table where Name = @NameInput"), SqlConnection); 

SqlParameter param = new SqlParameter(); 
param.ParameterName = "@NameInput"; 
param.Value = textbox.Text; 
param.SqlDbType = SqlDbType.Char; 
myCommand.Parameters.Add(param); 
相關問題