C# 如何将字符串转换为If-Else条件
我正在使用C创建一个验证 我的设想是这样的。我在数据库表中有一个数据,它的列名以int的数据类型结尾C# 如何将字符串转换为If-Else条件,c#,C#,我正在使用C创建一个验证 我的设想是这样的。我在数据库表中有一个数据,它的列名以int的数据类型结尾 ending_balance = 300 然后我有一个窗口窗体,带有文本框和执行按钮。当我在文本框中键入值时 txt_payment = 400; 验证将启动,因为期末余额小于txt_支付值300 这是我的查询和代码 private void btn_payment_Click(object sender, EventArgs e) { // Here's the connection
ending_balance = 300
然后我有一个窗口窗体,带有文本框和执行按钮。当我在文本框中键入值时
txt_payment = 400;
验证将启动,因为期末余额小于txt_支付值300
这是我的查询和代码
private void btn_payment_Click(object sender, EventArgs e)
{
// Here's the connection and query for selecting the specific column.
Data select = new Data();
select.Connection();
MySqlCommand cmd = new MySqlCommand("SELECT ending_balance FROM tblsalary_payments WHERE customer_id='"+customer_id+"' ORDER BY id DESC", select.connect);
MySqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows == true){
dr.Read();
if(txt_payment.Text < dr["ending_balance"].ToString()){
MessageBox.Show("The Payment Is Greater Than Ending Balance");
}
else{
... // Execute Query if the Validation Passed
}
}
dr.Close();
我的问题是,我不知道如何在字符串之间验证字符串。那是我的错误我试图将它们转换为布尔、整数、十进制等,但我仍然无法验证我的文本框
提前谢谢 您需要比较字符串中的数字,因此必须将这些字符串转换为数字。您可以使用int.Parse或double.Parse方法,具体取决于您必须将字符串转换为数字的类型
if(int.Parse(txt_payment.Text) < int.Parse(dr["ending_balance"].ToString())){
MessageBox.Show("The Payment Is Greater Than Ending Balance");
如注释所示,若数据库中有数字,则无需将其转换为字符串,然后再转换为数字
if(int.Parse(txt_payment.Text) < dr["ending_balance"]){
MessageBox.Show("The Payment Is Greater Than Ending Balance");
您需要比较字符串中的数字,因此必须将这些字符串转换为数字。您可以使用int.Parse或double.Parse方法,具体取决于您必须将字符串转换为数字的类型
if(int.Parse(txt_payment.Text) < int.Parse(dr["ending_balance"].ToString())){
MessageBox.Show("The Payment Is Greater Than Ending Balance");
如注释所示,若数据库中有数字,则无需将其转换为字符串,然后再转换为数字
if(int.Parse(txt_payment.Text) < dr["ending_balance"]){
MessageBox.Show("The Payment Is Greater Than Ending Balance");
使用
使用
您错过了文本格式验证:
// Ensure that the typed text can be converted to int
int paymentAmount;
if (!int.TryParse(txt_payment.Text, out paymentAmount))
{
MessageBox.Show("Invalid Format");
return;
}
那么剩下的看起来像:
if (paymentAmount < Convert.ToInt32(dr["ending_balance"]))
{
MessageBox.Show("The Payment Is Greater Than Ending Balance");
}
我认为这是Win Forms应用程序模型。如果是这样的话
有一个事件更适合数据输入验证:,请尝试使用它,而不是直接处理鼠标单击或键盘事件。您错过了文本格式验证:
// Ensure that the typed text can be converted to int
int paymentAmount;
if (!int.TryParse(txt_payment.Text, out paymentAmount))
{
MessageBox.Show("Invalid Format");
return;
}
那么剩下的看起来像:
if (paymentAmount < Convert.ToInt32(dr["ending_balance"]))
{
MessageBox.Show("The Payment Is Greater Than Ending Balance");
}
我认为这是Win Forms应用程序模型。如果是这样的话
有一个事件更适合数据输入验证:,尝试使用它,而不是直接处理鼠标单击或键盘事件。您在哪里尝试将其转换为int或decimal?在此处显示您使用的是小于比较,而您似乎应该使用大于。当您将代码转换为int时,代码必须正常工作,但由于您的条件错误,验证失败。您在哪里尝试将其转换为int或decimal?在此处显示您正在使用小于的比较,而看起来您应该使用大于。当您将代码转换为int时,代码必须正常工作,但由于您的条件错误,验证失败。如果数据库中的值已经是一个数字,则无需将其转换为文本并对其进行分析。只需将dr[ending_balance]强制转换为适当的类型。它应该是十进制的…@马克,你是说条件吗?如果是这样,则保持条件不变如果数据库中的值已经是一个数字,则不需要将其转换为文本并对其进行解析。只需将dr[ending_balance]强制转换为适当的类型。它应该是十进制的…@马克,你是说条件吗?如果是这样,请保持条件不变。谢谢。我将dr[]转换为。。。现在开始工作:上帝保佑!阿门:先生,谢谢。我将dr[]转换为。。。现在开始工作:上帝保佑!阿门: