C# 我想在sql命令中对多个变量使用if语句
我是编程新手。C语言的学习与使用 visual studio 我制作了一个包含两个文本框的文件。这些文本框的内容 使用javascript传输到另一个文件 列表文件C# 我想在sql命令中对多个变量使用if语句,c#,sql,ajax,variables,if-statement,C#,Sql,Ajax,Variables,If Statement,我是编程新手。C语言的学习与使用 visual studio 我制作了一个包含两个文本框的文件。这些文本框的内容 使用javascript传输到另一个文件 列表文件 <script type="text/javascript"> function RunAjax1(custId) { var custId = document.getElementById("customerId").value; //var custN
<script type="text/javascript">
function RunAjax1(custId) {
var custId = document.getElementById("customerId").value;
//var custName = document.getElementById("customerName").value;
jQuery.ajax(
{
url: "CustActions.aspx?id=" + custId +"&custName="+customerName,
type: "GET"
}
).done(function (responseText) {
jQuery("#display").html(responseText)
});
}
</script>
}
向actionfile添加变量声明
<% SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString());
string cmdText = @"SELECT * FROM Customers where id= @_id";
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.Parameters.Add("_id", SqlDbType.Int).Value = Convert.ToInt16(Request["id"].ToString());
cmd.Parameters.Add("custName_",SqlDbType.VarChar).Value=Convert.ToChar(Request["custName"].ToString());
DataTable dt = new DataTable();
con.Open();
dt.Load(cmd.ExecuteReader());
con.Close();
foreach (DataRow dr in dt.Rows)
{
Response.Write(string.Format(@"<tr>
<td>{0}</td>
<td>{1}</td>
谢谢
这是你想要的吗?但是,不建议在aspx文件中放入这么多代码。最好是让代码接受2个参数,然后让存储过程处理空值,并使用if语句 像这样
Create proc dosomething
(
-- initialized the values to null if no value is passed in
@id tinyint = NULL
@CustomerName varchar 100 = NULL
)
BEGIN
if @tinyint is NULL and CustomerName is not null
SELECT * FROM Customers where id= @id ";
END
BEGIN
if @CustomerName is NULL and @tinyint is NOT NULL
SELECT * FROM Customers where customerName= @Customername";
END
BEGIN
if @CustomerName is NULL NOT and @tinyint is NOT NULL
SELECT * FROM Customers where (customerName= @Customername and id = @id) ";
END
要实现我认为您想要的功能,您必须将_id声明为可为null的int,int?。然后您可以通过'if\u id!=null`否则在C中,int变量不为null。但这只是一个猜测。你的问题不是很清楚。Aron,在sql查询之前,我将如何声明变量id和custName?因为在您的代码字符串cmdText=id!=空的@从id=@customerName=@custName的客户中选择*;VisualStudio正在id下放置一条红线,表示当前上下文中不存在名称id。除了声明两个变量_id和custName之外,我如何通过我使用的javascript获取它们的值?谢谢
<% SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString());
string cmdText = _id != null
? @"SELECT * FROM Customers where id= @_id"
: @"SELECT * FROM Customers where customerName= @custName_";
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.Parameters.Add("_id", SqlDbType.Int).Value = Convert.ToInt16(Request["id"].ToString());
cmd.Parameters.Add("custName_",SqlDbType.VarChar).Value=Convert.ToChar(Request["custName"].ToString());
DataTable dt = new DataTable();
con.Open();
dt.Load(cmd.ExecuteReader());
con.Close();
Create proc dosomething
(
-- initialized the values to null if no value is passed in
@id tinyint = NULL
@CustomerName varchar 100 = NULL
)
BEGIN
if @tinyint is NULL and CustomerName is not null
SELECT * FROM Customers where id= @id ";
END
BEGIN
if @CustomerName is NULL and @tinyint is NOT NULL
SELECT * FROM Customers where customerName= @Customername";
END
BEGIN
if @CustomerName is NULL NOT and @tinyint is NOT NULL
SELECT * FROM Customers where (customerName= @Customername and id = @id) ";
END