PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/SharePoint Manager 2007/SharePoint Manager 2007/Forms/OpenDBConnection.cs

#
C# | 334 lines | 126 code | 48 blank | 160 comment | 6 complexity | 9a1387d88ee3c6e682575c06eb7fbf00 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. //using Microsoft.SqlServer.Management.Smo;
  9. using System.Data.SqlClient;
  10. //using Microsoft.SqlServer.Management.Common;
  11. using Microsoft.Win32;
  12. using Keutmann.SharePointManager.Library;
  13. namespace Keutmann.SharePointManager.Forms
  14. {
  15. public partial class OpenDBConnection : Form
  16. {
  17. private const string RegConnectionsGroup = @"Config\Connections\";
  18. //private bool hasTablesBeenloaded = false;
  19. private string _ConnectionString = string.Empty;
  20. public string ConnectionString
  21. {
  22. get
  23. {
  24. return ConnectionBuilder.ConnectionString;
  25. }
  26. set
  27. {
  28. ConnectionBuilder = new SqlConnectionStringBuilder(value);
  29. }
  30. }
  31. //private ServerConnection _Connection = null;
  32. //public ServerConnection Connection
  33. //{
  34. // get
  35. // {
  36. // if (_Connection == null)
  37. // {
  38. // _Connection = new ServerConnection();
  39. // _Connection.ConnectAsUser = false;
  40. // }
  41. // return _Connection;
  42. // }
  43. // set
  44. // {
  45. // _Connection = value;
  46. // }
  47. //}
  48. private SqlConnectionStringBuilder _ConnectionBuilder = null;
  49. public SqlConnectionStringBuilder ConnectionBuilder
  50. {
  51. get
  52. {
  53. if (_ConnectionBuilder == null)
  54. {
  55. _ConnectionBuilder = new SqlConnectionStringBuilder();
  56. }
  57. return _ConnectionBuilder;
  58. }
  59. set
  60. {
  61. _ConnectionBuilder = value;
  62. }
  63. }
  64. public OpenDBConnection()
  65. {
  66. InitializeComponent();
  67. //string connString = SPMRegistry.GetValue(RegConnectionsGroup, cbServers.SelectedItem as string) as string;
  68. }
  69. //private void LoadConnectionStrings()
  70. //{
  71. // RegistryKey key = SPMRegistry.GetKey(RegConnectionsGroup);
  72. // foreach(string name in key.GetSubKeyNames())
  73. // {
  74. // cbServers.Items.Add(name);
  75. // }
  76. //}
  77. private void ConfigControls()
  78. {
  79. if (ConnectionBuilder.DataSource.Length > 0)
  80. {
  81. //int index = cbServers.Items.IndexOf(ConnectionBuilder.DataSource);
  82. //if (index < 0)
  83. //{
  84. // cbServers.Items.Add(ConnectionBuilder.DataSource);
  85. // cbServers.SelectedIndex = 0;
  86. //}
  87. //else
  88. //{
  89. // cbServers.SelectedIndex = index;
  90. //}
  91. tbServer.Text = ConnectionBuilder.DataSource;
  92. }
  93. if (ConnectionBuilder.InitialCatalog.Length > 0)
  94. {
  95. //int index = cbSelectDatabase.Items.IndexOf(ConnectionBuilder.InitialCatalog);
  96. //if (index < 0)
  97. //{
  98. // cbSelectDatabase.Items.Add(ConnectionBuilder.InitialCatalog);
  99. // cbSelectDatabase.SelectedIndex = 0;
  100. //}
  101. //else
  102. //{
  103. // cbSelectDatabase.SelectedIndex = index;
  104. //}
  105. tbDatabase.Text = ConnectionBuilder.InitialCatalog;
  106. }
  107. if (ConnectionBuilder.IntegratedSecurity)
  108. {
  109. rbWindowsAuthentication.Checked = true;
  110. }
  111. else
  112. {
  113. rbSQLserverAuthentication.Checked = true;
  114. tbUserName.Text = ConnectionBuilder.UserID;
  115. tbPassword.Text = ConnectionBuilder.Password;
  116. }
  117. }
  118. public void SetConnectionString()
  119. {
  120. ConnectionBuilder.DataSource = tbServer.Text;
  121. ConnectionBuilder.InitialCatalog = tbDatabase.Text;
  122. ConnectionBuilder.IntegratedSecurity = !rbSQLserverAuthentication.Checked;
  123. if (!ConnectionBuilder.IntegratedSecurity)
  124. {
  125. ConnectionBuilder.UserID = tbUserName.Text;
  126. ConnectionBuilder.Password = tbPassword.Text;
  127. }
  128. }
  129. private void AddDBConnection_Load(object sender, EventArgs e)
  130. {
  131. // LoadConnectionStrings();
  132. InitializeInterfaceStrings();
  133. ConfigControls();
  134. }
  135. //private void btnRefresh_Click(object sender, EventArgs e)
  136. //{
  137. // try
  138. // {
  139. // this.Cursor = Cursors.WaitCursor;
  140. // // Get a list of SQL servers available on the networks
  141. // DataTable dtSQLServers = SmoApplication.EnumAvailableSqlServers(false);
  142. // string localServerName = "";
  143. // if (cbServers.Items.Count == 0)
  144. // {
  145. // // By default select the local server
  146. // Server localServer = new Server();
  147. // localServerName = localServer.Name;
  148. // }
  149. // else
  150. // {
  151. // localServerName = cbServers.SelectedText;
  152. // }
  153. // string fullName = string.Empty;
  154. // foreach (DataRow drServer in dtSQLServers.Rows)
  155. // {
  156. // String serverName = drServer["Server"].ToString();
  157. // if (drServer["Instance"] != null && drServer["Instance"].ToString().Length > 0)
  158. // {
  159. // fullName = serverName + @"\" + drServer["Instance"].ToString();
  160. // }
  161. // if (cbServers.Items.IndexOf(fullName) < 0)
  162. // {
  163. // int index = cbServers.Items.Add(fullName);
  164. // if(String.Compare(serverName, localServerName, true) == 0)
  165. // {
  166. // cbServers.SelectedIndex = index;
  167. // }
  168. // Connection.ServerInstance = fullName;
  169. // }
  170. // }
  171. // }
  172. // catch (SmoException smoException)
  173. // {
  174. // MessageBox.Show(smoException.ToString());
  175. // }
  176. // catch (Exception exception)
  177. // {
  178. // MessageBox.Show(exception.ToString());
  179. // }
  180. // finally
  181. // {
  182. // this.Cursor = Cursors.Default;
  183. // }
  184. //}
  185. //private void cbSelectDatabase_DropDown(object sender, EventArgs e)
  186. //{
  187. // if (!hasTablesBeenloaded && cbServers.Items.Count > 0)
  188. // {
  189. // try
  190. // {
  191. // this.Cursor = Cursors.WaitCursor;
  192. // // Fill the databases combo
  193. // cbSelectDatabase.Items.Clear();
  194. // Server SelectedServer = new Server(cbServers.Text);
  195. // Int32 DBCount = 0;
  196. // foreach (Database db in SelectedServer.Databases)
  197. // {
  198. // if (!db.IsSystemObject)
  199. // {
  200. // DBCount++;
  201. // cbSelectDatabase.Items.Add(db.Name);
  202. // }
  203. // }
  204. // if (DBCount == 0)
  205. // {
  206. // MessageBox.Show("Did not find any SQL Server 2005 servers.");
  207. // }
  208. // hasTablesBeenloaded = true;
  209. // }
  210. // catch (SmoException smoException)
  211. // {
  212. // MessageBox.Show(smoException.ToString());
  213. // }
  214. // catch (Exception exception)
  215. // {
  216. // MessageBox.Show(exception.ToString());
  217. // }
  218. // finally
  219. // {
  220. // this.Cursor = Cursors.Default;
  221. // }
  222. // }
  223. //}
  224. //private void cbServers_SelectedIndexChanged(object sender, EventArgs e)
  225. //{
  226. // hasTablesBeenloaded = false;
  227. // string servername = cbServers.SelectedItem as string;
  228. // string conn = SPMRegistry.GetValue(RegConnectionsGroup, servername) as string;
  229. // if (conn != null && conn.Length > 0)
  230. // {
  231. // ConnectionString = conn;
  232. // ConfigControls();
  233. // }
  234. //}
  235. private void rbWindowsAuthentication_CheckedChanged(object sender, EventArgs e)
  236. {
  237. panelSQLServerAuthentication.Enabled = rbSQLserverAuthentication.Checked;
  238. }
  239. private void btnTestConnection_Click(object sender, EventArgs e)
  240. {
  241. Cursor.Current = Cursors.WaitCursor;
  242. SetConnectionString();
  243. SqlConnection sqlConn = new SqlConnection(ConnectionString);
  244. try
  245. {
  246. sqlConn.Open();
  247. }
  248. finally
  249. {
  250. Cursor.Current = Cursors.Default;
  251. sqlConn.Close();
  252. }
  253. MessageBox.Show(SPMLocalization.GetString("Succes"), SPMLocalization.GetString("Testing_Database_Connection"));
  254. }
  255. //private void cbSelectDatabase_SelectedIndexChanged(object sender, EventArgs e)
  256. //{
  257. // ConnectionBuilder.InitialCatalog = cbSelectDatabase.SelectedItem as string;
  258. //}
  259. private void btnOK_Click(object sender, EventArgs e)
  260. {
  261. SetConnectionString();
  262. //SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
  263. //if (!ConnectionBuilder.IntegratedSecurity && !cbSaveMyPassword.Checked)
  264. //{
  265. // builder.Password = "";
  266. //}
  267. //SPMRegistry.SetValue(RegConnectionsGroup, cbServers.SelectedText, builder.ToString());
  268. }
  269. private void InitializeInterfaceStrings()
  270. {
  271. label1.Text = SPMLocalization.GetString("Interface_EnterInfo_Text");
  272. lblServerName.Text = SPMLocalization.GetString("Interface_ServerName");
  273. groupBox1.Text = SPMLocalization.GetString("Interface_LogOn");
  274. lblPassword.Text = SPMLocalization.GetString("Interface_Password");
  275. label2.Text = SPMLocalization.GetString("Interface_UserName");
  276. rbSQLserverAuthentication.Text = SPMLocalization.GetString("Interface_UseSQLServerAuth");
  277. rbWindowsAuthentication.Text = SPMLocalization.GetString("Interface_UseWindowsAuth");
  278. groupBox2.Text = SPMLocalization.GetString("Interface_ConnectToSP");
  279. lblSelectDatabase.Text = SPMLocalization.GetString("Interface_EnterConfigDB");
  280. btnCancel.Text = SPMLocalization.GetString("Interface_ButtonCancel");
  281. btnOK.Text = SPMLocalization.GetString("Interface_ButtonOK");
  282. btnTestConnection.Text = SPMLocalization.GetString("Interface_TestConn");
  283. this.Text = SPMLocalization.GetString("Interface_OpenConn");
  284. }
  285. }
  286. }