欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.net與SQLite數(shù)據(jù)庫(kù)通過(guò)js和ashx交互(連接和操作)

 更新時(shí)間:2017年01月11日 09:16:34   作者:Hello_World_CFF  
這篇文章主要介紹了ASP.net與SQLite數(shù)據(jù)庫(kù)通過(guò)js和ashx交互(連接和操作),具有一定的參考價(jià)值,有興趣的可以了解一下。

ASP.net與SQLite數(shù)據(jù)庫(kù)通過(guò)js和ashx交互(連接和操作):

廢話(huà)(也是思路):用的是VS2010,打算做網(wǎng)站前后臺(tái)。由于不喜歡前臺(tái)語(yǔ)言里加些與html和css和js的其他內(nèi)容,想實(shí)現(xiàn)前后臺(tái)語(yǔ)言的分離,與前后臺(tái)通過(guò)js的ajax實(shí)現(xiàn)交互,故很多百度出來(lái)的方法不成立,雖聽(tīng)說(shuō)ashx過(guò)時(shí),但是他實(shí)現(xiàn)了我要的效果:即前后臺(tái)語(yǔ)言不是相互嵌入實(shí)現(xiàn)交互,而是通過(guò)js實(shí)現(xiàn)(有接口就可以)。

由于領(lǐng)導(dǎo)指定用SQLite,故這兩天還折騰了SQLite,不過(guò)對(duì)于這種小型的網(wǎng)站,它是個(gè)很好的選擇(不需要部署,只需在官網(wǎng)下載.net的,然后在項(xiàng)目中引用,在Web.config里進(jìn)行設(shè)置,即可操作——當(dāng)然還有一些錯(cuò)誤和注意事項(xiàng)要注意),總的說(shuō)來(lái),我用ashx和js的ajax實(shí)現(xiàn)前后臺(tái)的交互,后臺(tái)與SQLite數(shù)據(jù)庫(kù)連接——1.通過(guò)引用System.Data.SQLite.dll;2.在界面主文件夾下添加SQLite.Interop.dll為鏈接;3.Web.config文件配置;4.SQLiteHelper.cs編寫(xiě),ashx調(diào)用執(zhí)行(可劃分DAL進(jìn)行分類(lèi)建cs,便于管理)。

好處:實(shí)現(xiàn)前后臺(tái)語(yǔ)言的分離,不用部署,不用曾經(jīng)的SQLSERVER 的Model(get和set組成的東西)即可完成數(shù)據(jù)讀寫(xiě)(此處不管連接SQLSERVER的ADO)——暫時(shí)想到也知道這么一點(diǎn),僅供參考。

 正文:

一、ashx和js的ajax實(shí)現(xiàn)前后臺(tái)的交互:

文件路徑如圖一,js會(huì)在product.html里引用,故連接ashx的路徑,是相對(duì)product.html的路徑,js代碼如下:

$(document).ready(function () {
 $.ajax({
  url: '../InterSQLite/demo.ashx',
  type: 'post',
  datatype: 'json',
  cache: false,
  async: false,
  success: function (data) {
   alert(data);
  }
 })
})

ashx代碼如下:(主要起作用的是:context.Response.Write(strjson);)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcAdmin.InterSQLite
{
 /// <summary>
 /// demo 的摘要說(shuō)明
 /// </summary>
 public class demo : IHttpHandler
 {

  public void ProcessRequest(HttpContext context)
  {
   // string strjson = "[ {\"userName\":\"test\"}]";
   context.Response.ContentType = "text/plain";
   string str2 = "測(cè)試";
   // string strjson = "[ {\"userName\":\"test\"}]";
   string strjson = "[ {\"userName\":\"" + str2 + "\"}]";
   context.Response.Write(strjson);
  }

  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }
}

這一部分請(qǐng)注意:1.發(fā)布要在本地(我測(cè)試的發(fā)布在別的服務(wù)器,前臺(tái)接收不到)

        2.IIS配置(打開(kāi)本機(jī)的IIS——>配置):

可參考http://www.dbjr.com.cn/article/29787.htm

二、后臺(tái)與SQLite數(shù)據(jù)庫(kù)連接:

1.通過(guò)引用System.Data.SQLite.dll;

在官網(wǎng)選擇適應(yīng)電腦系統(tǒng)的.net的System.Data.SQLite.dll的下載:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

安裝后,有個(gè)測(cè)試的,點(diǎn)擊(如下圖1),選擇“System.Data.SQLite”(默認(rèn)的),改變“Data Source=已建好的SQLite數(shù)據(jù)庫(kù).db文件相對(duì)剛剛安裝的bin目錄的路徑”(例如我安裝的bin目錄是:C:\Program Files (x86)\System.Data.SQLite\2010\bin,我的數(shù)據(jù)庫(kù)文件在C:\cff\test下,則我的Data Source=../../../../cff/test/test.db),改好后,點(diǎn)擊“run”,如下圖2,則說(shuō)明安裝成功

此處個(gè)人碰到一個(gè)問(wèn)題:需要為數(shù)據(jù)庫(kù)所在的上層目錄設(shè)置權(quán)限才能全部Succeeded。錯(cuò)誤如下:attempt to write a readonly datebase

解決辦法:找到SQLite數(shù)據(jù)庫(kù)所在的文件夾,單擊右鍵,屬性->安全,為Users用戶(hù)組添加寫(xiě)入權(quán)限。這個(gè)就不截圖了。

2.在界面主文件夾下添加SQLite.Interop.dll為鏈接;

放錯(cuò)了或沒(méi)有添加鏈接,就會(huì)出現(xiàn)錯(cuò)誤:無(wú)法加載 DLL“SQLite.Interop.DLL”: 找不到指定的模塊。 (異常來(lái)自 HRESULT:0x8007007E)。

添加正確后如下圖所示:

3.Web.config配置:

 <connectionStrings>
  <add name="DB_XHKSQLite" connectionString="Data Source=相對(duì)安裝目錄的路徑(測(cè)試成功的路徑);Pooling=true;FailIfMissing=false" providerName="System.Data.SQLite"/>
 </connectionStrings>

4.SQLiteHelper.cs獲取connectionString,操作(讀?。?shù)據(jù)庫(kù):

復(fù)制代碼 代碼如下:

string str = System.Configuration.ConfigurationManager.ConnectionStrings["DB_XHKSQLite"].ToString();
         

“[]”里的參數(shù)與Web.config的connectionStrings的add的name相對(duì)應(yīng)。

另外除了下載工具創(chuàng)建數(shù)據(jù)庫(kù)外,還可以參考:http://www.dbjr.com.cn/article/67311.htm 用C#代碼創(chuàng)建,并操作SQLite數(shù)據(jù)庫(kù)(此方法未實(shí)踐,因?yàn)槲矣肧QLite Expert Personal 4.1 的圖形化界面實(shí)現(xiàn)的,此軟件界面如下圖所示:)

對(duì)于初期的測(cè)試,建議用下面內(nèi)容:

(在類(lèi)庫(kù)下此處記得引用System.Configuration,因?yàn)槟J(rèn)下他不被引用,當(dāng)然對(duì)于面對(duì)VS2010錯(cuò)誤的你,應(yīng)該自己會(huì)發(fā)現(xiàn)的,別的細(xì)節(jié)就忽略了,我忘了還有什么細(xì)節(jié)了)

從數(shù)據(jù)庫(kù)獲取的數(shù)據(jù)時(shí)dataset的,此處把它變成datatable,再轉(zhuǎn)成json傳給前臺(tái)。

下面是js需要調(diào)用的ashx文件的主干內(nèi)容:

//這是獲取連接字符串
   string str = System.Configuration.ConfigurationManager.ConnectionStrings["DB_XHKSQLite"].ToString();
   DataSet ds = new DataSet();
   //聲明一個(gè)Sqlite數(shù)據(jù)庫(kù)的鏈接
   using (SQLiteConnection conn = new SQLiteConnection(str))
   {
    //創(chuàng)建sqlite命令
    using (SQLiteCommand comm = conn.CreateCommand())
    {
     //打開(kāi)數(shù)據(jù)庫(kù)鏈接
     conn.Open();
     //select數(shù)據(jù)分頁(yè)用limit就行,很方便
     comm.CommandText = "Select * From book";
     using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(comm))
     {
      adapter.SelectCommand = comm;
      adapter.Fill(ds);
     }
     DataTable dt = new DataTable();
     dt = ds.Tables[0];
     // Common.Common ff = new Common.Common();
     string strjson = Common.Common.DataTableToJson(dt, 1);

     context.Response.Write(strjson);


    }
   }

Common.Common.DataTableToJson代碼:(按實(shí)際需要改格式)

public static string DataTableToJson(DataTable dt, int count)
  {
   StringBuilder sbjson = new StringBuilder();
   sbjson.Append("{");
   sbjson.Append("\"total\":" + count + ",\"rows\":[");
   if (dt != null)
   {
    for (int i = 0; i < dt.Rows.Count; i++)
    {
     if (i > 0)
     {
      sbjson.Append(",");
      sbjson.Append("{");
      foreach (DataColumn dc in dt.Columns)
      {
       if (dt.Columns.IndexOf(dc) > 0)
       {
        sbjson.Append(",");
        sbjson.Append("\"" + dc.ColumnName + "\":\"" + dt.Rows[i][dc.ColumnName].ToString().Trim() + "\"");
       }
       else
       {
        sbjson.Append("\"" + dc.ColumnName + "\":\"" + dt.Rows[i][dc.ColumnName].ToString().Trim() + "\"");
       }
      }
      sbjson.Append("}");
     }
     else
     {
      sbjson.Append("{");
      foreach (DataColumn dc in dt.Columns)
      {
       if (dt.Columns.IndexOf(dc) > 0)
       {
        sbjson.Append(",");
        sbjson.Append("\"" + dc.ColumnName + "\":\"" + dt.Rows[i][dc.ColumnName].ToString().Trim() + "\"");
       }
       else
       {
        sbjson.Append("\"" + dc.ColumnName + "\":\"" + dt.Rows[i][dc.ColumnName].ToString().Trim() + "\"");
       }
      }
      sbjson.Append("}");
     }
    }
   }
   sbjson.Append("]}");
   return sbjson.ToString();
  }

這是本人3天的摸索,感謝網(wǎng)上的各種資料,還有師父的遠(yuǎn)程幫助,謹(jǐn)以此總結(jié)獻(xiàn)給有需要的你,希望你能更快地實(shí)現(xiàn)前后臺(tái)的交互,若有誤導(dǎo)請(qǐng)?jiān)彛?/p>

總結(jié):運(yùn)行程序好像不僅僅是代碼的事(當(dāng)然,如果你夠厲害應(yīng)該都可以用代碼實(shí)現(xiàn)),他有時(shí)需要與圖形界面結(jié)合,設(shè)置權(quán)限等等,在一系列實(shí)現(xiàn)事情的過(guò)程中,逐一排查,才會(huì)呈現(xiàn)你想要的效果。——這是一個(gè)需要耐心的過(guò)程,這也是一個(gè)對(duì)未知的探索過(guò)程。加油!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論