首页 > 解决方案 > 如果 c# sqlClient 中有空表单,请不要保存信息

问题描述

我正在为论文制作我们的系统,并且正在尝试保存用户的帐户。我的条件是,如果有一个空表格,帐户将不会被保存,它会显示一个消息框。我试图把它放在一个if元素中,“如果 aForm.Text.Trim() != null”信息/帐户将被保存到数据库中。但是当我试图留下一个空表格并单击保存时,它仍然保存在数据库中。

这是代码 * SQL 中的所有列都设置为“非空”

private void aAddUserSave_Click(object sender, EventArgs e)
    {
        // it will save the data if all of the forms is not == to null
        // or empty spaces
        // and before the info will be saved
        // the confirm password should be == to the password

        if (aAddUserFirstName.Text.Trim() != null && aAddUserLastName.Text.Trim() != null && aAddUserAddress.Text.Trim() != null && aAddUserContact.Text.Trim() != null && aAddUserPass.Text.Trim() != null &&
            aAddUserPassConfirm.Text.Trim() != null && aAddUserForgotAnswer.Text.Trim() != null && aAddUserPassConfirm.Text == aAddUserPass.Text)
        {
            // location and the command for the database
            string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\makoy2017\Documents\My files\School Files\Veron System\System Files\DO NOT DELETE\veronServer.mdf;Integrated Security=True;Connect Timeout=30";
            string query = "INSERT INTO usersAccount (firstName, lastName, address, contactNo, position, password, forgotAnswer) values('" + this.aAddUserFirstName.Text + "', '" + this.aAddUserLastName.Text + "', '" + this.aAddUserAddress.Text +
                "', '" + this.aAddUserContact.Text + "', '" + this.aAddUserPosition.SelectedIndex + "', '" + this.aAddUserPass.Text + "', '" + this.aAddUserForgotAnswer.Text + "');";

            // connecting to the database
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
            SqlDataReader sqlDataReader;

            // try and catch for the saving and reading of the data
            // so if there's a error the system will not close
            // and show the message error
            try
            {
                // open the database connectiont to the system
                sqlConnection.Open();

                // execute the sql command for the sql reader
                sqlDataReader = sqlCommand.ExecuteReader();

                // read all of the data
                while (sqlDataReader.Read())
                {

                }

                // show user saved
                // when the data is saved successfully!
                MessageBox.Show("User Saved!");

            }
            catch (Exception ex)
            {
                // this will show the message box
                // what is the error of the data
                // the data will not be saved in the database
                MessageBox.Show(ex.Message);
            }
        }
        else {
            MessageBox.Show("Please check your confirm/password!");
        }
    }

请帮忙。使代码简单,因为我还是一个初学者。先感谢您 :)

标签: c#sqlvisual-studionull

解决方案


null和“空字符串”不是一回事。不过,有一个方便的辅助方法可以帮助进行条件检查。所以代替这个:

if (aAddUserFirstName.Text.Trim() != null)

你可以这样做:

if (!string.IsNullOrWhiteSpace(aAddUserFirstName.Text))

这将确保字符串不是:

  • 无效的
  • 一个空字符串(长度为 0)
  • 一个仅包含空格字符的非空字符串(你正在做什么.Trim()

重要提示:您的代码还有其他问题。首先,您对SQL 注入非常开放。这意味着您的代码正在盲目地执行您的用户想要发送的任何 SQL 代码。不要直接将用户可修改的值与您的 SQL 代码连接起来。相反,使用查询参数并将用户输入视为而不是代码

此外,当您实际上不读取任何数据时,您不需要使用ExecuteReader读取数据。你正在执行一个语句,而不是一个. 改为使用。INSERTSELECTExecuteNonQuery


推荐阅读