检索数据库上的最新更新 [英] Retrieve last updated on Database

查看:81
本文介绍了检索数据库上的最新更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imports System.Data.SqlClient
Public Class ProdA
    Dim com As New SqlConnection
    Dim cmd As SqlCommand
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

    com.ConnectionString = "Data Source=" + Production.TextBox3.Text + ";Database=Database1;User Id=" + Production.TextBox1.Text + ";Password=" + Production.TextBox2.Text + ";"
            com.Open()


这是我使用与SQL SERVER建立连接的过程.我的数据库有一个名为STOCKS_Details的表,其列为fact1/fact2/fact3/total/DATE.因此,要进行计算,我需要将前一行DATE放入VB应用程序的变量中.问题是如何编写查询以将值检索到变量..

谢谢您


This is the procedure i''m using build the connection with my SQL SERVER. my DB has table called STOCKS_Details whose columns are fact1/fact2/fact3/total/DATE.So to do calculations i need to pick previous row DATE into a variable on a VB application. The Problem is how can I write the Query to retrieve value to the variable..

THANK YOU

推荐答案

Sharath2790!

1.如果您的表具有标识列,请使用

Hi Sharath2790!

1. If ur table have identity column use

select max(@@IDENTITY) from Images



2.如果您的表没有身份,则需要创建过程



2. If ur table doesn''t have identity u need to create procedure

Create Procedure Get_LastRow

(

    @Tname Varchar(50)

)

AS

BEGIN


EXECUTE ('DECLARE GETLAST CURSOR DYNAMIC FOR SELECT * FROM ' + @Tname)
--select date from the above query

OPEN GETLAST

FETCH LAST FROM GETLAST

CLOSE GETLAST

DEALLOCATE GETLAST

END



并执行该过程以获得类似
的结果



and execute that procedure to get the result like

EXEC Get_LastRow 'dbo.TableName'



如果有帮助,请接受解决方案!

谢谢
〜JK



Accept the solution if this is helpful!!

Thanx
~JK


最后更新/添加了吗?

1.对于最近的更新记录,
添加时间戳列
和火灾查询
last updated/added ?

1. for last updated record,
add timestamp column
and fire query
select * from tbl timestampCol=max(timestampCol)




2.最后添加 条记录,




2. for lastly added record,

--Insert statement then
select @@Identity


祝您编码愉快!
:)


Happy Coding!
:)


Select top 1 fact1,fact2,fact3,total,date from stock_details order by date desc.




现在将此查询编写为sqlcommand并将输出填充到数据表中.

然后获取每个数据表列的值并将其设置为变量.

我正在用C#编写示例代码.




Now write this query as sqlcommand and fill the output in datatable.

Then get value for each datatable column and set it to your variable.

i am writing sample code in c#.

foreach(DataRow dr in datatable)
{
int fact1=Convert.ToInt32(dr["fact1"].ToString());
}



在VB中



in VB

For Each dr As DataRow In datatable
    Dim fact1 As Integer = Convert.ToInt32(dr("fact1").ToString())
Next




更新了解决方案.

如果只需要最大日期"列,请尝试此




Updated solution.

If you need only max date column, then try this

Select max(Date) from stock_detail

s

然后

s

Then

Dim dt as datetime 
dt=Convert.ToDateTime(cmd.ExecuteS

calar());

calar());


这篇关于检索数据库上的最新更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆