【问题标题】:ASP.NET C# equivalent of vbScript/VB LEFT in the exampleASP.NET C# 等效于示例中的 vbScript/VB LEFT
【发布时间】:2012-07-01 21:04:29
【问题描述】:

我继承了要更新的 Web 应用程序的 1 个页面,并且旧的经典 ASP 版本大量使用了 LEFT、RIGHT 和 MID 函数。更新后的应用程序使用 C# ASP.NET。有没有办法导入 VB 的 left、right 和 mid 函数或模拟这些函数的函数。

如果字符串比 C# 下的长度参数短,C# 子字符串函数似乎会返回错误。 (不要取笑我的 C# 代码,我几乎不使用 C#)。填写所需的代码会很棒

<%@ Page language="c#" %>
<%@ Import namespace="Microsoft.VisualBasic"%>

<script runat="server" language="C#">

protected virtual string Auth(string uid)
{
String xml;

if (!String.IsNullOrEmpty(Request["uid"]))
{

System.Data.DataTable dtTable = new System.Data.DataTable();


System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
conn.Open();

string query = "SELECT id, blah, blah from users WHERE left(secretkey,30)='" + Request["uid"] + "'"; <--need to know how to use left function here

blah blah blah...

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>blah</title>
</head>
<body>
<form id="Form1" method="post" runat="server"><% Page_Load(null, null); %></form>
</body>
</html>

【问题讨论】:

  • 您使用的左侧函数是 SQL,而不是 c#。
  • 那么你所期望的......如果它更短那么应该发生什么?
  • Comecme 是正确的。我不需要担心 C# 中的 left,我可以在 sql 查询中使用 LEFT 函数。请添加它作为答案,以便我可以给你信用
  • (这不会解决您的问题,但是)您应该使用参数化查询。例如。 dotnetperls.com/sqlparameter

标签: c# asp.net html


【解决方案1】:

就像我在关于 wuestion 的评论中已经说过的,你使用的是 SQL 的 left 函数,所以你使用的是 vb 还是 c# 都没关系。

【讨论】:

    【解决方案2】:

    来自我评论中的链接问题,

    Left()       = "string".Substring(0, length)
    Right()      = "string".Substring("string".Length - desiredLength)
    Mid()        = "string".Substring(start, length)
    

    【讨论】:

      【解决方案3】:

      第一个问题是在这个例子中你根本没有在 C# 中调用 left。

      string query = "SELECT id, blah, blah from users WHERE left(secretkey,30)='" + Request["uid"] + "'";

      您在 sql server 中使用 left 函数,这就是为什么您在网页中没有收到任何错误,请参阅 http://msdn.microsoft.com/en-us/library/ms177601.aspx

      第二个问题是密钥在表中,因此除非您将其值检索到客户端,否则您永远无法在其上使用 VB 的 left 函数

      所以我假设这只是一个非常糟糕的例子,而你正在寻找更像这样的东西。

      var secretkey = "ABCDEFGHIJKLMNOP"

      string query = "Select blah, blah from users where secretkey=" + secretkey.Substring(0,30)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-11
        • 2013-09-06
        相关资源
        最近更新 更多