我的工作在这里工作,但如果搜索1显示40而不是900,这是我的工作:
command.CommandType = CommandType.Text;
command.CommandText = ("SELECT * FROM Computation WHERE Transaction_ID LIKE '" + textBox1.Text.ToString() + "%'");
command.Connection = connection;
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
textBox2.Text = (String.Format("{0000,0:N2}", Int32.Parse(reader["Total_Bill"].ToString())));
}
connection.Close();
答案 0 :(得分:3)
当我写 in my comment 时,我不明白为什么你在这里使用LIKE
,但你可以ExecuteScalar
这在你的情况下是完美的。
执行查询,并返回第一行的第一列 查询返回的结果集。
像;
command.CommandType = CommandType.Text;
command.CommandText = ("SELECT Total_Bill FROM Computation WHERE Transaction_ID = @ID");
command.Parameters.AddWithValue("@ID", textBox1.Text);
command.Connection = connection;
try
{
connection.Open();
textBox2.Text = (String.Format("{0000,0:N2}", command.ExecuteScalar()));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
connection.Close();