home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / vbsql / rpc.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-04-03  |  12.5 KB  |  371 lines

  1. VERSION 2.00
  2. Begin Form PrimaryWindow 
  3.    Caption         =   "Remote Stored Procedure"
  4.    Height          =   6765
  5.    Icon            =   0
  6.    Left            =   150
  7.    LinkMode        =   1  'Source
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   6075
  10.    ScaleWidth      =   9060
  11.    Top             =   390
  12.    Width           =   9180
  13.    Begin VBSQL VBSQL1 
  14.       Caption         =   "VBSQL1"
  15.       Height          =   255
  16.       Left            =   5280
  17.       Top             =   120
  18.       Visible         =   0   'False
  19.       Width           =   855
  20.    End
  21.    Begin TextBox RESULT_FIELD 
  22.       FontBold        =   -1  'True
  23.       FontItalic      =   0   'False
  24.       FontName        =   "Courier"
  25.       FontSize        =   9.75
  26.       FontStrikethru  =   0   'False
  27.       FontUnderline   =   0   'False
  28.       Height          =   2430
  29.       Left            =   120
  30.       MultiLine       =   -1  'True
  31.       ScrollBars      =   3  'Both
  32.       TabIndex        =   3
  33.       Text            =   "Text1"
  34.       Top             =   3600
  35.       Width           =   8460
  36.    End
  37.    Begin Frame Frame2 
  38.       Caption         =   "Procedure Sampler"
  39.       Height          =   1575
  40.       Left            =   120
  41.       TabIndex        =   11
  42.       Top             =   1680
  43.       Width           =   8895
  44.       Begin TextBox PARAMVALUE_FIELD 
  45.          Height          =   285
  46.          Left            =   1800
  47.          TabIndex        =   15
  48.          Text            =   "Text1"
  49.          Top             =   1080
  50.          Width           =   6975
  51.       End
  52.       Begin TextBox PARAMNAME_FIELD 
  53.          Height          =   285
  54.          Left            =   1800
  55.          TabIndex        =   16
  56.          Text            =   "Text2"
  57.          Top             =   720
  58.          Width           =   6975
  59.       End
  60.       Begin ComboBox PROCNAME_COMBO 
  61.          Height          =   300
  62.          Left            =   1800
  63.          TabIndex        =   17
  64.          Top             =   360
  65.          Width           =   2895
  66.       End
  67.       Begin Label Label7 
  68.          Caption         =   "Parameter &Values:"
  69.          Height          =   255
  70.          Left            =   120
  71.          TabIndex        =   14
  72.          Top             =   1080
  73.          Width           =   1815
  74.       End
  75.       Begin Label Label6 
  76.          Caption         =   "Para&meter Names:"
  77.          Height          =   255
  78.          Left            =   120
  79.          TabIndex        =   13
  80.          Top             =   720
  81.          Width           =   1815
  82.       End
  83.       Begin Label Label3 
  84.          Caption         =   "&Procedure Name:"
  85.          Height          =   255
  86.          Left            =   120
  87.          TabIndex        =   12
  88.          Top             =   360
  89.          Width           =   1575
  90.       End
  91.    End
  92.    Begin CommandButton SEND_QUERY_BUTTON 
  93.       Caption         =   "E&xecute Proc"
  94.       Enabled         =   0   'False
  95.       Height          =   375
  96.       Left            =   6360
  97.       TabIndex        =   1
  98.       Top             =   720
  99.       Width           =   1575
  100.    End
  101.    Begin CommandButton MAKE_PROC_BUTTON 
  102.       Caption         =   "&Make Test Proc"
  103.       Enabled         =   0   'False
  104.       Height          =   375
  105.       Left            =   6360
  106.       TabIndex        =   0
  107.       Top             =   240
  108.       Width           =   1575
  109.    End
  110.    Begin Frame Frame1 
  111.       Caption         =   "Procedure qualifiers (optional)"
  112.       Height          =   1455
  113.       Left            =   120
  114.       TabIndex        =   4
  115.       Top             =   120
  116.       Width           =   5055
  117.       Begin TextBox OWNER_FIELD 
  118.          Height          =   285
  119.          Left            =   2040
  120.          TabIndex        =   10
  121.          Text            =   "Text3"
  122.          Top             =   1080
  123.          Width           =   2535
  124.       End
  125.       Begin TextBox DBNAME_FIELD 
  126.          Height          =   285
  127.          Left            =   2040
  128.          TabIndex        =   8
  129.          Text            =   "Text2"
  130.          Top             =   720
  131.          Width           =   2535
  132.       End
  133.       Begin ComboBox SERVER_COMBO 
  134.          Height          =   300
  135.          Left            =   2040
  136.          TabIndex        =   6
  137.          Top             =   360
  138.          Width           =   2535
  139.       End
  140.       Begin Label Label5 
  141.          Caption         =   "&Owner:"
  142.          Height          =   255
  143.          Left            =   120
  144.          TabIndex        =   9
  145.          Top             =   1080
  146.          Width           =   735
  147.       End
  148.       Begin Label Label4 
  149.          Caption         =   "&Database Name:"
  150.          Height          =   255
  151.          Left            =   120
  152.          TabIndex        =   7
  153.          Top             =   720
  154.          Width           =   1455
  155.       End
  156.       Begin Label Label1 
  157.          Caption         =   "Remote &Server:"
  158.          Height          =   255
  159.          Left            =   120
  160.          TabIndex        =   5
  161.          Top             =   360
  162.          Width           =   1455
  163.       End
  164.    End
  165.    Begin Label Label2 
  166.       Caption         =   "&Results:"
  167.       Height          =   255
  168.       Left            =   120
  169.       TabIndex        =   2
  170.       Top             =   3360
  171.       Width           =   735
  172.    End
  173.    Begin Menu CONNECT_MENU 
  174.       Caption         =   "&Connection"
  175.       Begin Menu LOGIN_MENU 
  176.          Caption         =   "&Login"
  177.       End
  178.       Begin Menu DATABASE_MENU 
  179.          Caption         =   "Change &Database"
  180.       End
  181.       Begin Menu EXIT_MENU 
  182.          Caption         =   "E&xit"
  183.       End
  184.    End
  185. Sub ClearFields ()
  186.     RESULT_FIELD.Text = ""
  187.     DBNAME_FIELD.Text = ""
  188.     OWNER_FIELD.Text = ""
  189.     PARAMNAME_FIELD.Text = ""
  190.     PARAMVALUE_FIELD.Text = ""
  191. End Sub
  192. Sub DATABASE_MENU_Click ()
  193.     If SqlConn% = 0 Then
  194.         MsgBox "Must login first"
  195.         Exit Sub
  196.     End If
  197.     CHNGDB.Show 1
  198. End Sub
  199. Sub EXIT_MENU_Click ()
  200.     ExitApplication
  201. End Sub
  202. Sub Form_Load ()
  203. Rem Initialize the connection to SQL Server
  204.     InitializeApplication
  205.     MsgBox DBLIB_VERSION$
  206.     PrimaryWindowTitle = "VBSQL RPC demonstration"
  207. Rem Call routine to clear the fields
  208. Rem Fill in the procedure names combobox
  209.     ClearFields
  210.     PROCNAME_COMBO.AddItem "sp_help"
  211.     PROCNAME_COMBO.AddItem "sp_who"
  212.     PROCNAME_COMBO.AddItem "echo_test"
  213. End Sub
  214. Function GetServers (Server_Control As Control) As Integer
  215. Rem This routine gets the name of all the remote servers
  216. Rem Fill each element in the combobox or list box which is passed into this procedure
  217. Rem execute the command.  Get each server name and fill the combobox.
  218.     If ExecuteSQLCommand("Select srvname from master..sysservers where srvid != 0") = FAIL% Then
  219.         GetServers = FAIL
  220.         Exit Function
  221.     Else
  222.         If SqlResults(SqlConn%) = FAIL% Then Exit Function
  223.         While SqlNextRow(SqlConn%) <> NOMOREROWS%
  224.             Server_Control.AddItem SqlData(SqlConn%, 1)
  225.         Wend
  226.     End If
  227.     GetServers = SUCCEED
  228. End Function
  229. Sub LOGIN_MENU_Click ()
  230.     Login.Show 1
  231.     If SqlConn% <> 0 Then
  232.         SEND_QUERY_BUTTON.Enabled = True
  233.         MAKE_PROC_BUTTON.Enabled = True
  234.         Results% = GetServers(SERVER_COMBO)
  235.     End If
  236. End Sub
  237. Sub MAKE_PROC_BUTTON_Click ()
  238. Static OutputData(0) As String
  239. cmd$ = "CREATE PROCEDURE echo_test(@inparm1 varchar(20), @inparm2 int, "
  240. cmd$ = cmd$ + "@outparm varchar(20) = NULL OUTPUT) AS "
  241. cmd$ = cmd$ + "select @outparm = @inparm1 "
  242. cmd$ = cmd$ + "return @inparm2"
  243. Ret% = ExecuteSQLCommand(cmd$)
  244. If Ret% = SUCCEED% Then
  245.     MsgBox "Procedure echo_test created successfully"
  246.     ' get rid of an result rows
  247.     numrows& = Process_SQL_query("", OutputData())
  248. End If
  249. End Sub
  250. Sub Parse_params (ParamIn As String, Delimiter As String, ParamOut() As String) '
  251. Rem This routine takes the comma delimited Parameter names/values
  252. Start% = 1
  253. For i% = 0 To UBound(ParamOut)
  254.     If done% = False Then
  255.         'look for delimiter
  256.         endpos% = InStr(Start%, ParamIn$, Delimiter$)
  257.         If endpos% = 0 Then
  258.             'we're at the last parameter
  259.             ParamOut(i%) = Mid$(ParamIn$, Start%, 255)
  260.             done% = True
  261.         Else
  262.             ParamOut(i%) = Mid$(ParamIn$, Start%, (endpos% - Start%))
  263.             Start% = endpos% + 1
  264.         End If
  265.     Else
  266.         'clear out rest of array
  267.         ParamOut(i%) = ""
  268.     End If
  269. Next i%
  270. End Sub
  271. Sub PROCNAME_COMBO_Click ()
  272. Select Case PROCNAME_COMBO.Text
  273.     Case Is = "sp_help"
  274.         PARAMNAME_FIELD.Text = "@objname VARCHAR(30)"
  275.         PARAMVALUE_FIELD.Text = "sysobjects"
  276.     Case Is = "echo_test"
  277.         PARAMNAME_FIELD.Text = "@inparm1 VARCHAR(20),@inparm2 INTEGER,@outparm VARCHAR(20) OUT"
  278.         PARAMVALUE_FIELD.Text = "hello,7,notused"
  279.     Case Else
  280.         PARAMNAME_FIELD.Text = ""
  281.         PARAMVALUE_FIELD.Text = ""
  282. End Select
  283. End Sub
  284. Sub SEND_QUERY_BUTTON_Click ()
  285. Static OutputData(500) As String
  286. CRLF$ = Chr$(13) + Chr$(10)
  287. Rem Assemble the procedure name
  288. Rem Get the parameter names
  289. Rem Get the parameter values
  290. Rem Initialize the stored proc.
  291. Rem Fill the parameters
  292. Static Param_Values(10) As String
  293. Static Param_Declares(10) As String
  294. Static Param_Types(2) As String
  295. Static Param_Datatype(1) As String
  296. '  Note:  If this is a remote server-to-server procedure call (SERVER_NAME is
  297. '       not blank), and your remote password is different from your local
  298. '       password, you will need to set the remote password field with SqlRPwSet%
  299. '       prior to logging on.
  300. procname$ = SERVER_COMBO.Text + "." + DBNAME_FIELD.Text + "." + OWNER_FIELD.Text + "." + PROCNAME_COMBO.Text
  301. Results% = SqlRpcInit(SqlConn%, procname$, 0)
  302. If PARAMVALUE_FIELD.Text <> "" Then
  303.     In$ = PARAMVALUE_FIELD.Text
  304.     Parse_params In$, ",", Param_Values()
  305.     In$ = PARAMNAME_FIELD.Text
  306.     Parse_params In$, ",", Param_Declares()
  307.     For i% = 0 To 10
  308.         'check to see if we are at the end of the parameters provided
  309.         If Param_Values(i%) = "" Then Exit For
  310.         'for each parameter, find out its type
  311.         'first split type declaration into name, type(len), output
  312.         In$ = Param_Declares(i%)
  313.         Parse_params In$, " ", Param_Types()
  314.         'param name is first part
  315.         paramname$ = Param_Types(0)
  316.         'If this is an output var, set the flag
  317.         If Left$(Param_Types(2), 3) = "OUT" Then
  318.             status% = SQLRPCRETURN%
  319.         Else
  320.             status% = 0
  321.         End If
  322.         
  323.         'now take type(len) and split into type, len)
  324.         In$ = Param_Types(1)
  325.         Parse_params In$, "(", Param_Datatype()
  326.         
  327.         Select Case Param_Datatype(0)
  328.             Case "VARCHAR"
  329.                 typecode% = SQLVARCHAR%
  330.                 If status% = SQLRPCRETURN% Then
  331.                     maxlen& = Val(Left$(Param_Datatype(1), Len(Param_Datatype(1)) - 1))
  332.                 Else
  333.                     maxlen& = -1
  334.                 End If
  335.                 datalen& = Len(Param_Values(i%))
  336.             Case "INTEGER"
  337.                 typecode% = SQLINT4%
  338.                 maxlen& = -1
  339.                 datalen& = -1
  340.             Case Else
  341.                 MsgBox "Sample does not handle " + Param_Datatype(0) + " paramters"
  342.                 Exit Sub
  343.         End Select
  344.         Results% = SqlRpcParam(SqlConn%, paramname$, status%, typecode%, maxlen&, datalen&, Param_Values(i%))
  345.     Next i%
  346. End If
  347. Results% = SqlRpcSend(SqlConn%)
  348. Rem Clear the result array, and result controls first
  349. i% = 0
  350. For i% = 0 To 499
  351.     OutputData(i%) = ""
  352. Rem Fill the result array with the data
  353. Rem Fill the results field
  354. RESULT_FIELD.Text = ""
  355. numrows& = Process_SQL_query(cmd$, OutputData())
  356. For i% = 0 To numrows& - 1
  357.     DataLine$ = DataLine$ + OutputData(i%) + CRLF$
  358. Next i%
  359. RESULT_FIELD.Text = DataLine$
  360. End Sub
  361. Sub VBSQL1_Error (SqlConn As Integer, Severity As Integer, ErrorNum As Integer, ErrorStr As String, RetCode As Integer)
  362. ' Call the required VBSQL error-handling function
  363. ' OSErr and OSErrStr not used in VBSQL for Windows, but DOS interprets
  364. ' anything other than -1 as an OS error
  365.     OsErr% = -1
  366.     RetCode% = UserSqlErrorHandler%(SqlConn, Severity%, ErrorNum%, OsErr%, ErrorStr$, OsErrStr$)
  367. End Sub
  368. Sub VBSQL1_Message (SqlConn As Integer, Message As Long, State As Integer, Severity As Integer, MsgStr As String)
  369.     UserSqlMsgHandler SqlConn, Message&, State%, Severity%, MsgStr$
  370. End Sub
  371.