ADO.NET之判断重复记录

44 篇文章 1 订阅

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IsDistinct
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        //连接数据库的字符串
        string connstr = @"Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DataBase\db_TomeTwo.mdf;
Integrated Security = SSPI;Persist Security Info=False";

        public int isHomologyNote(string Table, string term1, string term2, string Value1, string Value2)
        {
            string tem_sql = "";//定义字符串变量
            System.Data.OleDb.OleDbConnection tem_conn =
                new System.Data.OleDb.OleDbConnection(connstr);//连接数据库
            System.Data.OleDb.OleDbCommand tem_comm;//定义OleDbCommand类
            tem_conn.Open();//打开数据库的连接
            //设置SQL语句,查找要添加的记录
            tem_sql = "select top 1 * From " + Table + " where " + term1 + " = '" +
                Value1 + "' and " + term2 + " = '" + Value2 + "'";
            tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//执行SQL语句
            int RecordCount = 0;//定义变量
            if (tem_comm.ExecuteScalar() == null)//如果查询为空
                RecordCount = 0;
            else
                RecordCount = (int)tem_comm.ExecuteScalar();//返回查找结果的个数
            tem_conn.Close();//关闭连接
            tem_comm.Dispose();//释放资源
            tem_conn.Dispose();//释放资源
            return RecordCount;//返回查询记录数量
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isHomologyNote("tb_BasicTable", "Number", "Name", textBox1.Text, textBox2.Text) > 0)//如果查询结果大于0
                MessageBox.Show("已有重复记录");//弹出提示框
            else//可以对该记录进行添加
                MessageBox.Show("无记录,可以添加");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值