MsSqlDataConnection.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Data.SqlClient;
  7. namespace FastReport.Data
  8. {
  9. /// <summary>
  10. /// Represents a connection to MS SQL database.
  11. /// </summary>
  12. /// <example>This example shows how to add a new connection to the report.
  13. /// <code>
  14. /// Report report1;
  15. /// MsSqlDataConnection conn = new MsSqlDataConnection();
  16. /// conn.ConnectionString = "your_connection_string";
  17. /// report1.Dictionary.Connections.Add(conn);
  18. /// conn.CreateAllTables();
  19. /// </code>
  20. /// </example>
  21. public partial class MsSqlDataConnection : DataConnectionBase
  22. {
  23. /// <inheritdoc/>
  24. protected override string GetConnectionStringWithLoginInfo(string userName, string password)
  25. {
  26. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
  27. builder.IntegratedSecurity = false;
  28. builder.UserID = userName;
  29. builder.Password = password;
  30. return builder.ToString();
  31. }
  32. /// <inheritdoc/>
  33. public override string QuoteIdentifier(string value, DbConnection connection)
  34. {
  35. // already quoted? keep in mind GetDBObjectNames and non-dbo schema!
  36. if (!value.EndsWith("\""))
  37. value = "\"" + value + "\"";
  38. return value;
  39. }
  40. /// <inheritdoc/>
  41. public override Type GetConnectionType()
  42. {
  43. return typeof(SqlConnection);
  44. }
  45. /// <inheritdoc/>
  46. public override DbDataAdapter GetAdapter(string selectCommand, DbConnection connection,
  47. CommandParameterCollection parameters)
  48. {
  49. SqlDataAdapter adapter = new SqlDataAdapter(selectCommand, connection as SqlConnection);
  50. foreach (CommandParameter p in parameters)
  51. {
  52. SqlParameter parameter = adapter.SelectCommand.Parameters.Add(p.Name, (SqlDbType)p.DataType, p.Size);
  53. object value = p.Value;
  54. if ((SqlDbType)p.DataType == SqlDbType.UniqueIdentifier && (value is Variant || value is String))
  55. value = new Guid(value.ToString());
  56. parameter.Value = value;
  57. }
  58. return adapter;
  59. }
  60. /// <inheritdoc/>
  61. public override Type GetParameterType()
  62. {
  63. return typeof(SqlDbType);
  64. }
  65. private void GetDBObjectNames(string name, List<string> list)
  66. {
  67. DataTable schema = null;
  68. DbConnection conn = GetConnection();
  69. try
  70. {
  71. OpenConnection(conn);
  72. schema = conn.GetSchema("Tables", new string[] { null, null, null, name });
  73. }
  74. finally
  75. {
  76. DisposeConnection(conn);
  77. }
  78. foreach (DataRow row in schema.Rows)
  79. {
  80. string tableName = row["TABLE_NAME"].ToString();
  81. string schemaName = row["TABLE_SCHEMA"].ToString();
  82. if (String.Compare(schemaName, "dbo") == 0)
  83. list.Add(tableName);
  84. else
  85. list.Add(schemaName + ".\"" + tableName + "\"");
  86. }
  87. }
  88. /// <inheritdoc/>
  89. public override string[] GetTableNames()
  90. {
  91. List<string> list = new List<string>();
  92. GetDBObjectNames("BASE TABLE", list);
  93. GetDBObjectNames("VIEW", list);
  94. return list.ToArray();
  95. }
  96. }
  97. }