gpt4 book ai didi

c# - MySQL 查询中的输入字符串格式不正确

转载 作者:行者123 更新时间:2023-11-29 01:04:48 25 4
gpt4 key购买 nike

在我的代码中,我连续多次调用数据库。但是,当我尝试从数据库中读取整数值时,出现以下错误:输入字符串的格式不正确。这是我的代码:

private int getNumberOfProjectsAssigned()
{
ArrayList staffProjects = new ArrayList();
int numberOfProjects = 0;
try
{
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection = new MySqlConnection(strConnection);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT id_project_pk FROM `test`.`staff_on_project` WHERE id_staff_pk = " + Convert.ToInt32(Session["CurrentUserID"]);
//SELECT idusers, first_name, last_name, job_title, code_quality, time_bonus,analysis_of_requirements FROM `test`.`users` WHERE security_level > 1;
connection.Open();

MySqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
int projectId = Convert.ToInt32(reader["id_project_pk"].ToString());
staffProjects.Add(projectId);
}
connection.Close();
foreach (int i in staffProjects)
{
command.CommandText = "SELECT still_active FROM `test`.`projects` WHERE idprojects = " + i;
//SELECT idusers, first_name, last_name, job_title, code_quality, time_bonus,analysis_of_requirements FROM `test`.`users` WHERE security_level > 1;
connection.Open();

reader = command.ExecuteReader();

while (reader.Read())
{
int projectId = Convert.ToInt32(reader["still_active"].ToString()); // Error Occurs Here
if(projectId == 1)
{
projectsStaffWorksOn.Add(projectId);
}
numberOfProjects = projectsStaffWorksOn.Count;
}
connection.Close();
}
}
catch { }
return numberOfProjects;
}

它在我在代码中标记的位置抛出错误。任何帮助将不胜感激!

最佳答案

still_active 的值之一不是有效整数 - 可能是空字符串?

关于c# - MySQL 查询中的输入字符串格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198823/

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