2015-04-23 128 views
0

我想將一些Wincc標籤值插入到SQL服務器中。爲了這個,我首先定義的SQL連接,我已經測試了像一個簡單的SQL插入標籤粗略值:如何在Simatic WinCC中使用變量靈活的VB腳本

strSQL = "INSERT INTO Test (T1,T2) Values (3,4)" 

我看到了,我可以在這個SQL中使用變量CMD

const d = 5 
strSQL = "INSERT INTO Test (T1,T2) Values (" & d & ",4)" 

應該如何我使用Wincc標籤而不是這個變量?

回答

-1

嘗試使用帶參數的參數化SQL查詢並將變量寫入此參數。並檢查你的變量和數據字段在sql表中的數據類型 - 它必須是相同的。這是解決您的問題的唯一可能方式。

這是C++例如:

ADOQuery1->SQL->Add("INSERT INTO Your_table (field1, field2, field3, field4, field5, field6, field7, field8) VALUES (:param1,'"+Name_text->Text+"', '"+Type_text->Text+"', :param5, :param4, :param3, :param2, '"+Comment_text->Text+"')"); 
ADOQuery1->Parameters->ParamByName("param5")->Value= StrToFloat(edit1_text->Text); 
ADOQuery1->Parameters->ParamByName("param4")->Value= StrToFloat(edit2_text->Text); 
ADOQuery1->Parameters->ParamByName("param3")->Value= StrToFloat(edit3_text->Text); 
ADOQuery1->Parameters->ParamByName("param2")->Value= StrToFloat(edit4_text->Text); 
ADOQuery1->Parameters->ParamByName("param1")->Value= StrToInt(edit5_text->Text); 
ADOQuery1->ExecSQL(); 

您可以改寫像這樣的VBS。

看也在這裏(好像VBS代碼): How do I create a parameterized SQL query? Why Should I?

0

我可以和你分享一些代碼,我使用WinCC flexible 2008用於插入到數據庫中的簡單記錄afeter由工廠完成任何配方。

'------------------------------------------------------------ 
Dim sServer,sDataBaseName ,sConn,oConn,oRS, FetchData 
Dim sUserName,sPassWord 
Dim adOpenKeyset, adLockOptimistic, adCmdTable 
Dim mRecordstr, tableName 
Dim field 


Dim loc_PRESSURE 
Dim loc_ENDDATETIME 
Dim loc_RECEIPE 
Dim loc_BEGINDATETIME 
Dim loc_USER 
Dim loc_TEMPERATURE 

loc_USER = SmartTags("DatabaseSQL\var_USER") 
loc_BEGINDATETIME = CDate(SmartTags("DatabaseSQL\var_BEGINDATETIME")) 
loc_ENDDATETIME = CDate(SmartTags("DatabaseSQL\var_ENDDATETIME")) 
loc_RECEIPE = SmartTags("DatabaseSQL\var_RECEIPE") 
loc_PRESSURE = SmartTags("DatabaseSQL\var_PRESSURE") 
loc_TEMPERATURE = SmartTags("DatabaseSQL\var_TEMPERATURE") 



sServer = "192.168.40.3" 'e.g 
sDataBaseName = "DBPLC" 
sUserName = "USER"'INSERT USER NAME ENABLED IN DATABASE 
sPassWord = "SECRETPASSWORD" ' 
tableName = "TABLE_PLC" 

adOpenKeyset = 1 
adLockOptimistic = 3 
adCmdTable = 2 

sConn="DRIVER={SQL Server};SERVER=" & sServer & ";DATABASE=" & sDataBaseName & ";Encrypt=NO;" 
Set oConn = CreateObject("ADODB.Connection") 
oConn.CommandTimeout = 36000 
oConn.Open sConn, sUserName, sPassWord 


Set FetchData = CreateObject("ADODB.Recordset") 

'FetchData.open "SELECT * FROM TABLE_PLC", oConn, 3 
FetchData.Open tableName, oConn, adOpenKeyset, adLockOptimistic, adCmdTable 

FetchData.AddNew 

FetchData.Fields("USER").Value = loc_USER 
FetchData.Fields("BEGINDATETIME").Value = loc_BEGINDATETIME 
FetchData.Fields("ENDDATETIME").Value = loc_ENDDATETIME 
FetchData.Fields("RECEIPE").Value = loc_RECEIPE 
FetchData.Fields("PRESSURE").Value = loc_PRESSURE 
FetchData.Fields("TEMPERATURE").Value = loc_TEMPERATURE 


FetchData.Update 

FetchData.Close 
oConn.Close 
+0

本例中的WinCC變量保存在名爲「DatabaseSQL」的文件夾下。 – RammRras