在我的数据库中搜索数据[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我的工作在这里工作,但如果搜索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();

c# ms-access-2010 sql-like oledbcommand
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.