0

私はプログラミングが初めてです。C# の学習と Visual Studio の使用

2 つのテキスト ボックスを持つファイルを作成しました。これらのテキスト ボックスの内容は、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>

1 つまたは 2 つの変数 (null でない方) を使用するために、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

コードに2つのパラメーターを受け入れさせ、ストアドプロシージャにnullを処理させ、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 に答える
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 に答える