/Epi Info 7.1.4.0 Release - Web Enter Integration/Epi.Data.SqlServer/Forms/ConnectionStringDialog.cs

# · C# · 363 lines · 214 code · 51 blank · 98 comment · 14 complexity · 2212a00bedb2ccf37bb88021b72f4d9b MD5 · raw file

  1. #region Namespaces
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Data.SqlClient;
  8. using System.Drawing;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using Epi.Data;
  12. #endregion
  13. namespace Epi.Data.SqlServer.Forms
  14. {
  15. /// <summary>
  16. /// Dialog for building a SQL Server connection string
  17. /// </summary>
  18. public partial class ConnectionStringDialog : DialogBase, IConnectionStringGui // Form,
  19. {
  20. #region Public Data Members
  21. ///// <summary>
  22. ///// Sql server connection string
  23. ///// </summary>
  24. //public string connectionString;
  25. #endregion //Public Data Members
  26. #region Constructors
  27. /// <summary>
  28. /// Default Constructor
  29. /// </summary>
  30. public ConnectionStringDialog()
  31. {
  32. InitializeComponent();
  33. this.cmbServerName.Items.Add(SharedStrings.BROWSE_FOR_MORE);
  34. this.cmbServerName.Items.Add("(local)");
  35. this.cmbServerName.Text = "";
  36. dbConnectionStringBuilder = new SqlConnectionStringBuilder();
  37. }
  38. #endregion
  39. #region IConnectionStringBuilder Members
  40. public bool ShouldIgnoreNonExistance
  41. {
  42. set { }
  43. }
  44. public virtual void SetDatabaseName(string databaseName)
  45. {
  46. this.cmbDatabaseName.Text = databaseName;
  47. }
  48. public virtual void SetServerName(string serverName)
  49. {
  50. this.cmbServerName.Text = serverName;
  51. }
  52. public virtual void SetUserName(string userName)
  53. {
  54. txtUserName.Text = userName;
  55. }
  56. public virtual void SetPassword(string password)
  57. {
  58. txtPassword.Text = password;
  59. }
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. protected SqlConnectionStringBuilder dbConnectionStringBuilder;
  64. /// <summary>
  65. /// Gets or sets the SqlDBConnectionString Object
  66. /// </summary>
  67. public DbConnectionStringBuilder DbConnectionStringBuilder
  68. {
  69. get
  70. {
  71. return dbConnectionStringBuilder;
  72. }
  73. //set
  74. //{
  75. // dbConnectionStringBuilder = (SqlConnectionStringBuilder)value;
  76. //}
  77. }
  78. /// <summary>
  79. /// Sets the preferred database name
  80. /// </summary>
  81. public virtual string PreferredDatabaseName
  82. {
  83. set
  84. {
  85. cmbDatabaseName.Text = value;
  86. }
  87. get
  88. {
  89. return cmbDatabaseName.Text;
  90. }
  91. }
  92. /// <summary>
  93. /// Gets a user friendly description of the connection string
  94. /// </summary>
  95. public virtual string ConnectionStringDescription
  96. {
  97. get
  98. {
  99. return cmbServerName.Text + "::" + cmbDatabaseName.Text;
  100. }
  101. }
  102. /// <summary>
  103. /// Gets whether or not the user entered a password
  104. /// </summary>
  105. public bool UsesPassword
  106. {
  107. get
  108. {
  109. if (this.txtPassword.Text.Length > 0)
  110. {
  111. return true;
  112. }
  113. else
  114. {
  115. return false;
  116. }
  117. }
  118. }
  119. #endregion //IConnectionStringBuilder Members
  120. #region Event Handlers
  121. /// <summary>
  122. /// Handles the Click event of the OK button
  123. /// </summary>
  124. /// <param name="sender">Object that fired the event</param>
  125. /// <param name="e">.NET supplied event parameters</param>
  126. private void btnOK_Click(object sender, EventArgs e)
  127. {
  128. OnOkClick();
  129. }
  130. /// <summary>
  131. /// Class the OnAuthenticationCheckChanged method
  132. /// </summary>
  133. /// <param name="sender">Object that fired the event</param>
  134. /// <param name="e">.NET supplied event parameters</param>
  135. private void rdbAuthentication_CheckedChanged(object sender, EventArgs e)
  136. {
  137. lblPassword.Enabled = rdbSqlAuthentication.Checked;
  138. lblUserName.Enabled = rdbSqlAuthentication.Checked;
  139. txtPassword.Enabled = rdbSqlAuthentication.Checked;
  140. txtUserName.Enabled = rdbSqlAuthentication.Checked;
  141. }
  142. /// <summary>
  143. /// Handles the Click event of the Cancel button
  144. /// </summary>
  145. /// <param name="sender">Object that fired the event</param>
  146. /// <param name="e">.NET supplied event parameters</param>
  147. private void btnCancel_Click(object sender, EventArgs e)
  148. {
  149. OnCancelClick();
  150. }
  151. /// <summary>
  152. /// Handles the Change event of the Server Name's selection
  153. /// </summary>
  154. /// <param name="sender">Object that fired the event</param>
  155. /// <param name="e">.NET supplied event parameters</param>
  156. private void cmbServerName_SelectedIndexChanged(object sender, EventArgs e)
  157. {
  158. // if last item in list (Browse for Servers)
  159. if ((string)cmbServerName.SelectedItem == SharedStrings.BROWSE_FOR_MORE)
  160. {
  161. string serverName = BrowseForServers.BrowseNetworkServers();
  162. if (!string.IsNullOrEmpty(serverName))
  163. {
  164. this.cmbServerName.Items.Insert(0, serverName);
  165. this.cmbServerName.SelectedIndex = 0;
  166. string database = this.cmbDatabaseName.Text;
  167. Epi.Data.SqlServer.SqlDatabase db = new SqlDatabase();
  168. dbConnectionStringBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
  169. dbConnectionStringBuilder.DataSource = cmbServerName.Text;
  170. dbConnectionStringBuilder.UserID = txtUserName.Text;
  171. if (rdbWindowsAuthentication.Checked)
  172. {
  173. dbConnectionStringBuilder.IntegratedSecurity = true;
  174. }
  175. else
  176. {
  177. dbConnectionStringBuilder.UserID = txtUserName.Text;
  178. dbConnectionStringBuilder.Password = txtPassword.Text;
  179. }
  180. db.ConnectionString = this.DbConnectionStringBuilder.ToString();
  181. try
  182. {
  183. if (db.TestConnection())
  184. {
  185. this.cmbDatabaseName.Text = string.Empty;
  186. List<string> databaseNamesList = db.GetDatabaseNameList();
  187. cmbDatabaseName.DataSource = databaseNamesList;
  188. }
  189. else
  190. {
  191. MessageBox.Show("Connection failed."); // TODO: hard coded string.
  192. }
  193. }
  194. catch (Exception ex)
  195. {
  196. MessageBox.Show("Connection failed: " + ex.Message); // TODO: hard coded string
  197. }
  198. return;
  199. }
  200. this.cmbServerName.SelectedText = string.Empty;
  201. }
  202. }
  203. /// <summary>
  204. /// Handles the Click event of the Test button
  205. /// </summary>
  206. /// <param name="sender">Object that fired the event</param>
  207. /// <param name="e">.NET supplied event parameters</param>
  208. protected virtual void btnTest_Click(object sender, EventArgs e)
  209. {
  210. //string database = this.cmbDatabaseName.Text;
  211. //Epi.Data.SqlServer.SqlDatabase db = new SqlDatabase();
  212. //db.ConnectionString = this.DbConnectionStringBuilder.ToString(); //connectionString;
  213. //try
  214. //{
  215. // if (db.TestConnection())
  216. // {
  217. // MessageBox.Show("Success!");
  218. // this.cmbDatabaseName.Items.Clear();
  219. // List<string> databaseNamesList = db.GetDatabaseNameList();
  220. // foreach (string name in databaseNamesList)
  221. // {
  222. // this.cmbDatabaseName.Items.Add(name);
  223. // }
  224. // }
  225. // else
  226. // {
  227. // MessageBox.Show("Connection failed.");
  228. // }
  229. //}
  230. //catch (Exception ex)
  231. //{
  232. // MessageBox.Show("Connection failed: " + ex.Message);
  233. //}
  234. }
  235. #endregion Event Handlers
  236. #region Private Methods
  237. #endregion Private Methods
  238. #region Protected Methods
  239. /// <summary>
  240. /// Validates if all the necessary input is provided.
  241. /// </summary>
  242. /// <returns></returns>
  243. protected override bool ValidateInput()
  244. {
  245. base.ValidateInput();
  246. if (string.IsNullOrEmpty(cmbServerName.Text.Trim()))
  247. {
  248. ErrorMessages.Add("Server name is required"); // TODO: Hard coded string
  249. }
  250. // TODO: Check if this is required.
  251. //if (string.IsNullOrEmpty(txtDatabaseName.Text.Trim()))
  252. //{
  253. // ErrorMessages.Add("Database name is required"); // TODO: Hard coded string
  254. //}
  255. if (rdbSqlAuthentication.Checked)
  256. {
  257. // Check if user id and password are provided ...
  258. if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
  259. {
  260. ErrorMessages.Add("Login name is required");
  261. }
  262. if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
  263. {
  264. ErrorMessages.Add("Password is required");
  265. }
  266. }
  267. return (ErrorMessages.Count == 0);
  268. }
  269. /// <summary>
  270. /// Occurs when cancel is clicked
  271. /// </summary>
  272. protected void OnCancelClick()
  273. {
  274. this.dbConnectionStringBuilder.ConnectionString = string.Empty;
  275. this.PreferredDatabaseName = string.Empty;
  276. this.DialogResult = DialogResult.Cancel;
  277. this.Close();
  278. }
  279. /// <summary>
  280. /// Occurs when OK is clicked
  281. /// </summary>
  282. protected virtual void OnOkClick()
  283. {
  284. if (ValidateInput() == true)
  285. {
  286. //BuildConnectionString(cmbDatabaseName.Text);
  287. dbConnectionStringBuilder = new SqlConnectionStringBuilder();
  288. dbConnectionStringBuilder.DataSource = cmbServerName.Text;
  289. dbConnectionStringBuilder.InitialCatalog = cmbDatabaseName.Text;
  290. dbConnectionStringBuilder.UserID = txtUserName.Text;
  291. if (rdbWindowsAuthentication.Checked)
  292. {
  293. dbConnectionStringBuilder.IntegratedSecurity = true;
  294. }
  295. else
  296. {
  297. dbConnectionStringBuilder.UserID = txtUserName.Text;
  298. dbConnectionStringBuilder.Password = txtPassword.Text;
  299. }
  300. //removed //zack
  301. //connectionString = dbConnectionStringBuilder.ToString();
  302. this.DialogResult = DialogResult.OK;
  303. this.Hide();
  304. }
  305. else
  306. {
  307. ShowErrorMessages();
  308. }
  309. }
  310. #endregion //Protected Methods
  311. }
  312. }