【问题标题】:if/else statement for insert command parameters after select command is implemented c#?c#实现select命令后插入命令参数的if/else语句?
【发布时间】:2013-03-27 14:48:56
【问题描述】:

访问 2003 VS 2010 C#

请有人帮我做一个 else 语句。我已经使用 if 语句来检查是否存在重复记录,并且我在使用插入命令参数时如何使用 else 语句而苦苦挣扎。提前致谢

这里是方法

  OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;

        using (var command = myCon.CreateCommand())
            {
                command.CommandText = "select * from SomeTable where ID = @ID";
                command.Parameters.AddWithValue("ID", int.Parse(txtID.Text));

                myCon.Open();
                var reader = command.ExecuteReader();
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            txtID.Text = reader["ID"].ToString();

                        }
                    } MessageBox.Show("ID Already exist");
                    else (reader.HasRows !=null // need here pls. 
                    {

                      cmd.CommandText = "INSERT INTO SomeTable (ID, AgeGroup, Gender) VALUES (@ID, @AgeGroup, @Gender)";
                      cmd.Parameters.AddWithValue("@ID", txtID.Text);
                      cmd.Parameters.AddWithValue("@AgeGroup", cBAgeGroup.Text);
                      cmd.Parameters.AddWithValue("@Gender", cBGender.Text);
                      cmd.Connection = myCon;
                      cmd.ExecuteNonQuery();
                    }
                 }

                }
                  //  cmd.Connection = myCon;
                  //  cmd.ExecuteNonQuery();
                    myCon.Close();

更新 2 我在 else 语句中移动了 cmd 连接和 cmd.ExecuteNonQuery。我能够检查 id 是否已经存在并能够插入新记录。所以代码正在按原样执行。

感谢大家的建议。

【问题讨论】:

  • @SonerGönül - 更好?
  • 既然用c#打了标签,也就不用写了..
  • 我不完全确定发生了什么,但您似乎嵌套了两个命令。如果是我,我会将 INSERT INTO 的代码移到它自己的 using 语句中。我会单独调试它以确保它正常工作。您可以使用布尔值,以便如果您的阅读器有行,则将布尔值设置为 true。执行另一个 if 语句来检查布尔值并插入。
  • 感谢Soner提醒

标签: c# ms-access


【解决方案1】:

MessageBox 不属于ifelse 之间,您可以直接使用else

if (reader.HasRows)
{
    while (reader.Read())
    {
        txtID.Text = reader["ID"].ToString();

    }
} MessageBox.Show("ID Already exist");         // <--- remove this here
else (reader.HasRows !=null // need here pls.  // <--- remove this here

因此,将消息框放入if,您已在其中检测到具有给定 id 的行:

if (reader.HasRows)
{
    MessageBox.Show("ID Already exist");
    // ...

然后你可以在else 中插入新记录,因为你已经知道HasRows 的反义词是“没有行”:

else
{
  cmd.CommandText = "INSERT INTO SomeTable (ID, AgeGroup, Gender) VALUES (@ID, @AgeGroup, @Gender)";
  // ...

但是,我不确定这是否是您代码中的唯一问题。

更新其实还有其他问题,看看这段完全重构的代码:

string selectSql = "SELECT * FROM SomeTable WHERE ID = @ID";
string insertSql = "INSERT INTO SomeTable (ID, AgeGroup, Gender) VALUES (@ID, @AgeGroup, @Gender)";

int id;
if (!int.TryParse(txtID.Text, out id))
    MessageBox.Show("ID must be an integer.");
else
{
    using (var myCon = new OleDbConnection(connectionString))
    {
        bool idExistsAlready = true;
        using (var selectCommand = new OleDbCommand(selectSql, myCon))
        {
            selectCommand.Parameters.AddWithValue("@ID", id);
            myCon.Open();
            using (var reader = selectCommand.ExecuteReader())
                idExistsAlready = reader.HasRows;
        }
        if (idExistsAlready)
            MessageBox.Show("ID exists already.");
        else
        {
            using (var insertCommand = new OleDbCommand(insertSql, myCon))
            {
                insertCommand.Parameters.AddWithValue("@ID", id);
                insertCommand.Parameters.AddWithValue("@AgeGroup", cBAgeGroup.Text);
                insertCommand.Parameters.AddWithValue("@Gender", cBGender.Text);
                int affectedRecords = insertCommand.ExecuteNonQuery();
            }
        }
    }
}

【讨论】:

  • 好的,谢谢。我收到“未为命令对象设置命令文本”的错误。所以我正在调查这个。所以你怀疑是对的。
  • 那么 if (!int.TryParse(txtID.Text, out id)) 行是什么意思?,更具体地说是 out id?
  • @bucketblast: int.TryParsetxtID.Text 中的字符串转换为integer。如果可以解析,则返回true 并设置out 变量。否则返回false。查看 msdn 以获取有关 out 参数的信息:msdn.microsoft.com/en-us/library/ee332485.aspx 在这种方式下,我预先检查用户是否在 TextBox 中输入了有效的(整数)ID。如果输入无效,则无需触摸数据库。
  • 感谢您的链接。你会不同意我这样做的方式。我的软件编程技能是不是我的强项?所以我不知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 2014-06-28
  • 2016-05-27
  • 1970-01-01
  • 2021-04-24
  • 1970-01-01
相关资源
最近更新 更多