JS中获取数据库中的值

    在本次项目中,遇到很多问题,经过努力,都逐步得到解决。静下心来,做一个记录,以供以后学习。

    在项目中遇到一个问题,需要在JS中读取数据库中的值,然后再把值返回到页面中,解决方案如下:使用Ajax方法来实现,需要用到ajax.dll(一个ajax技术开发的帮助类库)。

    实施过程如下:

    1、引用Ajax.dll

    2、在App_Code写具体的方法,最好单独建立一个类文件,然后写具体方法。        

View Code
public class AjaxMethod
{
    public AjaxMethod()
    {
        //
        
//TODO: 在此处添加构造函数逻辑
        
//
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)] 
    public static string GetCardMoney(string cardNo,string cardPwd)
    {
        string mConn = IConfiguration.getParameter("connectString");
        IDBOSQL.IDBO dbo = IDBOSQL.IDBO.getIDBO(mConn);
        dbo.openDatabase();
        DataSet ds = dbo.executeSelectSql("select Card_Money,Service_Discount,Good_Discount from Table_CardInfo join Dic_CardType on Table_CardInfo.CardType_ID= Dic_CardType.CardType_ID where Card_NO='"+cardNo+"' and Card_Pwd= '"+cardPwd+"'and card_Status='正常'");
        DataTable dt = ds.Tables[0];
        string  money = dt.Rows[0][0].ToString();
        string service_discount = dt.Rows[0][1].ToString();
    string good_discount = dt.Rows[0][2].ToString();
        dbo.closeDatabase();
       return money+","+service_discount+','+good_discount;//此处返回一个多个值拼接成的字符串
    }
}
3、在JS中调用
moneydiscount= AjaxMethod.GetCardMoney(card, pwd).value;
moneydiscount是一个多个值拼接成的字符串,要获取多个值的话,可以把该字符串转换为一个数组,然后去访问。
arr=moneydiscount.split(",");        这样的话可以很方便的使用Ajax返回多个值。
4、要把结果再返回到页面中
document.getElementById("txtCard_Money1").value=arr[0]; 
5、以上方法要在Web.config文件中增加
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
   今天的内容就写到这里,以后有时间再慢慢写

       

原文地址:https://www.cnblogs.com/trieagle/p/2201938.html