2013-02-09 54 views
0

我是編程新手。學習C#並使用 visual studio我想在sql命令裏面用多個變量if語句

我用兩個文本框創建了一個文件。這些文本框 內容是使用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命令之前,爲了使用使用一個 或兩個變量(取不爲空)。 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_"; 

}

加上變量聲明爲一個聲明動作文件

謝謝

+1

指定您的問題 – Jivan 2013-02-09 15:23:04

+0

爲了做我認爲它是你想要的,你將不得不聲明_id爲可爲空的int,int ?.然後你可以看到_id是否爲'if(_id!= null)'否則在C#中,int變量不爲null。但這只是一個猜測。你的問題不是很清楚。 – PhoenixReborn 2013-02-09 15:31:04

回答

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

+0

Aron,我將如何在SQL查詢之前聲明變量_id和custName_? 因爲在您的代碼中 string cmdText = _id!= null ? @「SELECT * FROM Customers where id = @_id」 :@「SELECT * FROM Customers where customerName = @custName_」; visual studio在_id下放置一條紅線,表示「在當前上下文中不存在名稱_id」。 除了聲明兩個變量_id和custName_ 我將如何通過我正在使用的JavaScript獲取它們的值? 謝謝 – 2013-02-10 14:45:46

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