SQL ServerR2 2008 SMO 在 C# 命名空间错误中不起作用。我无法创建服务器对象。包括stackoverflow帖子在内的各种站点中列出的参考资料对我不起作用,请帮助我。
第一个错误:
缺少对 Microsoft.SqlServer.Management.Sdk.Sfc 的引用
添加引用时,出现第二个错误:
sdk 在 microsoft.sqlserver.management 中不存在
我可以在这里上传源代码吗(我没有看到选项)?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Sdk.Sfc;
namespace DatabaseConnectionManagement
{
public partial class FrmAddConnection : Form
{
ServerConnection m_ServerConnection;
Microsoft.SqlServer.Management.Smo.Server m_Server; //Final Error is in this line
EventHandler _Handler;
public FrmAddConnection()
{
InitializeComponent();
}
public Server DatabaseServer
{
get { return this.m_Server; }
}
void Application_Idle(object sender, EventArgs e)
{
// Application was idle and thats when we do the work.
// Lets first un-register the event.
Application.Idle -= this._Handler;
//Check for all the available servers
DataTable dtServers = SmoApplication.EnumAvailableSqlServers(false);
dtServers.PrimaryKey = new DataColumn[] { dtServers.Columns[0] };
this.cmbServerName.DataSource = dtServers;
this.cmbServerName.DisplayMember = "Name";
this.cmbServerName.ValueMember = "Name";
//Set the default server
if (!string.IsNullOrEmpty(Properties.Settings.Default.Server))
{
this.cmbServerName.SelectedIndex = dtServers.Rows.IndexOf(dtServers.Rows.Find(Properties.Settings.Default.Server.ToString()));
}
else
{
this.cmbServerName.SelectedIndex = 0;
}
//What type of authentication the user wants? We will have to change the string as required.
if (Properties.Settings.Default.SQLAuthenticationMode)
{
this.rdbServerAuthentication.Checked = true;
this.rdbWindowsAuthentication.Checked = false;
if (Properties.Settings.Default.SavePassword == true)
{
this.txtUserName.Text = Properties.Settings.Default.UserName;
this.txtPassword.Text = Properties.Settings.Default.Password;
this.chkSavePassword.Checked = true;
}
else
{
return;
}
}
else
{
this.rdbServerAuthentication.Checked = false;
this.rdbWindowsAuthentication.Checked = true;
this.txtUserName.Enabled = false;
this.txtPassword.Enabled = false;
//this.chkSavePassword.Enabled = false;
}
string tmpDbName = Properties.Settings.Default.DatabaseName;
if (!string.IsNullOrEmpty(tmpDbName))
{
this.btnTestConnection_Click(this, null);
this.cmbDbName.SelectedItem = Properties.Settings.Default.DatabaseName;
}
this.lblSyncStatus.Visible = false;
this.pnlConnectionInfo.Enabled = true;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.lblSyncStatus.Visible = true;
this.lblSyncStatus.BringToFront();
this.pnlConnectionInfo.Enabled = false;
// Lets do the work when we've got some time.
this._Handler = new EventHandler(this.Application_Idle);
Application.Idle += this._Handler;
}
private void btnRefresh_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
DataTable dtServers = SmoApplication.EnumAvailableSqlServers(false);
this.cmbServerName.DataSource = dtServers;
this.cmbServerName.DisplayMember = "Name";
this.cmbServerName.ValueMember = "Name";
this.Cursor = Cursors.Default;
}
private void ListDatabasesInServer()
{
this.cmbDbName.Items.Clear();
// Loop through the databases list
foreach (Database db in this.m_Server.Databases)
{
//We don't want to be adding the System databases to our list
//Check if database is system database
if (!db.IsSystemObject)
{
this.cmbDbName.Items.Add(db.Name); // Add database to combobox
}
}
this.cmbDbName.SelectedIndex = 0;
//We have the list now
}
private void rdbServerAuthentication_Click(object sender, EventArgs e)
{
this.txtUserName.Enabled = true;
this.txtPassword.Enabled = true;
this.chkSavePassword.Enabled = true;
}
private void rdbWindowsAuthentication_Click(object sender, EventArgs e)
{
this.txtUserName.Enabled = false;
this.txtPassword.Enabled = false;
//this.chkSavePassword.Enabled = false;
}
private void btnTestConnection_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
try
{
// If a server is selected
if (!string.IsNullOrEmpty(this.cmbServerName.Text))
{
string message = this.ConnectDatabase();
if (string.IsNullOrEmpty(message))
{
//This will populate list of databases on selected server
this.ListDatabasesInServer();
}
else
{
MessageBox.Show(message, "SQL Authetication", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Cursor = Cursors.Default;
return;
}
}
else
{
// A server was not selected, show an error message
this.Cursor = Cursors.Default;
MessageBox.Show("Please select a server first", "Server not selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
catch(Exception ex)
{
this.Cursor = Cursors.Default;
MessageBox.Show(ex.Message, "SQL Authetication", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
this.Cursor = Cursors.Default;
}
private string ConnectDatabase()
{
if (!string.IsNullOrEmpty(this.cmbServerName.Text))
{
try
{
this.m_ServerConnection = new ServerConnection(this.cmbServerName.Text.ToString());
//this.m_ServerConnection
//First Check type of Authentication
if (this.rdbWindowsAuthentication.Checked == true) //Windows Authentication
{
this.m_ServerConnection.LoginSecure = true;
this.m_Server = new Server(this.m_ServerConnection);
}
else
{
// Create a new connection to the selected server name
this.m_ServerConnection.LoginSecure = false;
this.m_ServerConnection.Login = this.txtUserName.Text; //Login User
this.m_ServerConnection.Password = this.txtPassword.Text; //Login Password
this.m_ServerConnection.DatabaseName = this.cmbDbName.Text; //Database Name
// Create a new SQL Server object using the connection we created
this.m_Server = new Server(this.m_ServerConnection);
}
return string.Empty;
}
catch(Exception ex)
{
return ex.Message;
}
}
return "No server selected";
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Server = this.cmbServerName.SelectedValue.ToString();
Properties.Settings.Default.SQLAuthenticationMode = this.rdbServerAuthentication.Checked;
if (!string.IsNullOrEmpty(this.cmbDbName.Text))
{
Properties.Settings.Default.DatabaseName = this.cmbDbName.SelectedItem.ToString();
}
else
{
Properties.Settings.Default.DatabaseName = string.Empty;
}
if (this.chkSavePassword.Checked)
{
Properties.Settings.Default.UserName = this.txtUserName.Text;
Properties.Settings.Default.Password = this.txtPassword.Text;
Properties.Settings.Default.SavePassword = this.chkSavePassword.Checked;
}
else
{
Properties.Settings.Default.UserName = string.Empty;
Properties.Settings.Default.Password = string.Empty;
Properties.Settings.Default.SavePassword = false;
}
string message = ConnectDatabase();
if (!string.IsNullOrEmpty(message))
{
MessageBox.Show(this, message, "SQL Authentication",MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
Properties.Settings.Default.Save();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void cmbServerName_SelectedIndexChanged(object sender, EventArgs e)
{
this.cmbDbName.Items.Clear();
}
}
}