Guest User

Untitled

a guest
Nov 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. DataTable t = _conn.GetSchema("Tables");
  2.  
  3. public IList<string> ListTables()
  4. {
  5. List<string> tables = new List<string>();
  6. DataTable dt = _connection.GetSchema("Tables");
  7. foreach (DataRow row in dt.Rows)
  8. {
  9. string tablename = (string)row[2];
  10. tables.Add(tablename);
  11. }
  12. return tables;
  13. }
  14.  
  15. SELECT name FROM sysobjects WHERE xtype = 'U'
  16.  
  17. System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection("server=192.168.0.1;uid=sa;pwd=1234");
  18. SqlCon.Open();
  19.  
  20. System.Data.SqlClient.SqlCommand SqlCom = new System.Data.SqlClient.SqlCommand();
  21. SqlCom.Connection = SqlCon;
  22. SqlCom.CommandType = CommandType.StoredProcedure;
  23. SqlCom.CommandText = "sp_databases";
  24.  
  25. System.Data.SqlClient.SqlDataReader SqlDR;
  26. SqlDR = SqlCom.ExecuteReader();
  27.  
  28. while(SqlDR.Read())
  29. {
  30. MessageBox.Show(SqlDR.GetString(0));
  31. }
  32.  
  33. string[] GetAllTables(SqlConnection connection)
  34. {
  35. List<string> result = new List<string>();
  36. SqlCommand cmd = new SqlCommand("SELECT name FROM sys.Tables", connection);
  37. System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
  38. while(reader.Read())
  39. result.Add(reader["name"].ToString());
  40. return result.ToArray();
  41. }
  42.  
  43. public static List<string> GetTableNames(this SqlConnection connection)
  44. {
  45. using(SqlConnection conn = connection)
  46. {
  47. if(conn.State == ConnectionState.Open)
  48. {
  49. return conn.GetSchema("Tables").AsEnumerable().Select(s => s[2].ToString()).ToList();
  50. }
  51. }
  52. //Add some error-handling instead !
  53. return new List<string>();
  54. }
  55.  
  56. private IEnumerable<string> getAllTables()
  57. {
  58. var sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
  59. var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection);
  60. var server = new Microsoft.SqlServer.Management.Smo.Server(serverConnection);
  61. var database = server.Databases[databaseName];
  62. foreach (Microsoft.SqlServer.Management.Smo.Table table in database.Tables)
  63. {
  64. yield return table.Name;
  65. }
  66. }
  67.  
  68. public System.Collections.Generic.Dictionary<string, string> GetAllTables(System.Data.SqlClient.SqlConnection _connection)
  69. {
  70. if (_connection.State == System.Data.ConnectionState.Closed)
  71. _connection.Open();
  72. System.Data.DataTable dt = _connection.GetSchema("Tables");
  73. System.Collections.Generic.Dictionary<string, string> tables = new System.Collections.Generic.Dictionary<string, string>();
  74. foreach (System.Data.DataRow row in dt.Rows)
  75. {
  76. if (row[3].ToString().Equals("BASE TABLE", StringComparison.OrdinalIgnoreCase)) //ignore views
  77. {
  78. string tableName = row[2].ToString();
  79. string schema = row[1].ToString();
  80. tables.Add(tableName, schema);
  81. }
  82. }
  83. _connection.Close();
  84. return tables;
  85. }
  86.  
  87. <div>
  88. <asp:Button ID="GridViewTableListButton" runat="server" Text="List all Tables on server"
  89. onclick="GridViewTableListButton_Click" />
  90. <asp:GridView ID="GridViewTableList" runat="server">
  91. </asp:GridView>
  92. </div>
  93.  
  94. protected void GridViewTableListButton_Click(object sender, EventArgs e)
  95. {
  96. objConn.Open();
  97. DataTable t = objConn.GetSchema("Tables");
  98. GridViewTableList.DataSource = t;
  99. GridViewTableList.DataBind();
  100. objConn.Close();
  101. }
  102.  
  103. using System.Data;
  104.  
  105. SqlConnection objConn = new SqlConnection();
  106.  
  107. objConn.ConnectionString = ConfigurationManager.ConnectionStrings[connString].ConnectionString;
  108.  
  109. public class connectionClass
  110. {
  111. .....
  112. public string connClass()
  113. {
  114. connString = "LocalSqlServer"; // LOCAL home PC Version
  115. }
  116. }
  117.  
  118. <add name="LocalSqlServer" connectionString="Data Source=MyPCsNameSQLEXPRESS;Initial Catalog=databasename;Integrated Security=True" providerName="System.Data.SqlClient"/>
Add Comment
Please, Sign In to add comment