1
+ using System ;
2
+ using System . Xml . Serialization ;
3
+ using System . Data ;
4
+ using System . Collections . Generic ;
5
+ using taskt . UI . Forms ;
6
+ using System . Windows . Forms ;
7
+ using taskt . UI . CustomControls ;
8
+ using System . Data . OleDb ;
9
+ using System . Drawing ;
10
+
11
+ namespace taskt . Core . Automation . Commands
12
+ {
13
+
14
+
15
+
16
+ [ Serializable ]
17
+ [ Attributes . ClassAttributes . Group ( "Database Commands" ) ]
18
+ [ Attributes . ClassAttributes . Description ( "This command allows you to define a connection to an OLEDB data source" ) ]
19
+ [ Attributes . ClassAttributes . UsesDescription ( "Use this command to create a new connection to a database." ) ]
20
+ [ Attributes . ClassAttributes . ImplementationDescription ( "This command implements 'OLEDB' to achieve automation." ) ]
21
+ public class DatabaseDefineConnectionCommand : ScriptCommand
22
+ {
23
+ [ XmlAttribute ]
24
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please Enter the instance name" ) ]
25
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter the unique instance name that was specified in the **Create Excel** command" ) ]
26
+ [ Attributes . PropertyAttributes . SampleUsage ( "**myInstance** or **seleniumInstance**" ) ]
27
+ [ Attributes . PropertyAttributes . Remarks ( "Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error" ) ]
28
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
29
+ public string v_InstanceName { get ; set ; }
30
+
31
+ [ XmlAttribute ]
32
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Define Connection String" ) ]
33
+ [ Attributes . PropertyAttributes . InputSpecification ( "" ) ]
34
+ [ Attributes . PropertyAttributes . SampleUsage ( "" ) ]
35
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
36
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
37
+ public string v_ConnectionString { get ; set ; }
38
+
39
+ [ XmlAttribute ]
40
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Test Connection Before Proceeding" ) ]
41
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "Yes" ) ]
42
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "No" ) ]
43
+ [ Attributes . PropertyAttributes . InputSpecification ( "Select an option which best fits to the specification you would like to make." ) ]
44
+ [ Attributes . PropertyAttributes . SampleUsage ( "Select one of the provided options." ) ]
45
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
46
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
47
+ public string v_TestConnection { get ; set ; }
48
+
49
+ [ XmlIgnore ]
50
+ [ NonSerialized ]
51
+ private TextBox ConnectionString ;
52
+ public DatabaseDefineConnectionCommand ( )
53
+ {
54
+ this . CommandName = "DatabaseDefineConnectionCommand" ;
55
+ this . SelectionName = "Define Database Connection" ;
56
+ this . CommandEnabled = true ;
57
+ this . CustomRendering = true ;
58
+ this . v_InstanceName = "sqlDefault" ;
59
+ this . v_TestConnection = "Yes" ;
60
+ }
61
+
62
+ public override void RunCommand ( object sender )
63
+ {
64
+ var engine = ( Core . Automation . Engine . AutomationEngineInstance ) sender ;
65
+ var connection = v_ConnectionString . ConvertToUserVariable ( sender ) ;
66
+ var instance = v_InstanceName . ConvertToUserVariable ( sender ) ;
67
+ var testPreference = v_TestConnection . ConvertToUserVariable ( sender ) ;
68
+
69
+ var oleDBConnection = new OleDbConnection ( connection ) ;
70
+
71
+ //attempt to open and close connection
72
+ if ( testPreference == "Yes" )
73
+ {
74
+ oleDBConnection . Open ( ) ;
75
+ oleDBConnection . Close ( ) ;
76
+ }
77
+
78
+ engine . AddAppInstance ( instance , oleDBConnection ) ;
79
+
80
+ }
81
+ public override List < Control > Render ( frmCommandEditor editor )
82
+ {
83
+ base . Render ( editor ) ;
84
+
85
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_InstanceName" , this , editor ) ) ;
86
+
87
+ CommandItemControl helperControl = new CommandItemControl ( ) ;
88
+ helperControl . Padding = new Padding ( 10 , 0 , 0 , 0 ) ;
89
+ helperControl . ForeColor = Color . AliceBlue ;
90
+ helperControl . Font = new Font ( "Segoe UI Semilight" , 10 ) ;
91
+ helperControl . Name = "connection_helper" ;
92
+ helperControl . CommandImage = UI . Images . GetUIImage ( "VariableCommand" ) ;
93
+ helperControl . CommandDisplay = "Build Connection String" ;
94
+ helperControl . Click += ( sender , e ) => Button_Click ( sender , e ) ;
95
+
96
+
97
+ ConnectionString = ( TextBox ) CommandControls . CreateDefaultInputFor ( "v_ConnectionString" , this ) ;
98
+
99
+ var connectionLabel = CommandControls . CreateDefaultLabelFor ( "v_ConnectionString" , this ) ;
100
+ var connectionHelpers = CommandControls . CreateUIHelpersFor ( "v_ConnectionString" , this , new [ ] { ConnectionString } , editor ) ;
101
+
102
+ RenderedControls . Add ( connectionLabel ) ;
103
+ RenderedControls . Add ( helperControl ) ;
104
+ RenderedControls . AddRange ( connectionHelpers ) ;
105
+ RenderedControls . Add ( ConnectionString ) ;
106
+
107
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_TestConnection" , this , editor ) ) ;
108
+
109
+ return RenderedControls ;
110
+
111
+ }
112
+
113
+ private void Button_Click ( object sender , EventArgs e )
114
+ {
115
+ ShowConnectionBuilder ( ) ;
116
+ }
117
+
118
+ public void ShowConnectionBuilder ( )
119
+ {
120
+
121
+ var MSDASCObj = new MSDASC . DataLinks ( ) ;
122
+ var connection = new ADODB . Connection ( ) ;
123
+ MSDASCObj . PromptEdit ( connection ) ;
124
+
125
+ if ( ! string . IsNullOrEmpty ( connection . ConnectionString ) )
126
+ {
127
+ ConnectionString . Text = connection . ConnectionString ;
128
+ }
129
+ }
130
+
131
+ public override string GetDisplayValue ( )
132
+ {
133
+ return $ "{ base . GetDisplayValue ( ) } - [Instance Name: '{ v_InstanceName } ']";
134
+ }
135
+ }
136
+ }
0 commit comments