将数据行与字符串进行比较 [英] Compare a datarow with strings

查看:88
本文介绍了将数据行与字符串进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是,我有一个数据库和应用程序,应用程序将数据加载到数据库,但问题是我经常得到重复的值,所以在我将值添加到数据库之前,我想浏览所有记录在数据库中查看是否有任何记录与我想要添加的行匹配。我想比较theese 3字符串和我的数据库中每行的前3个字符串。我该怎么办?



The idea is that i`ve got a database and and application, the application loads data into the database but the problem is that I often get duplicate values, so before i added the values to the database i want to browse all records in the DB and see if any record matches the line i want to add. I want to compare theese 3 strings with the first 3 collumns of every row in my databale. How do I do that?

string third = array[0];
string first = array[1];
string second = array[2];

string vudz = "SELECT name,lastname,dob FROM players";

NpgsqlDataAdapter npgsqldataadapter1 = new NpgsqlDataAdapter(new NpgsqlCommand(vudz, conection));
                               
npgsqldataadapter1.Fill(datatable11);
DataRow drow = datatable11.NewRow();
datatable11.Rows.Add();





/编辑代码块由Jibesh / Edit



/Edit Code Block corrected by Jibesh /Edit

推荐答案

为什么不运行查询:



Why not just run a query:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT COUNT(*) FROM players WHERE [name]=@AR1 AND lastname=@AR2 AND dob=@AR3", con))
        {
        com.Parameters.AddWithValue("@AR1", first);
        com.Parameters.AddWithValue("@AR2", second);
        com.Parameters.AddWithValue("@AR3", third);
        if( com.ExecuteScalar > 0)
            {
            // It exists already
            }
        }
    }


这篇关于将数据行与字符串进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆