SQL错误:无法加载文件或程序集'Microsoft.SqlServer.SqlClrProvider,

时间:2017-02-07 09:25:45

标签: c# .net sql-server windows visual-studio

美好的一天。 我收到了这个错误,我已经尝试了在论坛上建议的所有内容,但没有成功。 单击保存按钮时,我的应用程序会创建一个SQL连接文件。这是我得到的错误: 无法加载文件或程序集

  

Microsoft.SqlServer.SqlClrProvider,Version = 13.100.0.0,Culture =   中性,PublickeyToken = 89845dcd8080cc91'或其依赖项之一。   系统找不到指定的文件。

代码:

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

private void btnSave_Click(object sender, EventArgs e)
{
    try
   {
        if (cmbServerName.Text == "")
        {
            MessageBox.Show("Please Select/Enter Server Name","SQL Server Settings", MessageBoxButtons.OK,MessageBoxIcon.Information);
            cmbServerName.Focus();
            return;
        }
        if (cmbAuthentication.SelectedIndex == 1)
        {
            if (txtUserName.Text == "")
            {
                MessageBox.Show("please enter user name", "SQL Server Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
               txtUserName.Focus();
                return;
            }
            if (txtPassword.Text == "")
            {
                MessageBox.Show("please enter password", "SQL Server Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPassword.Focus();
                return;
            }
        }
        Cursor = Cursors.WaitCursor;
        timer1.Enabled = true;
        if (cmbAuthentication.SelectedIndex == 0)
        {
           con = new SqlConnection(@"Data source= '" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
        }
        if (cmbAuthentication.SelectedIndex == 1)
        {
            con = new SqlConnection(@"Data Source=  '" + cmbServerName.Text.ToString() + "';Initial Catalog=master;User ID=  '" + txtUserName.Text.ToString() + "';Password='" + txtPassword.Text.ToString() + "'");
        }
        con.Open();
        if ((con.State == ConnectionState.Open))
        {
          //  MessageBox.Show("please enter password", "SQL Server Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (MessageBox.Show("It will create the Database and configure the sql server, Do you want to proceed?", "SQL Server Settings", MessageBoxButtons.YesNo , MessageBoxIcon.Information) == DialogResult.Yes)
            {
                using (StreamWriter sw = new StreamWriter(Application.StartupPath + "\\SQLSettings.dat"))
                {
                    if (cmbAuthentication.SelectedIndex == 0)
                    {
                        sw.WriteLine(@"Data Source= '" + cmbServerName.Text.ToString() +"';Initial Catalog=DigitalCourtRollDB;Integrated Security=True");
                        sw.Close();
                    }
                    if (cmbAuthentication.SelectedIndex == 1)
                    {
                        sw.WriteLine(@"Data Source= '" + cmbServerName.Text.ToString() + "';Initial Catalog=DigitalCourtRollDB;User ID='" + txtUserName.Text.ToString()+"' ;Password= '" + txtPassword.Text.ToString() + "'");
                        sw.Close();
                    }
                    CreateDB();
                }
            }
            else {
                System.Environment.Exit(0);
            }
        }
        MessageBox.Show("The Database has been created and SQL Server setting has been saved successfully..." + "\r\n" + "Application will be closed,Please start it again", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        System.Environment.Exit(0);
   }
    catch (Exception ex)
   {
        MessageBox.Show("Unable to connect to sql server" + "\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
    finally
    {
       if ((con.State == ConnectionState.Open))
       {
           con.Close();
        }
    }
}
void CreateDB()
{
    try
    {
        con = new SqlConnection(@"Data source= '"+ cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
        con.Open();
        string cb2 = "Select * from sysdatabases where name='DigitalCourtRollDB'";
        cmd = new SqlCommand(cb2);
        cmd.Connection = con;
        rdr = cmd.ExecuteReader();
        if (rdr.Read())
        {
            con = new SqlConnection(@"Data source='" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
            con.Open();
            string cb1 = "Drop Database DigitalCourtRollDB";
            cmd = new SqlCommand(cb1);
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            con.Close();
            con = new SqlConnection(@"Data source='" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
            con.Open();
            string cb = "Create Database DigitalCourtRollDB";
            cmd = new SqlCommand(cb);
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            con.Close();
            using (StreamReader sr = new StreamReader(Application.StartupPath + "\\DBScript.sql"))
            {
                st = sr.ReadToEnd();
                Server server = new Server(new ServerConnection(con));
                server.ConnectionContext.ExecuteNonQuery(st);
            }
        }
        else {
            con = new SqlConnection(@"Data source='" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
            con.Open();
            string cb3 = "Create Database DigitalCourtRollDB";
            cmd = new SqlCommand(cb3);
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            con.Close();
            using (StreamReader sr = new StreamReader(Application.StartupPath + "\\DBScript.sql"))
            {
                st = sr.ReadToEnd();
                Server server = new Server(new ServerConnection(con));
                server.ConnectionContext.ExecuteNonQuery(st);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if ((con.State == ConnectionState.Open))
        {
            con.Close();
        }
    }
}

以前有人遇到过这个问题吗? 我正在使用c#Visual Studio 2015. SQL server 2016。

非常感谢你。

0 个答案:

没有答案