Mysql 如何返回数据读取器的值(ASP.NET MVC)
我想获取值[2]和值[4]以进行检查 如何获得价值 请帮忙 多谢各位 dr=comm.ExecuteReader();具有如下图像所示的值:Mysql 如何返回数据读取器的值(ASP.NET MVC),mysql,asp.net-mvc,database,return-value,Mysql,Asp.net Mvc,Database,Return Value,我想获取值[2]和值[4]以进行检查 如何获得价值 请帮忙 多谢各位 dr=comm.ExecuteReader();具有如下图像所示的值: 使用()返回IDisposable对象之前,请确保已处理掉IDisposable对象。否则,您可能会泄漏本机资源和内存(例如,您可能会过早耗尽数据库连接池) 您的代码无缘无故地定义了空的Odbc…对象 尽可能对所有IO使用async——当您使用ASP.NET时,这是受支持的(请注意,您不能对ADO.NET对象使用并发async,而可以在Windows上使
- 使用()返回
对象之前,请确保已处理掉IDisposable
对象。否则,您可能会泄漏本机资源和内存(例如,您可能会过早耗尽数据库连接池)IDisposable
- 您的代码无缘无故地定义了空的
对象Odbc…
- 尽可能对所有IO使用
——当您使用ASP.NET时,这是受支持的(请注意,您不能对ADO.NET对象使用并发async
,而可以在Windows上使用文件系统IO)async
公共异步任务索引()
{
const string connectionString=“Driver={MySQL ODBC 5.3 Unicode Driver};SERVER=localhost;Port=0000;DATABASE=ABC;UID=root;PASSWORD=123;OPTION=3;”;
//const string sql=“SELECT*FROM USER”;//在与DataReader
一起使用的查询中,切勿使用SELECT*
,因为您不知道哪些列将位于哪些位置。请始终使用显式列名(例如,如果表设计更改,则查询将中断)@Dai我已经添加了2个列名..但我不知道如何显示…我总是使用response.write(在使用asp.net MVC之前)来检查值..如何在我的index.cshtml(/Home/index)中显示?切勿使用响应。在ASP.NET MVC控制器操作方法中写入。使用视图模型对象将所有渲染数据返回到视图。我已更新了答案,以提供完整示例。
public ActionResult Index()
{
string sCurr = "";
string myConString = "Driver={MySQL ODBC 5.3 Unicode Driver}; SERVER=localhost;Port=0000;DATABASE=ABC;UID=root;PASSWORD=123;OPTION=3;";
OdbcConnection dbConnection = new OdbcConnection(myConString);
dbConnection.Open();
string sql = "SELECT * FROM USER";
OdbcConnection conn = null;
OdbcCommand comm = null;
OdbcDataReader dr = null;
conn = new OdbcConnection(myConString);
conn.Open();
comm = new OdbcCommand(sql, conn);
dr = comm.ExecuteReader();
if (dr != null)
{
while (dr.Read())
{
sCurr = (dr.GetValue(0).ToString());
}
}
return View();
}
public async Task<ActionResult> Index()
{
const string connectionString = "Driver={MySQL ODBC 5.3 Unicode Driver}; SERVER=localhost;Port=0000;DATABASE=ABC;UID=root;PASSWORD=123;OPTION=3;";
//const string sql = "SELECT * FROM USER"; // <-- don't do this. Always name your columns!
const string sql = "SELECT UserId, Created, UserType, etc FROM `USER`":
using( OdbcConnection c = new OdbcConnection( connectionString ))
using( OdbcCommand cmd = c.CreateCommand() )
{
await c.OpenAsync();
cmd.CommandText = sql;
using( OdbcDataReader rdr = await cmd.ExecuteReaderAsync() )
{
while( await rdr.ReadAsync() )
{
Int32 userId = rdr.GetInt32( 0 );
DateTime created = rdr.GetDateTime( 1 );
String userType = rdr.GetString( 2 );
// etc...
}
}
}
// etc...
}
class UserRow
{
Int32 UserId { get; set; }
DateTime Created { get; set; }
// etc...
}
public async Task<ActionResult> Index()
{
const string connectionString = "Driver={MySQL ODBC 5.3 Unicode Driver}; SERVER=localhost;Port=0000;DATABASE=ABC;UID=root;PASSWORD=123;OPTION=3;";
//const string sql = "SELECT * FROM USER"; // <-- don't do this. Always name your columns!
const string sql = "SELECT UserId, Created, UserType, etc FROM `USER`":
using( OdbcConnection c = new OdbcConnection( connectionString ))
using( OdbcCommand cmd = c.CreateCommand() )
{
await c.OpenAsync();
cmd.CommandText = sql;
using( OdbcDataReader rdr = await cmd.ExecuteReaderAsync() )
{
List<UserRow> list = new List<UserRow>();
while( await rdr.ReadAsync() )
{
list.Add( new UserRow() {
UserId = rdr.GetInt32( 0 ),
Created = rdr.GetDateTime( 1 ),
// etc
} );
}
return this.View( model: list );
}
}
// etc...
}
@model IReadOnlyList<UserRow>
<div>
<table>
<thead>
<tr>
<th>UserID</th>
<th>Created</th>
etc
</tr>
</thead>
<tbody>
@foreach( UserRow row in this.Model )
{
<tr>
<td>@row.UserId</td>
<td>@row.Created</td>
etc
</tr>
}
</tbody>
</table>
</div>