0

我正在测试一个将一些数据写入 SQL Server 2008 数据库的小程序。这是代码:

Dim MyConn As ADODB.Connection
Dim MyRecSet As ADODB.Recordset
Dim st1 As Integer

Set MyConn = New ADODB.Connection
MyConn.ConnectionString = "Provider=sqloledb;" & _
       "Data Source=xxx\SQLEXPRESS2;" & _
       "Initial Catalog=db;" & _
       "User Id=sa;" & _
       "Password=xxx"
MyConn.Open

st1 = 5
 MyConn.Execute("INSERT INTO steen (steen1) VALUES (st1)")

MyConn.Close

但是,我收到错误消息:列名“st1”无效。我不知道为什么会出现这个错误。当我将查询更改为此:

INSERT INTO steen (steen1) VALUES (5)

它完美地工作。

有谁知道我为什么会收到错误?

谢谢

4

1 回答 1

2

尝试:

MyConn.Execute("INSERT INTO steen (steen1) VALUES ("& st1 & ")")

或者:

st1 = 5
 Dim StrQuery As String
StrQuery = "INSERT INTO steen (steen1) VALUES ("& st1 & ")"


MyConn.Execute(StrQuery)
于 2012-12-05T16:02:58.643 回答