我是編程新手。學習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_";
}
加上變量聲明爲一個聲明動作文件
謝謝
指定您的問題 – Jivan 2013-02-09 15:23:04
爲了做我認爲它是你想要的,你將不得不聲明_id爲可爲空的int,int ?.然後你可以看到_id是否爲'if(_id!= null)'否則在C#中,int變量不爲null。但這只是一個猜測。你的問題不是很清楚。 – PhoenixReborn 2013-02-09 15:31:04