如何从数据表和数据库中删除ID

时间:2016-02-28 20:08:03

标签: c# database ms-access datatable

这是我完成的完整代码。所以基本上我正在创建DataTable,然后我将我的DataTable与数据库连接起来。我可以通过ID编辑人,但我不知道如何通过ID删除人。我希望删除整行。

同样,在我编辑DataTable的部分,如果我选择不编辑表,我会得到一些空引用错误

  

对象引用未设置为对象的实例。

我的代码:

        /*
        * Creating DataTable
        */

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("ID", typeof(int)));
        dt.Columns.Add(new DataColumn("Vards", typeof(string)));
        dt.Columns.Add(new DataColumn("Uzvards", typeof(string)));

        /*
        * Connecting to DataBase
        */

        string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"datubaze.accdb\"";
        OleDbConnection con = new OleDbConnection(ConnectionString);
        OleDbCommand cmd = new OleDbCommand("SELECT * FROM PERSONA", con);
        con.Open();
        OleDbDataReader dataReader = cmd.ExecuteReader();

        while (dataReader.Read())
        {
             DataRow dr = dt.NewRow();
             dr["ID"] = dataReader["ID"];
             dr["Vards"] = dataReader["Vards"];
             dr["Uzvards"] = dataReader["Uzvards"];
             dt.Rows.Add(dr);
        }

        con.Close();
        dt.AcceptChanges();
        PrintDataTable(dt);

        Console.WriteLine();


         /* Edit Person in DataTable */

        Console.WriteLine("Kuru ID vēlaties labot?");
        int labosana = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Vai tiešām vēlaties labot šo ierakstu?");
        string vaiLabot = Console.ReadLine();

        if (vaiLabot == "yes")
        {
            Console.WriteLine("Ievadiet jauno vārdu:");
            string vards = Console.ReadLine();

            Console.WriteLine("Ievadiet jauno uzvārdu:");
            string uzvards = Console.ReadLine();

            dt.Rows[labosana-1]["Vards"] = vards;
            dt.Rows[labosana-1]["Uzvards"] = uzvards;
        }
        else
        {
            Console.WriteLine("Jūs atteicāties labot!");
        }

        /*
        * Adding Edited Person to Database
        */

        OleDbCommand upCmd = new OleDbCommand("UPDATE PERSONA SET Vards=?,Uzvards=? WHERE ID=?", con);
        upCmd.Parameters.Add(new OleDbParameter("@Vards",OleDbType.VarChar));
        upCmd.Parameters.Add(new OleDbParameter("@Uzvards", OleDbType.VarChar));
        upCmd.Parameters.Add(new OleDbParameter("@ID", OleDbType.Integer));

        foreach(DataRow dro in dt.GetChanges().Rows)
        {

         if (dro.RowState == DataRowState.Modified)
         {
             upCmd.Parameters[0].Value = dro[1];
             upCmd.Parameters[1].Value = dro[2];
             upCmd.Parameters[2].Value = dro[0];
             con.Open();
             upCmd.ExecuteNonQuery();
             con.Close();
         }

        }

我试了很多代码。但它们似乎都不起作用。尝试通过名称删除这样的内容。

for(int i = dt.Rows.Count-1; i >= 0; i--)
{
    DataRow dr = dt.Rows[i];
    if (dr["Vards"] == "Name")
        dr.Delete();
}

2 个答案:

答案 0 :(得分:2)

使用此代码从数据库中删除Id:

public int DeleteById(int Id)
{
        string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"datubaze.accdb\"";

        OleDbConnection con = new OleDbConnection(ConnectionString);
        OleDbCommand cmd = new OleDbCommand("DELETE FROM PERSONA WHERE ID = @ID", con);
        cmd.Parameters.Add(new OleDbParameter("ID", Id));

        return cmd.ExecuteNonQuery();
}

答案 1 :(得分:0)

更新:试试这个 - 从DataTable中删除

$scope.getLine = function(search){
 arcTouchAPI.getLine(search).then(function(){
      console.log(viewShare.getDetails);
      $scope.details = viewShare.getDetails.details; 
 });

};

或者如果使用LINQ,您也可以执行类似

的操作
    DataRow rowToBeDeleted;
for(int i = dt.Rows.Count-1; i >= 0; i--)
    {
        DataRow dr = dt.Rows[i];
        if (dr["Vards"] == "Name")
rowToBeDeleted = dr;            

    }

dt.Rows.Remove(rowToBeDeleted );