简体   繁体   English

使用C#计算其中包含特定字符串变量的mysql数据库行的数量并将其显示为int时出错

[英]Error using C# to count the number of mysql database rows with specific string varibles in them and display them as int

The code I am writing is to check how many employees are going to lunch at the same time. 我正在编写的代码是要检查有多少员工同时要吃午餐。 The information is in a MySQL table named: "edata" in the column named: "Lunchtime", and are stored as a string such as "4:00pm-5:00pm". 该信息位于名为“ Lunchtime”的列中的名为“ edata”的MySQL表中,并以字符串形式存储,例如“ 4:00 pm-5:00pm”。

Name   |   Lunchtime
------------------------
Paul       4:00pm-5:00pm
Mike       4:00pm-5:00pm
Aaron      1:00pm-2:00pm

The ultimate goal is to count the number of shifts that are chosen by the "lunchTimes.Text" variable and display how many employees selected the shift as an int named: "count". 最终目标是计算“ lunchTimes.Text”变量选择的轮班数量,并显示多少员工选择了该轮班作为名为“ count”的int。

ie Program user selects the "4:00pm-5:00pm" option, and the program returns "2" since 2 employees have selected that time slot. 即,程序用户选择“ 4:00 pm-5:00pm”选项,由于有2名员工选择了该时间段,因此程序返回“ 2”。

private void select_btn_Click(object sender, EventArgs e)
    {

        shiftLunch = lunchTimes.Text;

        string constring = "datasource=127.0.0.1;port=3306;username=root;password=password";
        string Query = "select count(Lunchtime) from database.edata where Lunchtime= '"+shiftLunch+"';";

        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);

        conDataBase.Open();
        Int32 count = (Int32)cmdDataBase.ExecuteScalar();


        Console.WriteLine(count);

Currently I am getting this error: 目前,我收到此错误:

"An unhandled exception of type 'System.InvalidCastException' occurred in First_cSharp_App.exe Additional information: Specified cast is not valid." “在First_cSharp_App.exe中发生了类型为'System.InvalidCastException'的未处理异常。附加信息:指定的强制转换无效。”

At this line: 在这一行:

Int32 count = (Int32)cmdDataBase.ExecuteScalar();

I'm new to this so I apologize for any confusion. 我对此并不陌生,因此给您带来的任何困惑我深表歉意。

This is because ExecuteScalar returns object which couldn't explicitly converted to Int32 at Runtime. 这是因为ExecuteScalar返回的object在运行时无法明确转换为Int32

The proper way will be doing the following: 正确的方法是执行以下操作:

int count = (int)(cmdDataBase.ExecuteScalar());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将图像添加到数据库,然后使用C#在datagridview中显示它们? - How to add images to database and then display them in datagridview using C#? C# MySql ASP.NET 从数据库中的行中获取项目并将它们放入 Z4E9F7F44562A881FE61E0ZBFFE0 - C# MySql ASP.NET Get items from rows in the database and put them in a gridview 使用Linq数据库C#显示特定行并在GridView中更新 - Display specific rows and update in GridView using Linq database c# 如何将 Select 不同的记录然后在 C# Access 数据库中统计它们? - How to Select Distinct records then count them in C# Access database? 使用c#计算excel中的行数 - count number of rows in excel using c# C#我正在使用字符串数组,我希望使用“ foreach”显示它们的列表 - C# I'm using string arrays, and I was hoping to use a “foreach” to Display a list of them 如何获取字符串的特定部分并将其分配给c#中的变量? - How to get specific parts of string and assign them to variables in c#? C# 从 MySQL 计数行并显示到 datagridview - C# Count Rows from MySQL and display to datagridview 如何使用C#获取文档的特定HTML元素并隐藏/显示它们等 - How to get at specific HTML elements of a document using C# and Hide them/Show them etc 在C#中从MySQL数据库获取行数 - Getting count of rows from MySQL database in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM