以上代码生成以上输出:
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?
答案 0 :(得分:3)
SMO默认使用Windows身份验证,因此您使用当前的Windows凭据进行连接。