gpt4 book ai didi

c# - SQL 插入查询,整天都在工作,但现在每次都失败

转载 作者:太空宇宙 更新时间:2023-11-03 15:57:45 24 4
gpt4 key购买 nike

当完全相同的代码(产生此错误)今天一整天都完美工作时,第一次会导致以下错误的原因是什么?


--------------------------- A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

--------------------------- OK

代码:

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 server 网络连接错误。尝试在 sql 浏览器中登录,同时检查您的 sql 服务是否正在运行,并尝试调试您能够打开连接检查/登录凭据的代码。

关于c# - SQL 插入查询,整天都在工作,但现在每次都失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com