SQL 服务器中的最后一个值选择
本文关键字:选择 最后一个 服务器 SQL | 更新日期: 2023-09-27 18:36:50
我正在尝试从SQL Server数据库中选择数据表中列的最后一个值。
我的代码是
private void bind_chart()
{
// here i am using SqlDataAdapter for the sql server select query
SqlDataAdapter adp = new SqlDataAdapter("select last() * from Test", con);
// here am taking datatable
DataTable dt = new DataTable();
try
{
// here datatale dt is fill wit the adp
adp.Fill(dt);
// this string m catching in the stringbuilder class
// in the str m writing same javascript code that is given by the google.
// my data that will come from the sql server
// below code is same like as google's javascript code
str.Append(@" <script type='text/javascript'>
google.load('visualization', '1', {packages:['gauge']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Label');
data.addColumn('number', 'Value');");
// inside the below line dt.Rows.Count explain that
// how many rows comes in dt or total rows
str.Append("data.addRows(" + dt.Rows.Count + ");");
Int32 i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
// i need this type of output " data.setValue(0, 0, 'Memory'); so on in the first line so for this
//m using i for the 0,1& 2 and so on . and after this i am writting zero and after this student_name using datatable
str.Append("data.setValue( " + i + "," + 0 + "," + "'" + dt.Rows[i]["x"].ToString() + "');");
// i need this type of output " data.setValue(0, 1, 80);
//so on in the first line so for this
//m using i for the 0,1& 2 and so on . and after this i am writting zero and after this percentage using datatable
str.Append("data.setValue(" + i + "," + 1 + "," + dt.Rows[i]["y"].ToString() + ") ;");
// str.Append("['" + (dt.Rows[i]["student_name"].ToString()) + "'," + dt.Rows[i]["average_marks"].ToString() + "],");
}
str.Append("var chart = new google.visualization.Gauge(document.getElementById('chart_div'));");
// in the below line i am setting the height and width of the Gauge using your own requrirement
str.Append(" var options = {width: 800, height: 300, redFrom: 90, redTo: 100,");
// str.Append(" var chart = new google.visualization.BarChart(document.getElementById('chart_div'));");
str.Append("yellowFrom: 75, yellowTo: 90, minorTicks: 5};");
str.Append("chart.draw(data, options);}");
str.Append("</script>");
lt.Text = str.ToString().TrimEnd(',');
}
catch
{
}
finally
{
}
}
并希望在仪表上显示最后一个值。在这里,我没有从生成的查询中获取最后一个值。我不知道为什么。
最佳和推荐的方法。
select top 1 * from test order by ID desc
我认为 Last() 不是 sql 服务器的功能
但是您可以使用我的以下方式获得最后一行.试试这个
SELECT * FROM TableName WHERE PrimaryKeyId= (SELECT MAX(PrimaryKeyId) FROM TableName )
或者,尝试其他方式。
SELECT TOP 1 * FROM TableName ORDER BY PrimaryKeyId DESC
确保为主键设置标识。
SQL Server 不支持 LAST() 语法。相反,您可以使用此处描述的另一种方法 - http://www.w3schools.com/sql/sql_func_last.asp