C# 接口实现根据不同的ID去不同表中的值进行比较

首先用VS创建一个控制台,注意,2019不要选择.Net Core控制台应用 选择.Net Framework 控制台应用。
创建好之后我们右键项目添加接口文件
在这里插入图片描述
在这里插入图片描述
选择一个接口文件,注意自己命名,我这里用的是默认的名称
接口代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;

namespace CheckFile
{
    interface Interface1
    {
        bool File(string file_id, string folder_id);
    }
    class MyFlie : Interface1
    {
        public bool File(string file_id, string folder_id)
        {
            string con = @"Data Source =*******;Database=Test; User ID = sa;Pwd = ****** ;";//连接数据库,自己填写服务名称,帐号密码
            SqlConnection conn = new SqlConnection(con);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            string s1 = "select file_path from dms_file where file_id=" + "'"+file_id + "'";
            cmd.CommandText = s1;
            string ss1 = Convert.ToString(cmd.ExecuteScalar());//获得SQL语法所得到的值ss1
            string s2 = "select folder_path from dms_folder where folder_id=" + "'" + folder_id + "'"; 
            cmd.CommandText = s2;
            string ss2 = Convert.ToString(cmd.ExecuteScalar());//获得SQL语法所得到的值ss2
            if (ss1 == ss2)//对两个值进行比较,看是否相等
            {
                 Console.WriteLine("T");
                 return true;
            }
            else
            {
               Console.WriteLine("F");
               return false;
            }      
        }
    }    
}

接口文件写好之后,我们在program赋值(file_id与folder_id)
Program.cs文件代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheckFile
{
    class Program
    {
        static void Main(string[] args)
        {
            MyFlie myFlie = new MyFlie();
            string file_id = "ab1";
            string folder_id = "3";
            myFlie.File(file_id, folder_id);
            Console.ReadKey();
        }
    }
}

参考此方法可以写自己类似的需求
例如取数据表中的某个值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值