本文介绍了如何检查是否记录存在与否和MS Access数据库在C#中插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,如果记录存在与否,如果它存在,我不想插入如果我BOT要插入在MS Access数据库中的数据在C#。

I want to check if record exists or not if it exists i dont want to insert if it bot i want to insert the data in ms access database in c#.

        OleDbCommand cmd = new OleDbCommand("insert into MyTable values('" + test + "','" + test + "','" + "123" + "');", con);
        OleDbCommand cmd1 = new OleDbCommand("select * from MyTable", con);
        temp = 0;
        try
        {
            con.Open();
            string count = (string)cmd1.ExecuteScalar();
            temp = cmd.ExecuteNonQuery();
            if (temp > 0)
            {
                MessageBox.Show("One Record Added");
            }
            else
            {
                MessageBox.Show("Record not added");
            }


        }
        catch
        { }

任何人都可以建议我一些代码。

Can Anyone suggest me some code.

先谢谢了。

推荐答案

过滤一些关键的基础上,你的SELECT查询。检查是否返回的存在或特定的记录不存在,做所需的处理。

Filter your Select query on the basis of some key . Check if it returns for existence or non-existence of the particular record and do the processing required .

 string cmdStr = "Select count(*) from MyTable where id = 1"; //get the existence of the record as count 

 OleDbCommand cmd = new OleDbCommand(cmdStr, conn);

  int count = (int)cmd.ExecuteScalar();

  if(count >0)
  {
         //record already exist 
  }

修改此行

  OleDbCommand cmd1 = new OleDbCommand("select * from MyTable", con);

这篇关于如何检查是否记录存在与否和MS Access数据库在C#中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:49