MsSqlConnectionEditor.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Windows.Forms;
  5. using System.Data.SqlClient;
  6. using System.Data.Sql;
  7. using System.Data.Common;
  8. using System.Data.SqlTypes;
  9. using System.Globalization;
  10. using FastReport.Utils;
  11. using FastReport.Forms;
  12. namespace FastReport.Data.ConnectionEditors
  13. {
  14. internal partial class MsSqlConnectionEditor : ConnectionEditorBase
  15. {
  16. private string connectionString;
  17. private void Localize()
  18. {
  19. MyRes res = new MyRes("ConnectionEditors,Common");
  20. lblServer.Text = res.Get("Server");
  21. gbServerLogon.Text = res.Get("ServerLogon");
  22. lblUserName.Text = res.Get("UserName");
  23. lblPassword.Text = res.Get("Password");
  24. gbDatabase.Text = res.Get("Database");
  25. rbDatabaseName.Text = res.Get("DatabaseName");
  26. rbDatabaseFile.Text = res.Get("DatabaseFile");
  27. res = new MyRes("ConnectionEditors,MsSql");
  28. rbUseWindows.Text = res.Get("UseWindows");
  29. rbUseSql.Text = res.Get("UseSql");
  30. cbSavePassword.Text = res.Get("SavePassword");
  31. btnAdvanced.Text = Res.Get("Buttons,Advanced");
  32. }
  33. public override void UpdateDpiDependencies()
  34. {
  35. base.UpdateDpiDependencies();
  36. tbDatabaseFile.Image = this.GetImage(1);
  37. }
  38. private void PopulateServerComboBox()
  39. {
  40. DataTable dataSources = null;
  41. Cursor saveCursor = Cursor;
  42. Cursor = Cursors.WaitCursor;
  43. try
  44. {
  45. #if NETCOREAPP
  46. DbDataSourceEnumerator dbDataSourceEnumerator = SqlClientFactory.Instance.CreateDataSourceEnumerator();
  47. dataSources = dbDataSourceEnumerator.GetDataSources();
  48. #else
  49. dataSources = SqlDataSourceEnumerator.Instance.GetDataSources();
  50. #endif
  51. }
  52. catch
  53. {
  54. return;
  55. }
  56. finally
  57. {
  58. Cursor = saveCursor;
  59. }
  60. List<string> servers = new List<string>();
  61. foreach (DataRow row in dataSources.Rows)
  62. {
  63. string str1 = row["ServerName"].ToString();
  64. string str2 = row["InstanceName"].ToString();
  65. servers.Add(String.IsNullOrEmpty(str2) ? str1 : str1 + "\\" + str2);
  66. }
  67. servers.Sort();
  68. foreach (string server in servers)
  69. {
  70. cbxServer.Items.Add(server);
  71. }
  72. }
  73. private void PopulateDatabasesComboBox()
  74. {
  75. DataTable table = null;
  76. IDbConnection currentConnection = null;
  77. IDataReader reader = null;
  78. Cursor saveCursor = Cursor;
  79. Cursor = Cursors.WaitCursor;
  80. try
  81. {
  82. currentConnection = GetCurrentConnection();
  83. IDbCommand command = currentConnection.CreateCommand();
  84. command.CommandText = "SELECT name FROM master.dbo.sysdatabases WHERE HAS_DBACCESS(name) = 1 ORDER BY name";
  85. currentConnection.Open();
  86. reader = command.ExecuteReader();
  87. table = new DataTable();
  88. table.Locale = CultureInfo.CurrentCulture;
  89. table.Load(reader);
  90. }
  91. catch
  92. {
  93. return;
  94. }
  95. finally
  96. {
  97. if (reader != null)
  98. reader.Dispose();
  99. if (currentConnection != null)
  100. currentConnection.Dispose();
  101. Cursor = saveCursor;
  102. }
  103. foreach (DataRow row in table.Rows)
  104. {
  105. cbxDatabaseName.Items.Add(row["name"]);
  106. }
  107. }
  108. private SqlConnection GetCurrentConnection()
  109. {
  110. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  111. builder.DataSource = cbxServer.Text;
  112. if (rbUseWindows.Checked)
  113. builder.IntegratedSecurity = true;
  114. else
  115. {
  116. builder.UserID = tbUserName.Text;
  117. builder.Password = tbPassword.Text;
  118. }
  119. builder.Pooling = false;
  120. return new SqlConnection(builder.ToString());
  121. }
  122. private void ServerOrLoginChanged(object sender, EventArgs e)
  123. {
  124. cbxDatabaseName.Items.Clear();
  125. UpdateControls(sender, e);
  126. }
  127. private void UpdateControls(object sender, EventArgs e)
  128. {
  129. bool loginEnabled = rbUseSql.Checked;
  130. lblUserName.Enabled = loginEnabled;
  131. lblPassword.Enabled = loginEnabled;
  132. tbUserName.Enabled = loginEnabled;
  133. tbPassword.Enabled = loginEnabled;
  134. cbSavePassword.Enabled = loginEnabled;
  135. gbDatabase.Enabled = cbxServer.Text != "" && (rbUseWindows.Checked || tbUserName.Text != "");
  136. cbxDatabaseName.Enabled = rbDatabaseName.Checked;
  137. tbDatabaseFile.Enabled = rbDatabaseFile.Checked;
  138. }
  139. private void cbxServer_DropDown(object sender, EventArgs e)
  140. {
  141. if (cbxServer.Items.Count == 0)
  142. PopulateServerComboBox();
  143. if (cbxServer.Items.Count == 0)
  144. cbxServer.Items.Add("");
  145. }
  146. private void cbxDatabaseName_DropDown(object sender, EventArgs e)
  147. {
  148. if (cbxDatabaseName.Items.Count == 0)
  149. PopulateDatabasesComboBox();
  150. if (cbxDatabaseName.Items.Count == 0)
  151. cbxDatabaseName.Items.Add("");
  152. }
  153. private void tbDatabaseFile_ButtonClick(object sender, EventArgs e)
  154. {
  155. using (OpenFileDialog dialog = new OpenFileDialog())
  156. {
  157. dialog.Filter = Res.Get("FileFilters,MdfFile");
  158. dialog.DefaultExt = ".mdf";
  159. if (dialog.ShowDialog() == DialogResult.OK)
  160. tbDatabaseFile.Text = dialog.FileName;
  161. }
  162. }
  163. private void btnAdvanced_Click(object sender, EventArgs e)
  164. {
  165. using (AdvancedConnectionPropertiesForm form = new AdvancedConnectionPropertiesForm())
  166. {
  167. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
  168. builder.BrowsableConnectionString = false;
  169. form.AdvancedProperties = builder;
  170. if (form.ShowDialog() == DialogResult.OK)
  171. ConnectionString = form.AdvancedProperties.ToString();
  172. }
  173. }
  174. protected override string GetConnectionString()
  175. {
  176. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
  177. builder.DataSource = cbxServer.Text;
  178. builder.IntegratedSecurity = rbUseWindows.Checked;
  179. if (builder.IntegratedSecurity)
  180. {
  181. builder.UserID = "";
  182. builder.Password = "";
  183. }
  184. else
  185. {
  186. builder.UserID = tbUserName.Text;
  187. builder.Password = tbPassword.Text;
  188. }
  189. builder.PersistSecurityInfo = cbSavePassword.Checked;
  190. if (rbDatabaseName.Checked)
  191. {
  192. builder.InitialCatalog = cbxDatabaseName.Text;
  193. builder.AttachDBFilename = "";
  194. }
  195. else
  196. {
  197. builder.AttachDBFilename = tbDatabaseFile.Text;
  198. builder.InitialCatalog = "";
  199. }
  200. return builder.ToString();
  201. }
  202. protected override void SetConnectionString(string value)
  203. {
  204. connectionString = value;
  205. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(value);
  206. if (String.IsNullOrEmpty(value))
  207. builder.IntegratedSecurity = true;
  208. cbxServer.Text = builder.DataSource;
  209. rbUseWindows.Checked = builder.IntegratedSecurity;
  210. rbUseSql.Checked = !rbUseWindows.Checked;
  211. tbUserName.Text = builder.UserID;
  212. tbPassword.Text = builder.Password;
  213. cbSavePassword.Checked = builder.PersistSecurityInfo;
  214. if (String.IsNullOrEmpty(builder.AttachDBFilename))
  215. {
  216. rbDatabaseName.Checked = true;
  217. cbxDatabaseName.Text = builder.InitialCatalog;
  218. }
  219. else
  220. {
  221. rbDatabaseFile.Checked = true;
  222. tbDatabaseFile.Text = builder.AttachDBFilename;
  223. }
  224. UpdateControls(this, EventArgs.Empty);
  225. }
  226. public MsSqlConnectionEditor()
  227. {
  228. InitializeComponent();
  229. Localize();
  230. }
  231. }
  232. }