0

我是编程新手。学习 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>

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

代码如下:

动作文件

<%      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_";

}

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

谢谢

4

2 回答 2

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 文件中。

于 2013-02-09T15:24:21.367 回答
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
于 2013-02-10T04:43:56.293 回答