The following references need to be added
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Microsoft.SqlServer.ConnectionInfo
ServerConnection oServerCon = new ServerConnection(txtServer.Text, txtUserName.Text, txtPassword.Text);
Server oServer = new Server(oServerCon);
lvDbOptions.Items.Clear();
String Value = String.Empty;
if (ddlOption.SelectedItem.ToString().ToLower() == "tables")
{
foreach (Table oTable in oServer.Databases[txtDatabase.Text].Tables)
{
if (oTable.IsSystemObject == false)
{
Value = oTable.ToString().Substring(oTable.ToString().IndexOf(".") + 2, (oTable.ToString().Length - 1) - (oTable.ToString().IndexOf(".") + 2));
lvDbOptions.Items.Add(new ListViewItem(Value));
}
}
}
else if (ddlOption.SelectedItem.ToString().ToLower() == "stored procedures")
{
foreach (StoredProcedure oProc in oServer.Databases[txtDatabase.Text].StoredProcedures)
{
if(oProc.IsSystemObject == false)
{
Value = oProc.ToString().Substring(oProc.ToString().IndexOf(".") + 2, (oProc.ToString().Length - 1) - (oProc.ToString().IndexOf(".") + 2));
lvDbOptions.Items.Add(new ListViewItem(Value));
}
}
}
else if (ddlOption.SelectedItem.ToString().ToLower() == "functions")
{
foreach (UserDefinedFunction oFunc in oServer.Databases[txtDatabase.Text].UserDefinedFunctions)
{
if (oFunc.IsSystemObject == false)
{
Value = oFunc.ToString().Substring(oFunc.ToString().IndexOf(".") + 2, (oFunc.ToString().Length - 1) - (oFunc.ToString().IndexOf(".") + 2));
lvDbOptions.Items.Add(new ListViewItem(Value));
}
}
}