主界面
功能
- 由数据库生成html代码以及ajax的初始化代码
- 根据html的标签生成ajax代码和ashx接收代码
- 由数据库生成实体类及简单的操作
使用环境
asp.net+SQL Server
测试数据库代码
create table Region(
re_id int identity(1,1) primary key,--主键id
re_name nvarchar(16) not null,--地区名称
re_shorthand nvarchar(8) not null,--地区简写
re_parentID int not null,--父系id
re_relation nvarchar( 32) not null--功能关系
);
create table testfk(
pk int primary key,
fk1 foreign key references Region(re_id),
fk2 foreign key references Region(re_id),
fk13213 foreign key references Region(re_id)
);
由数据库生成html
html代码
<div class="box box-info"> | |
<div class="box-header with-border"> | |
<h3 class="box-title">骑着猪猪的CodeMonkey生成代码</h3> | |
</div> | |
<!-- /.box-header --> | |
<!-- form start --> | |
<div class="form-horizontal"> | |
<div class="box-body"> | |
<div class="form-group"> | |
<label class="col-sm-2 control-label">pk</label> | |
<div class="col-sm-10"> | |
<input type="text" class="form-control" id="pk" placeholder="pk"/> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label class="col-sm-2 control-label">fk1</label> | |
<div class="col-sm-10"> | |
<select id="fk1" class="form-control" onchange="fk1change()"> | |
</select> | |
</div> | |
</div><div class="form-group"> | |
<label class="col-sm-2 control-label">fk2</label> | |
<div class="col-sm-10"> | |
<select id="fk2" class="form-control" onchange="fk2change()"> | |
</select> | |
</div> | |
</div><div class="form-group"> | |
<label class="col-sm-2 control-label">fk13213</label> | |
<div class="col-sm-10"> | |
<select id="fk13213" class="form-control" onchange="fk13213change()"> | |
</select> | |
</div> | |
</div> | |
</div> | |
<!-- /.box-body --> | |
<div class="box-footer"> | |
<button type="reset" class="btn btn-default">取消</button> | |
<button type="button" class="btn btn-info pull-right" onclick="ok()">确定</button> | |
</div> | |
<!-- /.box-footer --> | |
</div> | |
</div> |
ashx代码
<%@ WebHandler Language="C#" Class="Initfk1Select" %> | |
using System; | |
using System.Web; | |
using DataBaseClassLibrary; | |
public class Initfk1Select : IHttpHandler { | |
public void ProcessRequest (HttpContext context) { | |
context.Response.ContentType = "text/plain"; | |
MicrosoftSQLServer db = new MicrosoftSQLServer(SystemInfo._configure["constring"].ToString()); | |
string str = db.Execute_Select_ResultTableJSON("select * from BCMS_Region", false); | |
context.Response.Write(str); | |
} | |
public bool IsReusable { | |
get { | |
return false; | |
} | |
} | |
} | |
//---------------------------------------------------------------------------------------------- | |
<%@ WebHandler Language="C#" Class="Initfk2Select" %> | |
using System; | |
using System.Web; | |
using DataBaseClassLibrary; | |
public class Initfk2Select : IHttpHandler { | |
public void ProcessRequest (HttpContext context) { | |
context.Response.ContentType = "text/plain"; | |
MicrosoftSQLServer db = new MicrosoftSQLServer(SystemInfo._configure["constring"].ToString()); | |
string str = db.Execute_Select_ResultTableJSON("select * from BCMS_Region", false); | |
context.Response.Write(str); | |
} | |
public bool IsReusable { | |
get { | |
return false; | |
} | |
} | |
} | |
//---------------------------------------------------------------------------------------------- | |
<%@ WebHandler Language="C#" Class="Initfk13213Select" %> | |
using System; | |
using System.Web; | |
using DataBaseClassLibrary; | |
public class Initfk13213Select : IHttpHandler { | |
public void ProcessRequest (HttpContext context) { | |
context.Response.ContentType = "text/plain"; | |
MicrosoftSQLServer db = new MicrosoftSQLServer(SystemInfo._configure["constring"].ToString()); | |
string str = db.Execute_Select_ResultTableJSON("select * from BCMS_Region", false); | |
context.Response.Write(str); | |
} | |
public bool IsReusable { | |
get { | |
return false; | |
} | |
} | |
} | |
//---------------------------------------------------------------------------------------------- |
ajax代码
function fk1Init() | |
{ | |
$.ajax({ | |
type: "post", | |
url: "Initfk1Select.ashx", | |
timeout: 1000000, | |
data: { | |
}, | |
datatype: "json", | |
success: function (result) { | |
var str = eval(result); | |
var resHtml = ""; | |
$.each(str, function (index, val) { | |
resHtml += "<option value=\""+val.re_id+"\">"+val.re_name+"</option>"; | |
//alert(resHtml); | |
}); | |
$("#fk1").html(resHtml); | |
}, | |
error: function (XMLHttpRequest, textStatus, errorThrown) { | |
$("#erroInfo").text("网络错误"); | |
$("#loginInfo").modal("show"); | |
} | |
}); | |
} | |
function fk1change() | |
{ | |
alert($("#fk1").val()) | |
} | |
function fk2Init() | |
{ | |
$.ajax({ | |
type: "post", | |
url: "Initfk2Select.ashx", | |
timeout: 1000000, | |
data: { | |
}, | |
datatype: "json", | |
success: function (result) { | |
var str = eval(result); | |
var resHtml = ""; | |
$.each(str, function (index, val) { | |
resHtml += "<option value=\""+val.re_id+"\">"+val.re_name+"</option>"; | |
//alert(resHtml); | |
}); | |
$("#fk2").html(resHtml); | |
}, | |
error: function (XMLHttpRequest, textStatus, errorThrown) { | |
$("#erroInfo").text("网络错误"); | |
$("#loginInfo").modal("show"); | |
} | |
}); | |
} | |
function fk2change() | |
{ | |
alert($("#fk2").val()) | |
} | |
function fk13213Init() | |
{ | |
$.ajax({ | |
type: "post", | |
url: "Initfk13213Select.ashx", | |
timeout: 1000000, | |
data: { | |
}, | |
datatype: "json", | |
success: function (result) { | |
var str = eval(result); | |
var resHtml = ""; | |
$.each(str, function (index, val) { | |
resHtml += "<option value=\""+val.re_id+"\">"+val.re_name+"</option>"; | |
//alert(resHtml); | |
}); | |
$("#fk13213").html(resHtml); | |
}, | |
error: function (XMLHttpRequest, textStatus, errorThrown) { | |
$("#erroInfo").text("网络错误"); | |
$("#loginInfo").modal("show"); | |
} | |
}); | |
} | |
function fk13213change() | |
{ | |
alert($("#fk13213").val()) | |
} |
由输入标签生成ajax和ashx部分代码
html->ajax
ajax代码
// ========================================================================================== | |
// Name:请求test.ashx的ajax | |
// Author:骑着猪猪的CodeMonkey | |
// Create date:2017/7/18 | |
// Explain:这是有骑着猪猪的CodeMonkey的代码生成器生成的代码 | |
// 用于插入提交数据给服务器 | |
// ========================================================================================== | |
pk=$('#pk').val(); | |
fk1=$('#fk1').val(); | |
fk2=$('#fk2').val(); | |
fk13213=$('#fk13213').val(); | |
$.ajax({ | |
type: "post", | |
url: "test.ashx", | |
timeout: 1000000, | |
data: { | |
"pk":pk, | |
"fk1":fk1, | |
"fk2":fk2, | |
"fk13213":fk13213 | |
}, | |
datatype: "json", | |
success: function (result) { | |
var obj = jQuery.parseJSON(result); | |
//...自己的业务逻辑 | |
}, | |
error: function (XMLHttpRequest, textStatus, errorThrown) { | |
alert("网络错误"); | |
} | |
}); | |
// ========================================================================================== | |
// Name:请求test.ashx的ajax | |
// Author:骑着猪猪的CodeMonkey | |
// Create date:2017/7/18 | |
// Explain:这是有骑着猪猪的CodeMonkey的代码生成器生成的代码 | |
// 用于将服务器返回的数据填充到界面上 | |
// ========================================================================================== | |
$.ajax({ | |
type: "post", | |
url: "test.ashx", | |
timeout: 1000000, | |
data: { | |
"primaryKey":pk//需要自己填写 | |
}, | |
datatype: "json", | |
success: function (result) { | |
var obj = jQuery.parseJSON(result); | |
$('#pk').val(obj.pk); | |
$('#fk1').val(obj.fk1); | |
$('#fk2').val(obj.fk2); | |
$('#fk13213').val(obj.fk13213); | |
}, | |
error: function (XMLHttpRequest, textStatus, errorThrown) { | |
alert("网络错误"); | |
} | |
}); |
ashx代码
string pk = context.Request["pk"].ToString(); | |
string fk1 = context.Request["fk1"].ToString(); | |
string fk2 = context.Request["fk2"].ToString(); | |
string fk13213 = context.Request["fk13213"].ToString(); | |
ClassName obj = new ClassName(pk,fk1,fk2,fk13213); |
由数据库生成实体类
数据库生成实体类
实体类代码
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using DataBaseClassLibrary; | |
using System.Data; | |
namespace TestCodeTools | |
{ | |
/// <summary> | |
/// 由数据库表testfk生成的实体类 | |
/// 作者:骑着猪猪的CodeMonkey | |
/// </summary> | |
public class testfk | |
{ | |
public string pk; | |
public string fk1; | |
public string fk2; | |
public string fk13213; | |
public testfk() | |
{ | |
} | |
public testfk(string pk,string fk1,string fk2,string fk13213) | |
{ | |
this.pk=pk; | |
this.fk1=fk1; | |
this.fk2=fk2; | |
this.fk13213=fk13213; | |
} | |
/// <summary> | |
/// 向表中插入一行数据 | |
/// </summary> | |
/// <returns>受响应的行数</returns> | |
public int inserttestfk() | |
{ | |
string par = JsonConvert.SerializeObject(this); | |
MicrosoftSQLServer db = new MicrosoftSQLServer("Input Database Constring"); | |
string cmd = "insert into testfk values(pk,@fk1,@fk2,@fk13213)"; | |
int x = db.Execute_NonQuery(cmd, par, false); | |
return x; | |
} | |
/// <summary> | |
/// 根据主键在表中删除一行数据 | |
/// </summary> | |
/// <param name="primaryKey"></param> | |
/// <returns></returns> | |
public static int deletetestfkByPrimaryKey(string primaryKey) | |
{ | |
Dictionary<string, object> par = new Dictionary<string, object>(); | |
par.Add("@pk", primaryKey); | |
MicrosoftSQLServer db = new MicrosoftSQLServer("Input Database Constring"); | |
string cmd = "delete from testfk where pk = @pk"; | |
int x = db.Execute_NonQuery(cmd, par, false); | |
return x; | |
} | |
/// <summary> | |
/// 按住键修改某一行信息 | |
/// </summary> | |
/// | |
/// <returns>受响应行数</returns> | |
public int updatetestfkByPrimaryKey() | |
{ | |
string par = JsonConvert.SerializeObject(this); | |
MicrosoftSQLServer db = new MicrosoftSQLServer("Input Database Constring"); | |
string cmd = "update testfk set fk1=@fk1,fk2=@fk2,fk13213=@fk13213 where pk=@pk;"; | |
int x = db.Execute_NonQuery(cmd, par, false); | |
return x; | |
} | |
/// <summary> | |
/// 查询testfk中的所有信息 | |
/// </summary> | |
/// <returns>json</returns> | |
public static string selecttestfkReturnJSON() | |
{ | |
MicrosoftSQLServer db = new MicrosoftSQLServer("Input Database Constring"); | |
string cmd = "select * from testfk;"; | |
string x = db.Execute_Select_ResultTableJSON(cmd,false); | |
return x; | |
} | |
/// <summary> | |
/// 查询testfk中的所有信息 | |
/// </summary> | |
/// <returns>DataTable</returns> | |
public static DataTable selecttestfkReturnTable() | |
{ | |
MicrosoftSQLServer db = new MicrosoftSQLServer("Input Database Constring"); | |
string cmd = "select * from testfk;"; | |
DataTable x = db.Execute_Select_ResultTable(cmd, false); | |
return x; | |
} | |
/// <summary> | |
/// 按primaryKey查询testfk中的信息 | |
/// </summary> | |
/// <param name="primary">主键</param> | |
/// <returns>json</returns> | |
public static string selecttestfkReturnJSONByPrimary(string primary) | |
{ | |
MicrosoftSQLServer db = new MicrosoftSQLServer("Input Database Constring"); | |
Dictionary<string, object> par = new Dictionary<string, object>(); | |
par.Add("@pk", primary); | |
string cmd = "select * from testfk where pk = @pk"; | |
string x = db.Execute_Select_ResultTableJSON(cmd, par,false); | |
return x; | |
} | |
/// <summary> | |
/// 按primaryKey查询testfk中的信息 | |
/// </summary> | |
/// <param name="primary">主键</param> | |
/// <returns>DataTable</returns> | |
public static DataTable selecttestfkReturnTable(string primary) | |
{ | |
MicrosoftSQLServer db = new MicrosoftSQLServer("Input Database Constring"); | |
string cmd = "select * from testfk where pk = @pk"; | |
Dictionary<string, object> par = new Dictionary<string, object>(); | |
par.Add("@pk", primary); | |
DataTable x = db.Execute_Select_ResultTable(cmd,par, false); | |
return x; | |
} | |
public static testfk selectClassNameReturnObject(string primary) | |
{ | |
string json = selecttestfkReturnJSONByPrimary(primary); | |
json = json.Substring(1, json.Length - 2); | |
testfk obj = (testfk)JsonConvert.DeserializeObject(json, typeof(testfk)); | |
return obj; | |
} | |
} | |
} |
备注
因为要保证生成器的通用型,所以在生成的代码中还需要更具实际的情况做一些小的调整