用記事本編寫app接口,sqllite數據庫操作

首先,保證我們的電腦安裝了.net框架和iis的應用程序開發功能,
我們在iis下新建一個網站,選擇一個.net的程序池
在我們網站根目錄,新建一個文件,命名爲ports.ashx
寫入代碼如下:
<%@ WebHandler Language="C#" Class="ports" %>


using System;
using System.Web;


public class ports : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("hellow world");
}
public bool IsReusable {
get {
return false;
}
}




}
保存後訪問 127.0.0.1/ports.ashx

網頁顯示 hellow world

---------------------------------------

然後是參數解析和json構造
引用下面類
using System.Web.Script.Serialization;
新建兩個內部類
class d
{
public string a{get;set;}
public int b{get;set;}
public string c{get;set;}
}

class JsonHelp
{
public static string GetJson<T>(T obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(obj); 
}
public static T ParseFromJson<T>(string szJson)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Deserialize<T>(szJson); 
}
}
修改方法ProcessRequest
context.Response.ContentType = "text/plain";
d t=new d();
t.a="hellow";
t.b=100;
string c = context.Request["c"];
t.c=c;
context.Response.Write(JsonHelp.GetJson(t));
保存代碼,訪問頁面獲得請求結果
http://127.0.0.1/?c=creat
{"a":"hellow","b":100,"c":"creat"}


http://127.0.0.1/?c=creat
第三 數據庫使用
由於ms sql太大,安裝也慢


http://127.0.0.1/?c=creat




因爲我電腦是64位,所以下載了64位的sqllite
我下載了sqllite ,只有

500K,配置好環境變量 
下載sqllite2.0 64位的庫
下載System.Data.SQLite.dll
SQLite.Interop.096.dll
SQLite.Interop.dll
在站點根目錄新建bin目錄,將庫文件放入其中



http://127.0.0.1/?c=creat
第四新建內部類
class OpDB
{
public static string getDbConnectionString()
{
try
{
return "Data Source="+HttpRuntime.AppDomainAppPath.ToString()+"im.db";
}
catch (Exception ex)
{
throw (ex);
}
}


public static bool Insert_or_Update_or_Delete(string Sql, SQLiteParameter[] paras)
{
// 創建數據庫的連接對象
try
{

SQLiteConnection con2 = new SQLiteConnection(getDbConnectionString());
con2.Open();
SQLiteCommand cmd2 = new SQLiteCommand(Sql, con2);
cmd2.Parameters.AddRange(paras);
cmd2.ExecuteNonQuery();
return true;
}
catch (Exception)
{

throw;
}

}
public static DataTable load(string sql)
{
// 創建數據庫的連接對象
SQLiteConnection conn = new SQLiteConnection(getDbConnectionString());
try
{
// 打開數據庫
conn.Open();
// 構造數據適配器
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);


DataSet ds = new DataSet();
da.Fill(ds);


return ds.Tables[0];
}
catch (Exception ex)
{
throw ex;
return null;
}
finally
{
// 關閉連接
conn.Close();
}
}

public static DataTable load(string sql, SQLiteParameter[] paras)
{
// 創建數據庫的連接對象
SQLiteConnection conn = new SQLiteConnection(getDbConnectionString());
try
{
// 打開數據庫
conn.Open();


// 構造數據命令
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.Parameters.AddRange(paras);
// 構造數據適配器 
SQLiteDataAdapter da = new SQLiteDataAdapter();
da.SelectCommand = cmd;


DataSet ds = new DataSet();
da.Fill(ds);


return ds.Tables[0];
}
catch (Exception ex)
{
throw ex;
return null;
}
finally
{
// 關閉連接
conn.Close();
}
}



}


新建方法
public static void create()
{
string sql = @"CREATE TABLE Messages
(
msg_id INTEGER PRIMARY KEY,
sendcontent NVARCHAR(1000),
fromid NVARCHAR(50),
toid NVARCHAR(50),
headimg NVARCHAR(1000),
sendimg NVARCHAR(1000),
sendvoice NVARCHAR(1000),
time BIGINT default 0,
issend BOOLEAN default 0
)";
try
{
OpDB.Insert_or_Update_or_Delete(sql, new SQLiteParameter[]{});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}




string sql2 = @"CREATE TABLE Logins
(
userid NVARCHAR(50) PRIMARY KEY,
changetime BIGINT
)";
try
{
OpDB.Insert_or_Update_or_Delete(sql2, new SQLiteParameter[] { });
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
修改方法
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
d t=new d();
t.a="hellow";
t.b=100;
string c = context.Request["c"];
t.c=c;
if(c=="creat")
create();
context.Response.Write(JsonHelp.GetJson(t));
}
調用接口地址,參數c=creat
獲得正確json結果
檢查站點根目錄
發現im.db已經創建,用sqlite expert professional打開該文件,可以看到我們代碼新建的表
所有代碼都被正確調用了

http://127.0.0.1/?c=creat
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章