又一个MVC问题。
我正在开发一个Web应用程序工具,它可以执行大量的数据库请求等。这一页给了我很多MVC的斗争。
我之前用C#创建了页面,这就是它的样子
您最多可以显示4个网格,以便比较值。
现在在MVC中,我正在考虑以这种方式解决问题:
然而,我总是通常基于连接问题得到随机错误消息...我尝试了很多不同的东西,这就是为什么我的代码现在有点乱,但这里是最重要的部分。 (只有2个选项的例子)
查看
传递MainDDL1,SubDDL1,Search1,MainDDL2,SubDDL2,Search2(可行)
CONTROLLER
public PartialViewResult getGrid1(string MainDDL1, string SubDDL1, string Search1)
{
DataSearchModel voModel = new DataSearchModel();
voModel.dtResultSet1 = DLA.DataSearchContext.getResultSet1(MainDDL1, SubDDL1, Search1);
return PartialView(MainDDL1, voModel);
}
public PartialViewResult getGrid2(string MainDDL2, string SubDDL2, string Search2)
{
DataSearchModel voModel = new DataSearchModel();
voModel.dtResultSet2 = DLA.DataSearchContext.getResultSet2(MainDDL2, SubDDL2, Search2);
return PartialView(MainDDL2, voModel);
}
public ViewResult DataSearch(string text)
{
DataSearchModel oModel = new DataSearchModel();
oModel.alMainDDL = DLA.DataSearchContext.getMainDDL();
return View(oModel);
}
我真的不喜欢我必须使用dtResultSet1和dtResultSet2而不是只调用相同的方法。为什么我不能只调用getResultSet,dtResultSet等?!自从我创建新模型以来,模型应该拥有自己的方法吗?或者是一个与对象无法比较的模型。
模型
声明dtResultSet1,dtResultSet2,搜索字符串等。
上下文
public static DataTable getResultSet1(string sChoice, string sFeat, string sSearch)
{
return setResultSet1(sChoice, sFeat, sSearch);
}
private static DataTable setResultSet1(string sChoice, string sFeat, string sSearch)
{
DataTable dtTemp = new DataTable();
string sQuery = setSqlQuery(sChoice, sFeat, sSearch);
OleDbConnection dbConnection = null;
// Instantiate the Connection Object
dbConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleDataBase"].ConnectionString);
//dbConnection.Open();
dbConnection.Open();
OleDbCommand dbCommand = null;
// Instantiate the Command Object
dbCommand = new OleDbCommand(sQuery, dbConnection);
dbCommand.CommandType = CommandType.Text;
OleDbDataReader dr = null;
// Execute the Stored Procedure
dr = dbCommand.ExecuteReader();
dtTemp = setResultSetRows(dtTemp, sChoice, dr);
dr.Dispose();
dbConnection.Close();
return dtTemp;
}
private static string setSqlQuery(string sChoice, string sFeat, string sSearch)
{
switch (sChoice)
{
case "T_PRCL":
case "T_PRCL_FEA":
case "T_GIS_PRCL":
return "SELECT * FROM " + sChoice + " WHERE " + sFeat + "='" + sSearch + "' and sys_del_flag = 0";
case "SGD_SFC_FEAT":
case "MSURFACE":
case "SGD_MIN_FEAT":
case "MMINERAL":
return "SELECT * FROM " + sChoice + " WHERE " + sFeat + "='" + sSearch + "' AND EXPIRY_DATE is NULL";
case "V_SURFACE":
case "V_MINERAL":
return "SELECT * FROM " + sChoice + " WHERE " + sFeat + "='" + sSearch + "'";
default:
return "SELECT sysdate as UNKNOWN_ERROR from dual";
}
}
2. one的方法完全相同..再次,我更愿意将所有内容放在一个名为getResultSet的静态方法中。此外,我确实将Connection作为全局变量(OleDBConnection = null等),连接等是自己的方法。每当Connections重叠时,这给了我更多随机错误。通常如果一个Reader在另一个之前完成。我不明白,因为它们是不同的模型(对象)不应该使用自己的独立变量,对象?!
我希望有人能为此带来一些启示。
答案 0 :(得分:0)
从Pabloker的Tipp看到Connection Management ASP.net后,我能够解决我的问题。
以下是本准则中最重要的部分。
<强> CONTEXT 强>
public class MyConnectionManager : IDisposable
{
[ThreadStatic] // static per thread
private static OleDbConnection con;
public static OleDbConnection Connection
{
get
{
if (con == null)
{
con = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleDataBase"].ConnectionString);
con.Open();
}
return con;
}
}
public void Dispose()
{
if (con != null)
{
con.Close();
}
}
}
private static DataTable setResultSet(string sChoice, string sFeat, string sSearch)
{
DataTable dtTemp = new DataTable();
string sQuery = setSqlQuery(sChoice, sFeat, sSearch);
// Instantiate the Command Object
OleDbCommand dbCommand = new OleDbCommand(sQuery, MyConnectionManager.Connection);
dbCommand.CommandType = CommandType.Text;
// Execute the Stored Procedure
OleDbDataReader dr = dbCommand.ExecuteReader();
dtTemp = setResultSetRows(dtTemp, sChoice, dr);
return dtTemp;
}
public static DataTable getResultSet(string sChoice, string sFeat, string sSearch)
{
return setResultSet(sChoice, sFeat, sSearch);
}
<强> MODEL 强>
public DataTable dtResultSet { get; set; }
<强> CONTROLLER 强>
public PartialViewResult getGrid1(string MainDDL1, string SubDDL1, string Search1)
{
DataSearchModel voModel = new DataSearchModel();
voModel.dtResultSet = DLA.DataSearchContext.getResultSet(MainDDL1, SubDDL1, Search1);
return PartialView(MainDDL1, voModel);
}
public PartialViewResult getGrid2(string MainDDL2, string SubDDL2, string Search2)
{
DataSearchModel voModel = new DataSearchModel();
voModel.dtResultSet = DLA.DataSearchContext.getResultSet(MainDDL2, SubDDL2, Search2);
return PartialView(MainDDL2, voModel);
}
查看强>
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup({
error: function (xhr, status, error) {
alert("An AJAX error occured: " + status + "\nError: " + error);
}
});
$('#btnSubmit').click(function () {
var time = new Date().getTime(); // @* unique random number to workaround IE cache issue - IE will cache the ajax if you don't use this *@
var oMainDDL1 = $('#MainDDL1').data("tComboBox");
var oSubDDL1 = $('#SubDDL1').data("tComboBox");
var sSearch1 = $("#Search1").val();
var oMainDDL2 = $('#MainDDL2').data("tComboBox");
var oSubDDL2 = $('#SubDDL2').data("tComboBox");
var sSearch2 = $("#Search2").val();
var actionURL = '@Url.Action("getGrid1", "DataSearch", new { MainDDL1 = "PLACEHOLDER" })'.replace('PLACEHOLDER', oMainDDL1.value()) + "&SubDDL1=" + oSubDDL1.value() + "&Search1=" + sSearch1 + "&time=" + time;
if (actionURL != null) {
alert('actionURL');
$.get(actionURL, function (data) {
$('#result1').fadeOut('slow', 'linear', function () { $('#result1').empty(); $('#result1').append(data); });
$('#result1').fadeIn('slow', 'linear', function () {
if ($.browser.msie) {
this.style.removeAttribute('filter'); // @* Needed to fix IE7 cleartype bug with jQuery fade, but will crap out on FF/Chrome *@
}
});
});
}
actionURL = '@Url.Action("getGrid2", "DataSearch", new { MainDDL2 = "PLACEHOLDER" })'.replace('PLACEHOLDER', oMainDDL2.value()) + "&SubDDL2=" + oSubDDL2.value() + "&Search2=" + sSearch2 + "&time=" + time;
if (actionURL != null) {
alert('actionURL');
$.get(actionURL, function (data) {
$('#result2').fadeOut('slow', 'linear', function () { $('#result2').empty(); $('#result2').append(data); });
$('#result2').fadeIn('slow', 'linear', function () {
if ($.browser.msie) {
this.style.removeAttribute('filter'); // @* Needed to fix IE7 cleartype bug with jQuery fade, but will crap out on FF/Chrome *@
}
});
});
}
});
});
function onMainDDL1Change(e) {
var combo = $("#SubDDL1").data("tComboBox");
combo.value("");
combo.reload();
}
function onSubDDL1DataBinding(e) {
var combo = $("#MainDDL1").data("tComboBox");
e.data = $.extend({}, e.data, { mainDDL1ID: combo.value() });
}
function onMainDDL2Change(e) {
var combo = $("#SubDDL2").data("tComboBox");
combo.value("");
combo.reload();
}
function onSubDDL2DataBinding(e) {
var combo = $("#MainDDL2").data("tComboBox");
e.data = $.extend({}, e.data, { mainDDL2ID: combo.value() });
}
</script>
<table>
<tr>
<td>
@(Html.Telerik().ComboBox()
.Name("MainDDL1")
.AutoFill(true)
.DataBinding(binding => binding.Ajax().Select("LoadMainDDL", "DataSearch"))
.HighlightFirstMatch(true)
.ClientEvents(events => events.OnChange("onMainDDL1Change"))
)
</td>
<td>
@(Html.Telerik().ComboBox()
.Name("MainDDL2")
.AutoFill(true)
.DataBinding(binding => binding.Ajax().Select("LoadMainDDL", "DataSearch"))
.HighlightFirstMatch(true)
.ClientEvents(events => events.OnChange("onMainDDL2Change"))
)
</td>
</tr>
<tr>
<td>
@(Html.Telerik().ComboBox()
.Name("SubDDL1")
.DataBinding(binding => binding.Ajax().Select("LoadSubDDL1", "DataSearch"))
.HighlightFirstMatch(true)
.ClientEvents(events => events.OnDataBinding("onSubDDL1DataBinding"))
)
</td>
<td>
@(Html.Telerik().ComboBox()
.Name("SubDDL2")
.DataBinding(binding => binding.Ajax().Select("LoadSubDDL2", "DataSearch"))
.HighlightFirstMatch(true)
.ClientEvents(events => events.OnDataBinding("onSubDDL2DataBinding"))
)
</td>
</tr>
<tr>
<td>
@Html.TextBox("Search1")
</td>
<td>
@Html.TextBox("Search2")
</td>
</tr>
<tr align="center">
<td colspan="4">
<input type="button" class="t-button button1" value="Search" id="btnSubmit" />
</td>
</tr>
</table>
<div id="result1">
</div>
<div id="result2">
</div>
PARTIAL VIEW - 示例
@model ...DataSearchModel
@(Html.Telerik().Grid(Model.dtResultSet)
.Name("Grid")
.Footer(false)
.Columns(columns =>
{
columns.Bound(o => o.Row[0]).Title("T_PRCL");
}))