错误:无法连接到服务器

时间:2016-09-09 15:43:06

标签: c# sql-server

当我尝试运行该程序时它开始正常,但是当我输入正确的答案并点击输入错误时:

Error建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)

下面的代码有问题吗?如果没有错误,我可以做些什么来解决它?

        if (txt1.Text == "A" && txt2.Text == "B" && txt3.Text == "C" && txt4.Text == "D" && txt5.Text == "E" && txt6.Text == "F" && txt7.Text == "G" && txt8.Text == "H")
        {
            var txt = new[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8};
            for (int i = 0; i < 8; i++)
            {
                txt[i].Enabled = false;
            }
            SqlConnection conn = new SqlConnection("Server=localhost;Database=abcdefgh;uid=root");
            try
            {
                conn.Open();
                SqlCommand insert = new SqlCommand("INSERT INTO txt_abcdefgh (a_txt1, b_txt2, c_txt3, d_txt4, e_txt5, f_txt6, g_txt7, h_txt8) VALUES('" + txt1.Text + "','" + txt2.Text + "','" + txt3.Text + "','" + txt4.Text + "','" + txt5.Text + "','" + txt6.Text + "','" + txt7.Text + "','" + txt8.Text + ";)", conn);
                insert.ExecuteNonQuery();
                MessageBox.Show("");
                pnlEasy.Visible = true;
                Height = 476;
                Width = 336;
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
        }
        else
        {
            MessageBox.Show("Wrong answer" + "\nTry Again");
        }

0 个答案:

没有答案