簡體   English   中英

如何在JavaScript文件中調用SQL函數? ASP.NET C#

[英]How to call a sql function inside a javascript file? asp.net c#

我正在開發一個日歷,該日歷從sql數據庫中獲取條目。 在javascript文件內部,以編程方式構建日歷,當它在一天之內生成日歷單元時(在這種情況下是var計數器),我對代碼進行了硬編碼,我只需要將查詢放入函數中

JavaScript文件

  if (counter == 2 //2 is the Harcoded day
        && mes == 8 //Hardcoded month
        && anio == 2014) { //Harcoded year
            PageMethods.Msg(onSuccess);
            function onSuccess(response) {
                alert(response);
            }

            htmlContent += "<div class='usuer'>Here data retrieved from DB</div>";
            htmlContent += "<div class='client'>Here data retrieved from DB</div>";
            htmlContent += "<div class='num'>Here data retrieved from DB</div>";
            htmlContent += "<div class='status'></div>";
        }

我在后面的代碼中創建了方法,但我不知道如何傳遞對象Div來獲取查詢結果並將其設置在Divs中,或者獲取計數器將其放入查詢中並獲取特定日期的結果

背后的代碼

[System.Web.Services.WebMethod]
    public static string Msg()
    {
        return "Hello world";


        string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
        using (SqlConnection myConnection = new SqlConnection(connStr))
        {
            myConnection.Open();
            SqlCommand sqlCommand = new SqlCommand(" SELECT * FROM table",myConnection);


            //Add parameters to the query
            //<--- Here? How to get counter of the javascript file? :( 


            SqlDataReader dr = sqlCommand.ExecuteReader();
            if (dr != null)
            {
                while (dr.Read())
                {
                    //<-- Here? How to set the result to the divs? :(

                }
            }
        }
    }

更好地使用Ajax請求將數據加載到日歷中,但是如果必須

創建一個准備數據的操作,並將其作為參數發送以進行查看。 在視圖中使用<script>標記 在這里結束您的代碼。

ction

    public ActionResult MyJS()
    {
        calendar = new List<DateTime>();

        calendar.Add(DateTime.Parse("2010-03-21 12:00"));
        calendar.Add(DateTime.Parse("2011-03-21 12:00"));
        calendar.Add(DateTime.Parse("2012-03-21 12:00"));

        return View(calendar);
    }

活力

@model List<DateTime>
@{
    Layout = null; // important
}
@foreach (var item in Model)
{
    <text> 
        alert(" @item.ToString()  ");
    </text>
}

並添加到布局<script src="home/myjs"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM