D
D
Dmitry Fondomakin2017-07-05 12:21:45
SQL
Dmitry Fondomakin, 2017-07-05 12:21:45

C# [SOLVED] Why isn't data being updated in SQL when requested by a program?

Hello.
There was a situation incomprehensible to me, in the written program the data in the SQLExpess database is not updated, while the same query is executed without problems in ManagementStudio SQL-Server2012.
The data should be updated on button click:

private void button4_Click(object sender, EventArgs e)
        {
            if (strWindowsWork != "0")
            { // Работает окно
                sql_update_SectionWork_YES();
                sql_up();
                //label1.Text = "131313131";
            }
            else
            { // Не работает окно
                sql_update_SectionWork_NO();
                
                sql_up();
                label1.Text = strWindowsWork;
            }
        }

Where:
sql_update_SectionWork_YES(); - a class for changing the status of the "Working" window.
private void sql_update_SectionWork_YES()
        {
            //throw new NotImplementedException();

            // Подключаемся к базе данных SQL Server (начало)

            string nameIP = "192.168.0.77";
            string nameDB = "\\SQLEXPRESS";
            string userDB = "sa";
            string passBD = "193755";

            SqlConnection cnn = new SqlConnection(@"Data Source=" +
                nameIP + nameDB + "; User ID=" +
                userDB + ";Password=" + passBD);
            try
            {
                cnn.Open();

                // Формируем запрос к базе данных - запрашиваем информацию о ЛПУ

                // Запрашиваем из настроек данные о рабочем месте (начало)
                string strCodeLPUunic = Properties.Settings.Default.CodeLPUunic;
                string strSectionNum = Properties.Settings.Default.SectionNum;
                string strSectionSubNum = Properties.Settings.Default.SectionSubNum;
                string strSectionWindowsNum = Properties.Settings.Default.SectionWindowsNum;
                // Запрашиваем из настроек данные о рабочем месте (конец)

                // Запрос получает одно значение из БД

                //string sql = "UPDATE [dbo].[InfoSectionWindows] SET [SectionWork] = '3' WHERE [CodeLPU]='42501' AND [SectionNum]='А' AND [SectionSubNum]='1' AND [SectionWindowsNum]='3'";
                string sql = "UPDATE InfoSectionWindows SET SectionWork = '3' WHERE CodeLPU='42501' AND SectionNum='А' AND SectionSubNum='1' AND SectionWindowsNum='3'";

                SqlCommand command3 = new SqlCommand(sql, cnn);

                //// Очистка

                cnn.Close();
            }
            catch (Exception ex)
            {
                label1.Text = "Нет соединения с БД ! ";
            }


            cnn.Close();

            // Подключаемся к базе данных SQL Server (конец)
        }

class sql_up(); is responsible for checking the status of the window in place and making an entry in the local settings to reduce the load on the database. However, it is not worth considering, because. works well in other places. And checking the database data through ManagementStudio shows that cell updates in the database when executing the sql_update_SectionWork_YES () class; not happening.
Moreover, if we take the request itself, including the commented one, then in ManagementStudio they are executed without errors. The exception doesn't work either.
So I don't understand what exactly is not working.
PS: As always, the main problem is in crooked hands. )) The SQL query was not correctly formed. Loyal:
string sql = "UPDATE [myclinic_online_step].[dbo].[InfoSectionWindows] SET [SectionWork] = '1' WHERE [CodeLPU]='42501' AND [SectionNum]='А' AND [SectionSubNum]='1' AND [SectionWindowsNum]='2'";

The main mistake is the lack of a link to the database itself UPDATE [myclinic_online_step] . ManagementStudio skips such a request, the program does not.
Thanks for the help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hidden_pingvin, 2017-07-05
@hidden_pingvin

After initializing the SqlCommand, you need to execute
command3.ExecuteNonQuery();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question