不懂sql上的處理
不懂sql上的處理我在asp.net webform c#上 2個textbox + 1個button + 1個gridview
我的目的是想 使用者可以作3項search
1. Only Search name
2.Only Search age
3. Search name+age
但我現在只能search age是對的, name會出錯
下列是我的sql
SelectCommand="SELECT ID, NAME, Age, Sex, Nunber FROM Customer WHERE (([age] = @age) OR ([name] LIKE '%' + @name + '%'))">
<SelectParameters>
<asp:FormParameter FormField="name" Name="name" Type="String" />
<asp:FormParameter FormField="age" Name="age" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource> --------------------编程问答-------------------- --------------------编程问答-------------------- 查看参数是否有正确传入值 --------------------编程问答--------------------
string sql = "SELECT ID, NAME, Age, Sex, Nunber FROM Customer WHERE (([age] = @age) OR ([name] LIKE '%" + @name + "%+'))";--------------------编程问答-------------------- SelectCommand="SELECT ID, NAME, Age, Sex, Nunber FROM Customer WHERE (([age] = @age) OR ([name] LIKE '%' + @name + '%'))">
没看出来么问题。你调试跟踪下。 --------------------编程问答--------------------
SelectCommand="SELECT ID, NAME, Age, Sex, Nunber FROM Customer WHERE (([age] = @age) OR ([name] LIKE '%' + @name + '%'))">
问题大了。
@name怎么能这么用? --------------------编程问答-------------------- 應該什樣用才對呢? --------------------编程问答-------------------- LIKE '%'" + @name + "'%'))" --------------------编程问答-------------------- 不带“”IKE '%'" + @name + "'%'))"@name 就是一个常量了。 --------------------编程问答-------------------- <C# code>
SelectCommand=@"SELECT ID, NAME, Age, Sex, Nunber FROM Customer WHERE (([age] = @age) OR ([name] LIKE '%'@name'%'))"
</code>
@name左右的‘+’号加进去不知是用来连接字符串还是什么的。如果是连接字符串的话应该是:
SelectCommand="SELECT ID, NAME, Age, Sex, Nunber FROM Customer WHERE (([age] = @age) OR ([name] LIKE '%'" + @name + "'%'))"
这种形式的 --------------------编程问答-------------------- 顺道问一下:怎么像5#那样贴代码? --------------------编程问答--------------------
插入源代码 --------------------编程问答-------------------- SelectCommand="SELECT ID, NAME, Age, Sex, Nunber FROM Customer WHERE (([age] = @age) OR ([name] LIKE '%'" + @name + "'%'))"
补充:.NET技术 , ASP.NET