条件SQL语句 - 在插入/更新查询之间切换

时间:2013-12-13 08:49:40

标签: c# sql ms-access oledb

有没有办法在这里将if / else子句插入以下行:

command.CommandText = "UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3";

如果可以找到 ID = value3 (换句话说,行已存在),则会发生更新查询,但无法找到它,我想要进行插入查询:

command.CommandText = "INSERT INTO Table1 (ID,Team) VALUES (value1,value2)";

要发生......我怎么能这样做?

private void button1_Click(object sender, EventArgs e)
{
    // save to access database when user clicks on the save button
    using (OleDbConnection conn = new OleDbConnection())
    {
        //the file path of mdb
        string filepath = @"C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb";
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + filepath + ";";
        OleDbCommand command = new OleDbCommand();
        command.Connection = conn;
        //your update satemenet
        command.CommandText = "UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3";
        conn.Open();
        //update to ms access
        command.ExecuteNonQuery();
        conn.Close();
    }
}

5 个答案:

答案 0 :(得分:2)

由于command.ExecuteNonQuery();将返回受影响的行数,您可以检查从update命令返回的内容,如果该记录不存在,则该方法将返回0,然后您可以进入insert方法:

private string filepath = @"C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb";
private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + filepath + ";";

private void button1_Click(object sender, EventArgs e)
{       
    if (this.Update() == 0)
    {
        this.Insert();
    }
}

private int Update()
{   
    using (OleDbConnection conn = new OleDbConnection(connectionString))
    using (OleDbCommand command = new OleDbCommand("UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3", conn);
    {
        conn.Open();
        return command.ExecuteNonQuery();
    }
}
private int Insert()
{       
    using (OleDbConnection conn = new OleDbConnection(connectionString))
    using (OleDbCommand command = new OleDbCommand("INSERT INTO Table1 (ID,Team) VALUES (value1,value2)", conn);
    {
        conn.Open();
        return command.ExecuteNonQuery();
    }
}

要清除对答案的评论,OleDbConnection.Dispose()方法如下所示:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}

因此,Dispose方法将关闭连接,因此当您的连接位于using块中时,无需显式调用Close()

答案 1 :(得分:0)

听起来像是在MERGE语句之后......在JET版本的SQL中不存在。这对大多数事情都有效:

UPDATE Table1 RIGHT JOIN Table2
ON Table1.[KeyField] = Table2.[KeyField]
SET Table1.[KeyField] = Table2.[KeyField],
Table1.[OtherField] = Table2.[OtherField]

如果它位于Table2,则会将其插入Table1,覆盖值(更新),如果它已在Table1中。

答案 2 :(得分:0)

很简单。在运行SELECT COUNT(*)INSERT之前,通过UPDATE查询检查是否存在ID = 3。

答案 3 :(得分:0)

您可以先使用ExecuteNonQuery()执行UPDATE查询,然后返回受影响的行数。

如果受影响的行为0,那么您将运行INSERT查询。

            //the file path of mdb
            string filepath = @"C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb";
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + filepath + ";";
            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            //your update satemenet
            command.CommandText = "UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3;";
            conn.Open();
            //update to ms access
            int affectedRows = command.ExecuteNonQuery(); // returns 1 if the row exist, 0 if it does not.

            if (affectedRows == 0)
            {
                  command.CommandText = "INSERT INTO Table1 (ID,Team) VALUES (value1,value2)";
                  command.ExecuteNonQuery(); // perform insert
            }

            conn.Close()
编辑:正如戈德汤普森指出的那样,最初的解决方案无效。 ExecuteScalar()带有“UPDATE .....; SELECT @@ ROWCOUNT”。将其更改为使用ExecuteNonQuery,并从该方法获取受影响的行数。

使用@@ ROWCOUNT的旧解决方案:

            command.CommandText = "UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3; SELECT @@ROWCOUNT;";
            conn.Open();
            //update to ms access
            int affectedRows = (int)command.ExecuteScalar(); // returns 1 if the row exist, 0 if it does not.

            if (affectedRows == 0)
            {
                  command.CommandText = "INSERT INTO Table1 (ID,Team) VALUES (value1,value2)";
                  command.ExecuteNonQuery(); // perform insert
            }

答案 4 :(得分:-1)

使用@@ ROWCOUNT

command.CommandText = "UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3 IF @@ROWCOUNT = 0 INSERT INTO Table1 (ID,Team) VALUES (value1,value2)"

编辑:正如戈德所说这不起作用,我太快了,没注意到它是Access。

This 是一个类似的问题和解决方案。