string connectionString = @"data source=服务器名;initial catalog=数据库名;persist security info=True;user id=登录名;password=密码;MultipleActiveResultSets=True";
// string connectionString = @"Server=(local); Database=AdventureWorks; User ID=sa; Password=password";
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectionString;
string sqlCommandString = @"USE MES_DT20180611 SELECT name FROM sys.sysobjects WHERE type='U' ORDER BY name";
SqlCommand command = new SqlCommand();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = sqlCommandString;
command.Connection = connection;
try
{
connection.Open();
}
catch (Exception e)
{
throw e;
}
SqlDataReader reader = command.ExecuteReader();
// string a = reader[@"name"];
while (reader.Read())
{
Console.WriteLine(reader[@"name"]);
} 从sqlserver中获取所有表名