gpt4 book ai didi

c# - SQLDataReader 在使用 .Nest() 或 .Read() 时出现错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:02 26 4
gpt4 key购买 nike

我正在通过 SQLDataReader 从 MSSQL 中检索一些信息,但在调试它时我注意到在某些情况下,读取器会清除结果 View 并显示错误“枚举未产生结果”,请参见屏幕截图
Before Running passing Read(),
After passing read()


这是我的代码,错误发生在 getActiveUsers() 方法上。getDatabases() 工作得很好。 有人可以帮助我吗?干杯

 public partial class automation : System.Web.UI.Page
{
SqlConnection con;
static List<ActiveUsers> activeUsers = new List<ActiveUsers>();
protected void Page_Load(object sender, EventArgs e)
{
ASPxGridView1.DataSource = activeUsers.ToList();

}
public List<ActiveUsers> getDatabases()
{

//passing query
string SqlQuery = "SELECT [WorkspaceName],[MaConfig_Customers].Name FROM [MaConfig_CustomerDatabases] INNER JOIN [MaConfig_Customers] ON [MaConfig_CustomerDatabases].CustomerId = [MaConfig_Customers].CustomerId where [MaConfig_Customers].Status = 0";
//creating connection
string sqlconn = ConfigurationManager.ConnectionStrings["MaxLiveConnectionString"].ConnectionString;
con = new System.Data.SqlClient.SqlConnection(sqlconn);
var cmd = new SqlCommand(SqlQuery, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
List<ActiveUsers> results = new List<ActiveUsers>();
if (reader.Read())
{
while (reader.Read())
{
ActiveUsers company = new ActiveUsers();
company.DatabaseName = String.Format("{0}", reader["WorkspaceName"]);
company.ClientName = String.Format("{0}", reader["Name"]);
results.Add(company);
}
}
con.Close();
return results;
}

public void getActiveUsers()
{

activeUsers.Clear();
List<ActiveUsers> Databases= getDatabases();
SqlConnection conn = new SqlConnection();
string SqlQuery = "select [disabled], [ADMN_Users1].[Record_Id] ,[ADMN_Users].[User_Id] from admn_Users1 inner join [ADMN_Users] on [ADMN_Users1].[record_Id] = [ADMN_Users].[Record_Id] Where [disabled] & 0x2 = 0 ";

for (int i = 0;i < Databases.Count;i++)
{

conn.ConnectionString =
"Data Source=MAXSQLCLUS01;" +
"Initial Catalog=" + Databases[i].ToString()+";"+
"User id=sa;" +
"Password=Max1m1zer;";
var cmd = new SqlCommand(SqlQuery, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
int NumberOfUsersCounter = 0 ;
//TODO Select Enabled users
if (reader.Read())
{

while (reader.Read())
{
string user = String.Format("{0}", reader["User_Id"]);
//logic to remove system users

if (user.Equals("master", StringComparison.CurrentCultureIgnoreCase))
{


}
else
if (user.Equals("emailuser", StringComparison.CurrentCultureIgnoreCase))
{

}
else
if (user.Equals("webuser", StringComparison.CurrentCultureIgnoreCase))
{


}
else
{

NumberOfUsersCounter++;
}

}
ActiveUsers newEntry = new ActiveUsers();

newEntry.NumberActiveUsers = NumberOfUsersCounter.ToString();
newEntry.DatabaseName = Databases[i].DatabaseName.ToString();
newEntry.ClientName = Databases[i].ClientName.ToString();
activeUsers.Add(newEntry);

}
conn.Close();

//Add to ActiveUsers list


}



ASPxGridView1.AutoGenerateColumns = true;

ASPxGridView1.DataSource = activeUsers.ToList();
ASPxGridView1.DataBind();

}

protected void ASPxButton1_Click(object sender, EventArgs e)
{

getActiveUsers();
}

protected void btnExportExcel_Click(object sender, EventArgs e)
{
ASPxGridView1.DataBind();
ASPxGridViewExporter1.Landscape = true;
ASPxGridViewExporter1.FileName = "User Count Report";
ASPxGridViewExporter1.WriteXlsToResponse();

}
}

最佳答案

if (reader.Read())
{
while (reader.Read())
{
ActiveUsers company = new ActiveUsers();
company.DatabaseName = String.Format("{0}", reader["WorkspaceName"]);
company.ClientName = String.Format("{0}", reader["Name"]);
results.Add(company);
}
}

使用这个

if (reader.HasRows)
{
while (reader.Read())
{
ActiveUsers company = new ActiveUsers();
company.DatabaseName = String.Format("{0}", reader["WorkspaceName"]);
company.ClientName = String.Format("{0}", reader["Name"]);
results.Add(company);
}
}

你的if条件是错误的

if(reader.Read()) ==> is Wrong 

Read() 不是返回 bool 值

使用 HasRows 检查 SQLDataReader 中的行

关于c# - SQLDataReader 在使用 .Nest() 或 .Read() 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51420203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com