Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL错误:无法加载文件或程序集';Microsoft.SqlServer.SqlClrProvider,_C#_.net_Sql Server_Windows_Visual Studio - Fatal编程技术网

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

C# SQL错误:无法加载文件或程序集';Microsoft.SqlServer.SqlClrProvider,,c#,.net,sql-server,windows,visual-studio,C#,.net,Sql Server,Windows,Visual Studio,你好。 我犯了这个错误,我尝试了论坛上的所有建议,但没有成功。 单击“保存”按钮时,我的应用程序将创建一个SQL连接文件。这就是我得到的错误: 无法加载文件或程序集 Microsoft.SqlServer.SqlClrProvider,版本=13.100.0.0,区域性= 中性,PublickeyToken=89845dcd8080cc91'或其依赖项之一。 系统找不到指定的文件 代码: 以前有人遇到过这个问题吗? 我正在使用c#Visual Studio 2015。SQL server 201

你好。 我犯了这个错误,我尝试了论坛上的所有建议,但没有成功。 单击“保存”按钮时,我的应用程序将创建一个SQL连接文件。这就是我得到的错误: 无法加载文件或程序集

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

代码:

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


非常感谢您。

您有Microsoft.SqlServer.SqlClrProvider的参考资料吗?它位于什么名称空间中。您似乎只导入了Microsoft.SqlServer.Management.Smo&Common?您缺少的程序集似乎实际安装在GAC中。这已经在前面的问题中讨论过了,谢谢。已经修好了。参考文献的版本不同。
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();
        }
    }
}