ASP.NET Razor拆分记录并增加一个

时间:2015-01-28 19:48:29

标签: sql asp.net razor

在我的代码中我试图在文本框中显示我数据库中最后一个记录之后的记录。例如,如果我的最后一个记录是A560,我想显示A561。为了实现这一点,我知道我必须拆分记录然后操纵它,但我没有运气。这是数据库的样子:

Point_ID    Project No.    Project Manager    Comments
A558        1304           Oscar Duran        Found destroyed
A559        1304           Oscar Duran        Helicopter access
A560        1356           Julio Bravo        Airport parking lot

到目前为止,这是我的代码:

@{  
Layout = "~/_Layout.cshtml";

var db = Database.Open("ControlPoints");                                                    
var SelectCommand = "SELECT * FROM( SELECT TOP 5 * FROM AllControlMergedND WHERE Point_ID LIKE 'A___' ORDER BY Point_ID DESC )AS BaseData Order BY Point_ID ASC";                       
var SearchTerm = "";                                                                        

if(!Request.QueryString["SearchCP"].IsEmpty() ){                                            
    SelectCommand = "SELECT * FROM AllControlMergedND WHERE Point_ID = @0";                 
    SearchTerm = Request.QueryString["SearchCP"];                                           
}

if(!Request.QueryString["SearchProject"].IsEmpty() ){                                       
    SelectCommand = "SELECT * FROM AllControlMergedND WHERE [Project Used on] LIKE @0";
    SearchTerm = "%" + Request["SearchProject"] + "%";
}

var SelectData = db.Query(SelectCommand, SearchTerm);                                       
var grid = new WebGrid(source: SelectData, rowsPerPage: 10);                                
}

@{  
Validation.RequireField("Point_ID", " Required");                                           
Validation.RequireField("ProjectNo", " Required");                                          
Validation.RequireField("ProjectManager", " Required");

var Point_ID = "";                                                                          
var ProjectNo = "";                                                                         
var ProjectManager = "";

if(IsPost && Validation.IsValid() ){                                                        
    Point_ID = Request.Form["Point_ID"];
    ProjectNo = Request.Form["ProjectNo"];
    ProjectManager = Request.Form["ProjectManager"];
    db = Database.Open("ControlPoints");
    var InsertCommand = "INSERT INTO AllControlMergedND ([Point_ID], [Project No.], [Project Manager]) VALUES(@0, @1, @2)";
    db.Execute(InsertCommand, Point_ID, ProjectNo, ProjectManager);                                    

}


var SelectLastCP = "SELECT TOP 1 Point_ID FROM AllControlMergedND WHERE Point_ID LIKE 'A___' ORDER BY Point_ID DESC"; 
var SelectData2 = db.QuerySingle(SelectLastCP);                                             
var SuggestedPoint_ID = SelectData2.Point_ID;

}

<h2>Airborne Imaging Control Points Database</h2><br/><br/>                                 
    <form method="get">
        <fieldset>                                                                          
            <legend>Search Criteria</legend>                                                
            <div>
                <p><label for="SearchCP">Control Point ID:</label>                          
                <input type="text" name="SearchCP" value="@Request.QueryString["SearchCP"]" /> 
                <input type="submit" value="Search"/></p>                                   
            </div>
            <div>
                <p><label for="SearchProject">Project:</label>                              
                <input type="text" name="SearchProject" value="@Request.QueryString["SearchProject"]" />
                <input type="Submit" value="Search" /></p>
            </div>
        </fieldset>
    </form>
    <div>
        @grid.GetHtml(                                                                      
            tableStyle: "grid",                                                             
            headerStyle: "head",                                                            
            alternatingRowStyle: "alt",                                                     
            columns: grid.Columns(
                grid.Column("Point_ID"),
                grid.Column("Project No."),
                grid.Column("Project Used on"),
                grid.Column("WGS84 Lat"),
                grid.Column("WGS84 Long"),
                grid.Column("Ellips_Ht"),
                grid.Column("Project Manager"),
                grid.Column("Comments")
            )
        )
        <br/><br/>
    </div>

    <form method="post">                                                                    
        <fieldset>
            <legend>Create Control Point(s)</legend>
            <p><label for="Point_ID">Point ID:</label>
            <input type="text" name="Point_ID" value="@SuggestedPoint_ID" />
            @Html.ValidationMessage("Point_ID")</p>                                        

            <p><label for="ProjectNo">Project No:</label>
            <input type="text" name="ProjectNo" value="@Request.Form["ProjectNo"]" />
            @Html.ValidationMessage("ProjectNo")</p>                                        

            <p><label for="ProjectManager">Project Manager:</label>
            <input type="text" name="ProjectManager" value="@Request.Form["ProjectManager"]" />
            @Html.ValidationMessage("ProjectManager")</p>                                   

            <p><input type="submit" name="ButtonConfirm" value="Confirm" /></p>
        </fieldset>
    </form>

如您所见,我能够做的就是在文本框中显示我的数据库的最后一条记录,在本例中为A560。变量&#39; SuggestedPoint_ID&#39;拿着那个记录。我尝试过转换数据类型,但没有成功。任何帮助将不胜感激。

更新

我需要做的是做以下事情。拆分A560分两部分&#39; A&#39;和&#39; 560&#39;。然后增加&#39; 560&#39;一个人获得&#39; 561&#39;并最终附上&#39; A&#39;再次来到&#39; 561&#39;为了获得下一个增量&#39; A561&#39;

1 个答案:

答案 0 :(得分:1)

如果你想转换&#34; A560&#34;以int为例,然后由于你没有有效的数字而无法工作。需要删除。

var SuggestedPoint_ID = SelectData2.Point_ID.Replace("A", "");

这不是我推荐的方式,因为A可能是AA或B或ZZZZ之类的东西。我的观点是,您需要描述您需要什么才能更好地解决您的问题。

<强>更新

var source = "A560";
var lhs = source.Substring(0, 1);
var tmp = source.Replace(lhs, "");
int rhs;
if(int.TryParse(tmp, out rhs))
{
    rhs++;
}
var result = string.Format("{0}{1}", lhs, rhs);