数据库名是YFanDriver
数据库连接帐号是sa
数据库连接密码是sausing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace YFanDriver
{
    public partial class LoginForm : Form
    {        public LoginForm()
        {
            InitializeComponent();
            
        }        private void button1_Click(object sender, EventArgs e)
        {        }               
        }
}

解决方案 »

  1.   

    sqlserverLink.xml文件<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="DataBaseType" value="Sql Server 2000" />
        <add key="Data Source" value="(local)" />
        <add key="Initial Catalog" value="YFanDriver" />
        <add key="User ID" value="sa" />
        <add key="Password" value="sa" />
      </appSettings>
    </configuration>
      

  2.   

    Data Source=计算机名称/或者ip地;Initial Catalog=YFanDriver;User ID=sa;Password=sa
      

  3.   

    string userName = "sa";
                string password = "sa";
                string dataSource = "127.0.0.1";
                string sampleDatabaseName = "YFanDriver";
                // Create a connection string for the master database
                SqlConnectionStringBuilder connString1Builder;
                connString1Builder = new SqlConnectionStringBuilder();
                connString1Builder.DataSource = dataSource;
                connString1Builder.InitialCatalog = sampleDatabaseName;
                connString1Builder.Encrypt = true;
                connString1Builder.TrustServerCertificate = false;
                connString1Builder.UserID = userName;
                connString1Builder.Password = password;
                SqlConnection conn = new SqlConnection(connString1Builder.ToString()) connString1Builder.ToString();
      

  4.   

    sqlserverForm.cs文件界面
    button三个:
    btnLinkGet 获取连接
    btnLinkCheck 测试连接
    btnLinkSave 保存连接
    label四个:
    label1 服务器名称
    label2 用户名
    label3 密码
    label4 数据库名称
    textbox两个:
    txtUserName
    txtPassword
    ComboBox两个:
    cbbServerName
    cbbDatabaseName
      

  5.   

    sqlserverForm.cs文件代码//项目里面要引用SQLDMO.DLL//添加引用
    using System.Xml;
    using System.IO;#region 数据库连接操作        private void btnLinkGet_Click(object sender, EventArgs e)
            {
                string strXmlFileName = System.IO.Directory.GetCurrentDirectory() + @"\sqlserverFolder\sqlserverLink.xml";
                if (File.Exists(strXmlFileName))
                {
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.Load(strXmlFileName);
                    XmlNode xNode;
                    XmlElement xElem1;
                    XmlElement xElem2;
                    XmlElement xElem3;
                    XmlElement xElem4;
                    xNode = xDoc.SelectSingleNode("//appSettings");
                    xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='Data Source']");
                    xElem2 = (XmlElement)xNode.SelectSingleNode("//add[@key='Initial Catalog']");
                    xElem3 = (XmlElement)xNode.SelectSingleNode("//add[@key='User ID']");
                    xElem4 = (XmlElement)xNode.SelectSingleNode("//add[@key='Password']");
                    cbbServerName.Text = xElem1.GetAttribute("value").ToString();
                    txtUserName.Text = xElem3.GetAttribute("value").ToString();
                    txtPassword.Text = xElem4.GetAttribute("value").ToString();
                    getDatabaseName();
                    cbbDatabaseName.Text = xElem2.GetAttribute("value").ToString();
                    MessageBox.Show("获取连接成功!", MsgTitle);
                }
                else
                {
                    MessageBox.Show("连接未设置或未保存!请先设置连接参数,并点击“保存连接”按钮!", MsgTitle);
                }
            }        private void btnLinkCheck_Click(object sender, EventArgs e)
            {
                SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
                SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
                try
                {
                    srv.Connect(this.cbbServerName.Text.ToString(), this.txtUserName.Text, this.txtPassword.Text);
                    if (cbbDatabaseName.Text.Length > 0)
                    {
                        MessageBox.Show("服务器连接成功!", MsgTitle);
                    }
                    else
                    {
                        MessageBox.Show("服务器连接失败!", MsgTitle);
                    }
                }
                catch
                {
                    MessageBox.Show("服务器连接失败!", MsgTitle);
                }
            }        private void btnLinkSave_Click(object sender, EventArgs e)
            {
                linkSave();
            }        private void cbbServerName_Click(object sender, EventArgs e)
            {
                getServerName();
            }        private void cbbDatabaseName_Click(object sender, EventArgs e)
            {
                getDatabaseName();
            }        public void linkSave()
            {
                string strXmlFileName = System.IO.Directory.GetCurrentDirectory() + @"\sqlserverFolder\sqlserverLink.xml";
                XmlDocument xmldoc = new XmlDocument();
                //声明
                XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xmlnode.InnerText += " encoding=\"utf-8\"";
                xmldoc.AppendChild(xmlnode);
                //添加根节点
                XmlElement xeConfiguration = xmldoc.CreateElement("", "configuration", "");
                xmldoc.AppendChild(xeConfiguration);
                //添加参数节点
                XmlNode nd = xmldoc.SelectSingleNode("//configuration");
                XmlElement xeAppSettings = xmldoc.CreateElement("", "appSettings", "");
                nd.AppendChild(xeAppSettings);
                //添加参数一
                XmlNode nd1 = xmldoc.SelectSingleNode("//appSettings");
                XmlElement xeConnectionString1 = xmldoc.CreateElement("add");
                XmlAttribute xaKey1 = xmldoc.CreateAttribute("key");
                XmlAttribute xaValue1 = xmldoc.CreateAttribute("value");
                xaKey1.Value = "DataBaseType";
                xaValue1.Value = "Sql Server 2000";
                xeConnectionString1.Attributes.Append(xaKey1);
                xeConnectionString1.Attributes.Append(xaValue1);
                nd1.AppendChild(xeConnectionString1);
                //添加参数二
                XmlNode nd2 = xmldoc.SelectSingleNode("//appSettings");
                XmlElement xeConnectionString2 = xmldoc.CreateElement("add");
                XmlAttribute xaKey2 = xmldoc.CreateAttribute("key");
                XmlAttribute xaValue2 = xmldoc.CreateAttribute("value");
                xaKey2.Value = "Data Source";
                xaValue2.Value = cbbServerName.Text;
                xeConnectionString2.Attributes.Append(xaKey2);
                xeConnectionString2.Attributes.Append(xaValue2);
                nd2.AppendChild(xeConnectionString2);
                //添加参数三
                XmlNode nd3 = xmldoc.SelectSingleNode("//appSettings");
                XmlElement xeConnectionString3 = xmldoc.CreateElement("add");
                XmlAttribute xaKey3 = xmldoc.CreateAttribute("key");
                XmlAttribute xaValue3 = xmldoc.CreateAttribute("value");
                xaKey3.Value = "Initial Catalog";
                xaValue3.Value = cbbDatabaseName.Text;
                xeConnectionString3.Attributes.Append(xaKey3);
                xeConnectionString3.Attributes.Append(xaValue3);
                nd3.AppendChild(xeConnectionString3);
                //添加参数四
                XmlNode nd4 = xmldoc.SelectSingleNode("//appSettings");
                XmlElement xeConnectionString4 = xmldoc.CreateElement("add");
                XmlAttribute xaKey4 = xmldoc.CreateAttribute("key");
                XmlAttribute xaValue4 = xmldoc.CreateAttribute("value");
                xaKey4.Value = "User ID";
                xaValue4.Value = txtUserName.Text;
                xeConnectionString4.Attributes.Append(xaKey4);
                xeConnectionString4.Attributes.Append(xaValue4);
                nd4.AppendChild(xeConnectionString4);
                //添加参数五
                XmlNode nd5 = xmldoc.SelectSingleNode("//appSettings");
                XmlElement xeConnectionString5 = xmldoc.CreateElement("add");
                XmlAttribute xaKey5 = xmldoc.CreateAttribute("key");
                XmlAttribute xaValue5 = xmldoc.CreateAttribute("value");
                xaKey5.Value = "Password";
                xaValue5.Value = txtPassword.Text;
                xeConnectionString5.Attributes.Append(xaKey5);
                xeConnectionString5.Attributes.Append(xaValue5);
                nd5.AppendChild(xeConnectionString5);            xmldoc.Save(strXmlFileName);
                MessageBox.Show("保存连接成功!", MsgTitle);
            }        public void getServerName()
            {
                SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
                SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
                for (int i = 0; i < sqlServers.Count; i++)
                {
                    object srv = sqlServers.Item(i + 1);
                    if (srv != null)
                    {
                        this.cbbServerName.Items.Add(srv);
                    }
                }
                if (this.cbbServerName.Items.Count > 0)
                    this.cbbServerName.SelectedIndex = 0;
                else
                    this.cbbServerName.Text = "<No available SQL Servers>";
            }        public void getDatabaseName()
            {
                SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
                SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
                try
                {
                    srv.Connect(this.cbbServerName.Text.ToString(), this.txtUserName.Text, this.txtPassword.Text);
                    foreach (SQLDMO.Database db in srv.Databases)
                    {
                        if (db.Name != null)
                            this.cbbDatabaseName.Items.Add(db.Name);
                    }
                }
                catch
                {
                    MessageBox.Show("服务器访问失败!", MsgTitle);
                }
            }        #endregion