用记事本编写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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章