2013-10-24 66 views
-2

我想從SQL Server數據庫中選擇數據表中列的最後一個值。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 
      { 

      } 
     } 

並希望顯示在儀表的最後一個值。 這裏我沒有從我生成的查詢中獲取最後一個值。我不知道爲什麼。

回答

1

最好的和推薦的方法。

select top 1 * from test order by ID desc 
+0

Got的工作...... thnx @Sasidharan – SPandya

-1

SQL Server不支持LAST()語法。相反,你可以使用這裏所描述的另一種方法 - )http://www.w3schools.com/sql/sql_func_last.asp

+0

我不想要特定列,我想要每列的最後一個值 – SPandya

+0

您仍然可以執行所有列(select *)。此外,代碼需要定義「最後」的含義。如果您對最後插入的行感興趣,則需要維護「LastUpdated」列並按照select查詢中的順序進行排序。 SQL服務器不保證如何選擇行,除非查詢指定了ORDER BY。 – Jardalu

1

我覺得最後(不是SQL服務器

的功能,但是你可以通過使用我下面的方法我們得到的最後一排。試試這個

SELECT * FROM TableName WHERE PrimaryKeyId= (SELECT MAX(PrimaryKeyId) FROM TableName) 

或者嘗試另一種方式。

SELECT TOP 1 * FROM TableName ORDER BY PrimaryKeyId DESC 

爲您的主鍵確保設置身份。

+0

我正在嘗試從這個例子http://www.w3schools.com/sql/trysql.asp?filename=trysql_func_last&ss=-1 – SPandya

+0

好吧現在,你嘗試我的兩種方式,它肯定會得到最後一行的值。] –

+0

Working..thnx @ Ramesh – SPandya

相關問題