简体   繁体   English

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

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

What could cause the following error for the very first time when the exact same code (that produces this error) has been working perfectly all day today? 什么会导致以下错误的第一次时完全相同的代码(即产生这个错误)今天已经可以正常使用了一整天?


--------------------------- A network-related or instance-specific error occurred while establishing a connection to SQL Server. ---------------------------建立与SQL Server的连接时,发生了与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

--------------------------- OK - - - - - - - - - - - - - - 好

The code: 编码:

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;

This is Sql server network connection error. 这是Sql服务器网络连接错误。 try to login in sql browser also check your sql service is running or not and try to debug your code that are you able to open connection check/login credentials. 尝试在sql浏览器中登录还检查您的sql服务是否正在运行,并尝试调试您可以打开连接检查/登录凭据的代码。

Firstly, I'd like to thank everyone for their help and suggestions. 首先,我要感谢大家的帮助和建议。 I've managed to solve this problem. 我已经设法解决了这个问题。

As it turns out, the reason this code was failing was because there was an issue with retrieving setting data from Properties.Settings.Default.SettingName. 事实证明,此代码失败的原因是因为从Properties.Settings.Default.SettingName中检索设置数据存在问题。 The settings are there and saved, but it wasn't able to retrieve the settings into variables for some reason. 设置在那里并保存,但是由于某种原因它无法将设置检索到变量中。

I got this code to work by storing settings somewhere else other than Properties.Settings. 我通过将设置存储在除Properties.Settings之外的其他地方来使此代码起作用。

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

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