我有一个数据库名称"费用"我有4列(admno,receiptnumber,name,tutionfee)。这里的主键是"收据号码"。我输入了几个不同录取号码的录音。还有几个条目具有相同的入场号码。现在我只想打印我输入的最后一条记录。我怎么打印。我写了一段代码,但第一条记录正在打印。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
string str;
str = "select * from fees where admno='" + temp + "'";
SqlCommand com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
reader.Read();
TextBox1.Text = reader["admno"].ToString();
TextBox2.Text = reader["receiptnumber"].ToString();
TextBox3.Text = reader["name"].ToString();
TextBox4.Text = reader["tutionfee"].ToString();
答案 0 :(得分:3)
这应该这样做。添加参数以避免sql注入!
您需要按ID DESC
结尾订购,然后选择第一个(TOP1
)
string Command = "select TOP 1 * from fees where admno = @admno ORDER BY receiptnumber DSEC";
using (SqlConnection mConnection = new SqlConnection(ConnectionString))
{
mConnection.Open();
using (SqlCommand cmd = new SqlCommand(Command, mConnection))
{
cmd.Parameters.Add(new MySqlParameter("@admno", temp));
using (SqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
TextBox1.Text = reader["admno"].ToString();
TextBox2.Text = reader["receiptnumber"].ToString();
TextBox3.Text = reader["name"].ToString();
TextBox4.Text = reader["tutionfee"].ToString();
}
}
}
答案 1 :(得分:2)
假设收据号码是一个身份字段,您可以select top 1 * from fees order by receiptnumber desc