locked
c# how to insert and update dater-base RRS feed

  • Question

  • ok so i have the following code 

                    string connString;
                    connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};";
                    string test = processorid + mbored;
    
                    SqlConnection Con = new SqlConnection(connString);
    
                    SqlCommand Cmd = new SqlCommand("INSERT INTO elfenliedtopfan5_login " +
                "(machineid) " +
                        "VALUES(@machineid)",
                Con);
    
    
                    Cmd.Parameters.Add("@machineid", System.Data.SqlDbType.Int);
    
    
                    Cmd.Parameters["@machineid"].Value = test;
    
                    Con.Open();
    
                    int RowsAffected = Cmd.ExecuteNonQuery();
    
                    Con.Close();
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
    
                }

    but i get the following error when trying to insert data into the database

    {"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)"}

    not quite sure what this is happening as checked the connection string and this connects fine but for some reason i cant seem to get it to update any data or add data to it. 

    Tuesday, July 28, 2020 7:04 PM

All replies

  • You need to enable named pipes communications on MS SQL Server, which is communcations between two processes on the local machine your program and MS SQL Server.

    https://www.tutorialspoint.com/inter_process_communication/inter_process_communication_named_pipes.htm

    https://www.blackbaud.com/files/support/infinityinstaller/content/installermaster/tkenablenamedpipesandtcpipconnections.htm

    Tuesday, July 28, 2020 7:47 PM
  • Hi,

    Thank you for posting here.

    I encountered the same error a few days ago. I closed the Sql Server service for some reason and then forgot to reopen it. After I reopened it, the problem disappeared.

    Therefore, please check whether the Sql Server service on your machine is in a normal open state.

    If your error is not for this reason, you can try to find a solution in the link below.

    Solving Connectivity errors to SQL Server

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 29, 2020 1:51 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 4, 2020 2:56 AM
  • Hi

    Your error message shows for connection problem.

    It is difficult to say where is the problem .

    Check the connection string properly.

    Follow the link and try to troubleshoot.

    https://support.microsoft.com/en-in/help/4009936/solving-connectivity-errors-to-sql-server

    Thanks and regards
    Wednesday, August 5, 2020 3:28 PM