繁体   English   中英

SQL插入查询,全天工作,但现在每次都失败

[英]SQL Insert query, working all day but now fails everytime

什么会导致以下错误的第一次时完全相同的代码(即产生这个错误)今天已经可以正常使用了一整天?


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

- - - - - - - - - - - - - - 好

编码:

this.Cursor = Cursors.WaitCursor;
            try
            {
                // Insert into database
                sqlconnection = new SqlConnection(@"Data Source=" + Properties.Settings.Default.DBHost + ";Initial Catalog=BLAHBLAH;Persist Security Info=True;User ID=" + Properties.Settings.Default.DBUserName + ";Password=" + Properties.Settings.Default.DBPassword + ";");

                sqlconnection.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = sqlconnection;

                int count = 0;

                foreach (var item in files)
                {
                    cmd.CommandText = @"insert into Images (Name, Games) values ('" + item.Value + "', '" + games + "')";
                    cmd.ExecuteNonQuery();

                    count++;
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message);
            }

            sqlconnection.Close();

            this.Cursor = Cursors.Default;

这是Sql服务器网络连接错误。 尝试在sql浏览器中登录还检查您的sql服务是否正在运行,并尝试调试您可以打开连接检查/登录凭据的代码。

首先,我要感谢大家的帮助和建议。 我已经设法解决了这个问题。

事实证明,此代码失败的原因是因为从Properties.Settings.Default.SettingName中检索设置数据存在问题。 设置在那里并保存,但是由于某种原因它无法将设置检索到变量中。

我通过将设置存储在除Properties.Settings之外的其他地方来使此代码起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM