如何从一个表中选择值并插入到另一个表VB.NET中 [英] How to select values from one table and insert into another table VB.NET

查看:87
本文介绍了如何从一个表中选择值并插入到另一个表VB.NET中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语法错误,'SELECT'附近语法错误



表名abc

费用:

col数据类型



日期日期时间

a navarchar(最大)



我尝试了什么:



syntax error, Incorrect syntax near 'SELECT'

Table Name abc
Feilds:
col DataType

Date Datetime
a navarchar(max)

What I have tried:

This is my code. Please do needfull


 Dim regDate As Date = Date.Now
        Dim strDate As String = regDate.ToString("MM/dd/yyyy")
        sqlconn = New SqlConnection(str)
        sqlconn.Open()
        Try
            cmd = New SqlCommand("INSERT INTO abc (Date,a) values('" & regDate.ToString("MM/dd/yyyy") & "',SELECT sum(StockValue) from StockValue)", sqlconn)
            cmd.ExecuteNonQuery()
            MsgBox("Your Data Saved Succesfully")


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sqlconn.Close()

推荐答案

基于发布的代码,我认为SELECT ...方法更合适,因为应用程序需要表中的值。见下文。



Based on the posted code, I think the SELECT ... approach is more appropriate since the application is expecting a value from the table. See below.

cmd = New SqlCommand("INSERT INTO abc (Date,a) SELECT '" & regDate.ToString("MM/dd/yyyy") & "', sum(StockValue) from StockValue", sqlconn)





发布的代码可能不受影响到SQL注入,但你应该尝试使用参数化查询 SqlCommand.Parameters Property(System.Data.SqlClient) [ ^ ]


这篇关于如何从一个表中选择值并插入到另一个表VB.NET中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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