UMBRACO cshtml 显示来自 microsoft SQL server 表的数据

UMBRACO cshtml display data from microsoft SQL server table
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我继承了一个 Umbraco 4.9 站点,需要显示来自 SQL 数据库的一些数据.我使用 cshtml 文件创建了一个宏,但它在创建 sqldataconnection 时一直失败.

这是代码.

@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines
@using System.Xml.Linq
@using System.Text
@using System.Data
@using System.Data.SqlClient

@{
SqlConnection sqlConn = new SqlConnection(ConnectionString);
string sql = string.Format("SELECT * FROM [mydb] ");

}

<div class="park-list">
<h2>Parking List</h2>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <th>Date</th>
    </tr>
    <tr>
        <th>Name</th>
    </tr>
</table>
</div>

任何人都可以指出我正确的方向.谢谢

解决过程1

我建议创建一个辅助类或库,并从 razor 脚本中调用它.您可以简单地将该类添加到 App_Code 目录或在本地创建一个新项目并上传 dll.

也就是说,您可以使用像 PetaPoco 这样的轻量级工具:https : //github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs.将该文件添加到您的 App_Code 中,然后类似的操作可能会起作用:

@{
   var myDb = new PetaPoco.Database(connectionstringfromwebconfig);
   var someResult = myDb.Fetch("SELECT * FROM MyTable WHERE SomeColumn=@0", "Some value");
} 

<ul>
@foreach(var row in someResult)
{
 <li>
 @row.SomeOtherColumn 
 </li>
}
</ul> 

我在这里找到了这个代码:http : //our.umbraco.org/forum/developers/razor/31685-Razor-ans-SQL-commands并且还有其他代码可能在线程的底部有所帮助.

I have inherited an Umbraco 4.9 site and need to display some data from an SQL database. I have created a macro using a cshtml file but it keeps failing on the creation of the sqldataconnection.

Here is the code.

@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines
@using System.Xml.Linq
@using System.Text
@using System.Data
@using System.Data.SqlClient

@{
SqlConnection sqlConn = new SqlConnection(ConnectionString);
string sql = string.Format("SELECT * FROM [mydb] ");

}

<div class="park-list">
<h2>Parking List</h2>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <th>Date</th>
    </tr>
    <tr>
        <th>Name</th>
    </tr>
</table>
</div>

Can anyone point me in the right direction. Thanks

Solutions1

I would recommend creating a helper class or library and call that from the razor script. You could simply add the class to the App_Code directory or create a new project locally and upload the dll.

That said, you could use something lightweight like PetaPoco: https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs. Add that file to your App_Code and then something like this might work:

@{
   var myDb = new PetaPoco.Database(connectionstringfromwebconfig);
   var someResult = myDb.Fetch("SELECT * FROM MyTable WHERE SomeColumn=@0", "Some value");
} 

<ul>
@foreach(var row in someResult)
{
 <li>
 @row.SomeOtherColumn 
 </li>
}
</ul> 

I found this code here: http://our.umbraco.org/forum/developers/razor/31685-Razor-ans-SQL-commands and there is other code that may help at the bottom of the thread.

转载于:https://stackoverflow.com/questions/15237849/umbraco-cshtml-display-data-from-microsoft-sql-server-table

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入