我在查询构建器c#中需要帮助 - 删除不起作用

时间:2009-11-30 15:43:33

标签: c#

我在c#和ms-access中有一个小项目。我使用查询构建器来管理ms-access中的表。

问题是,选择查询效果很好,更新查询效果很好,但删除不起作用且没有错误信息!

请帮忙。

 public DataSet Update(DataSet ds)
        {
            using (cn)
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                string queryString = "SELECT [taskId],[resourceId] FROM [mytable]";
                cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _conStrName + ";User Id=admin;Password=;";
                OleDbConnection connection = new OleDbConnection(cn.ConnectionString);
                adapter.SelectCommand = new OleDbCommand(queryString, connection);
                adapter.SelectCommand.CommandText = queryString;

                OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

                adapter.Update(ds.Tables["mytable";"]);

                return ds;
            }

public DataSet Update(DataSet ds)
        {
            using (cn)
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                string queryString = "SELECT [taskId],[resourceId] FROM [mytable]";
                cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _conStrName + ";User Id=admin;Password=;";
                OleDbConnection connection = new OleDbConnection(cn.ConnectionString);
                adapter.SelectCommand = new OleDbCommand(queryString, connection);
                adapter.SelectCommand.CommandText = queryString;

                OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

                adapter.Update(ds.Tables["mytable";"]);

                return ds;
            }
        ==================================

2 个答案:

答案 0 :(得分:0)

使用:

DELETE FROM Store_Information WHERE store_name = "Los Angeles"

答案 1 :(得分:0)

如果您正在使用OleDb,请尝试以下方法:

    public static int DeleteStudentInfo(long studentNum)
    {
        var cmd = new OleDbCommand("DELETE FROM Students WHERE studentID = @studentId");
        cmd.Parameters.Add("@studentId", OleDbType.BigInt).Value = studentNum;
        return CallNonQuery(cmd);
    }

    private static int CallNonQuery(OleDbCommand query)
    {
        int rowsAffected;
        var conn = new OleDbConnection(ConfigSettingsManager.DBConnectionString);
        query.Connection = conn;
        try
        {
            conn.Open();
            rowsAffected = query.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return -1;
        }
        finally
        {
            conn.Close();
        }
        return rowsAffected;
    }

用您的连接字符串替换ConfigSettingsManager.DBConnectionString。