using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dataTable = SmoApplication.EnumAvailableSqlServers(true);
listBox1.ValueMember = "Name";
listBox1.DataSource = dataTable;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.Items.Clear();
if (listBox1.SelectedIndex != -1)
{
string serverName = listBox1.SelectedValue.ToString();
Server server = new Server(serverName);
try
{
foreach (Database database in server.Databases)
{
listBox2.Items.Add(database.Name);
}
}
catch (Exception ex)
{
string exception = ex.Message;
}
}
}
}
这对我来说很奇怪。
此代码如何在不要求用户名和密码的情况下进入SQL Server?
最佳答案
SMO默认为Windows身份验证,因此将使用您当前的Windows凭据进行连接。