我想在sql命令中使用if语句来表示多个变量

时间:2013-02-09 15:16:33

标签: c# sql ajax variables if-statement

我是编程新手。学习C#并使用 视觉工作室

我制作了一个带有两个文本框的文件。这些文本框的内容 使用javascript转移到另一个文件

listfile中

<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>

我想在sql命令之前使用if语句才能使用它 或两个变量(以无效为准)。 customerid是整数,而customerName是一个字符串。

代码如下:

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>

那就是我想要一个跟随

之类的陈述
if (_id is Notnull)
{
string cmdText = @"SELECT * FROM Customers where id= @_id";

}
else
{

string cmdText = @"SELECT * FROM Customers where customerName= @custName_";

}

加上对动作文件的变量声明

由于

2 个答案:

答案 0 :(得分:0)

<%       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();
这是你想要的吗?但是不建议将这么多代码放入aspx文件中。

答案 1 :(得分:0)

最好让你的代码接受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