Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想在sql命令中对多个变量使用if语句_C#_Sql_Ajax_Variables_If Statement - Fatal编程技术网

C# 我想在sql命令中对多个变量使用if语句

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

我是编程新手。C语言的学习与使用 visual studio

我制作了一个包含两个文本框的文件。这些文本框的内容 使用javascript传输到另一个文件

列表文件

<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