iFIX SQLServer数据库读写

‘数据库链接字符串

Const CONSTR = “Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ceshiku;Data Source=WIN-M2JO4N9DQ4O”

 

Dim Conn As ADODB.Connection

Dim Rs As ADODB.Recordset

Dim SQL As String

 

‘读取数据

Private Sub CommandButton1_Click()

Set Conn = New ADODB.Connection

Conn.ConnectionString = CONSTR

Conn.Open

SQL = “SELECT * FROM ceshibiao ORDER BY ID”

Set Rs = New ADODB.Recordset

Rs.Open SQL, Conn

Do While Not Rs.EOF

MsgBox Rs.Fields(“ID”) & Rs.Fields(“Value1”)

Rs.MoveNext

Loop

Rs.Close

Set Rs = Nothing

Conn.Close

Set Conn = Nothing

SQL = vbNullString

End Sub

‘添加数据

Private Sub CommandButton2_Click()

Set Conn = New ADODB.Connection

Conn.ConnectionString = CONSTR

Conn.Open

 

‘SQL = “INSERT INTO ceshibiao(ID,DataTime1,Value1,Value2) VALUES (,null,123.0,456.0)”

‘ID定义的是自增关键词,使用时应注意不予赋值,上句语法错误,DataTime1为DataTime类型,设置中允许null赋值

Dim t As String

t = Msecond()

SQL = “INSERT INTO ceshibiao VALUES (getdate(),123.0,456.0)”

 

Dim cmd As ADODB.Command

Set cmd = New ADODB.Command

cmd.ActiveConnection = Conn

cmd.CommandType = adCmdText

cmd.CommandText = SQL

cmd.Execute

 

Conn.Close

Set Conn = Nothing

 

End Sub

 

徐大军

发表评论

电子邮件地址不会被公开。 必填项已用*标注