简体   繁体   English

C#:如何针对字符串检查DataRow

[英]C# : How to check a DataRow against a String

I current have a connection open which just queries through with a dependency of user input. 我目前有一个打开的连接,它只是通过用户输入的依赖关系进行查询。 My Objects are correct and I have set-up the Instances related to the Objects and it is all working fine. 我的对象是正确的,并且我已经设置了与对象相关的实例,并且一切正常。

I now need to match if the user input is the same as the stored Database data however, it does not seem to be working; 现在,如果用户输入的内容与存储的数据库数据相同,则需要进行匹配,但是,它似乎不起作用; i am getting no errors though. 我没有任何错误。

After doing a bit of my own debugging and researching, I tried to convert my DataRow to a String using ToString(); 经过一些我自己的调试和研究,我尝试使用ToString();将DataRow转换为String ToString(); but still no luck. 但仍然没有运气。

Could anyone possible take a look into this section of code and possibly help me try to figure this out? 任何人都可以看看这部分代码,并可能帮助我设法弄清楚这一点吗? Many thanks. 非常感谢。

DataSet ds = new DataSet();
query = new MySqlDataAdapter(SQL, conn);
query.Fill(ds, "AllData");
DataTable dt = new DataTable();
dt = ds.Tables["AllData"];
DataRow[] r = dt.Select();
int i = 0;
while (i != r.Length)
    {
     string toTest = r[i]["BusinessID"].ToString();
     if(toTest == sinput)
    {

Note: sinput is the Users input and r[i]["BusinessID"] contains the data in the database which I am trying to match against the input string. 注意: sinput是用户输入,而r[i]["BusinessID"]包含数据库中我要与输入字符串匹配的数据。

Edit: I have not passed anything into the dt.Select() for security purposes as I do not want direct user input into a query. 编辑:出于安全目的,我没有将任何内容传递给dt.Select() ,因为我不希望用户直接输入查询。

Please find the code:Instead of data from database i have hard code the value. 请找到代码:我有硬编码值,而不是数据库中的数据。 Hope,it will help you. 希望对您有帮助。

        string Name = "Test1";
        int Id = 1;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Id",typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Rows.Add(1,"Test");
        dt.Rows.Add(1, "Test1");
        DataRow[] r = dt.Select();
        int i = 0; //Not Required.
        while (r.Length>0)
        {
            string toTest = r[i]["Name"].ToString();
            int toTest1 =Convert.ToInt32(r[i]["Id"]);
            if (toTest == Name)
            {
                Console.WriteLine(toTest);
            }
            if (toTest1 == Id)
            {
                Console.WriteLine(toTest1);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM