简体   繁体   English

如何通过文本框(Windows窗体),C#检查我的数据库中是否存在主键

[英]How to check if the primary key exists in my database from a textbox (windows form), c#

I have a textbox and a button in a windows form application. 我在Windows窗体应用程序中有一个文本框和一个按钮。 I want to check if the primary key (persId) exists in my sql database/dataset (made with Visual studio) when I enter a number in the textbox and press the button. 当我在文本框中输入数字并按下按钮时,我想检查主键(persId)是否存在于我的sql数据库/数据集(由Visual Studio制造)中。 I dont know how to compare the text with persId from the database. 我不知道如何将文本与数据库中的persId进行比较。

If the persId exists I want to fill two textboxes in a new form and show the persId and persName. 如果persId存在,我想以一种新形式填充两个文本框,并显示persId和persName。

I am new to programming in C# so I have probably missed something. 我是C#编程的新手,所以我可能错过了一些东西。 I looked at how to check if value exists in database from textbox c# but could not find an answer. 我研究了如何从文本框c#检查数据库中是否存在值,但找不到答案。

Thanks in advance! 提前致谢!

    public void searchPersId(string persId)
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand myCommand = new SqlCommand("SELECT persId FROM Customers  WHERE persId = @persId", conn); 
        myCommand.Parameters.AddWithValue("@persId", persId);


        if (textBox1.Text = myCommand  ) //I dont know how to compare the values of textbox with myCommand..
        {
            //Show values (persId and persName) in two textBoxes in a new form. 
        }

        else
        {
            MessageBox.Show("The ID does not exist.");
        }


    }

First, use the using -statement for everything implementing IDisposable like the connection to dispose unmanaged resources and to close the connection, even in case of an error. 首先,对实现IDisposable所有对象(例如连接)使用using -statement,以处置非托管资源并关闭连接,即使发生错误也是如此。

Then you have to open the connection and to use ExecuteReader to get a datareader to check if there's at least one record with that ID, you can use reader.HasRows . 然后,您必须打开连接并使用ExecuteReader来获取一个数据读取器,以检查是否存在至少一个具有该ID的记录,您可以使用reader.HasRows You also have to select the persName if you want it as mentioned. 如果需要,还必须选择persName

using(var conn = new SqlConnection())
using(var myCommand = new SqlCommand("SELECT persId, persName FROM Customers  WHERE persId = @persId", conn))
{
    myCommand.Parameters.AddWithValue("@persId", persId);
    conn.Open();
    using(var rd = myCommand.ExecuteReader())
    {
        bool personExists = rd.HasRows;
        if(personExists)
        {
            // advance the reader to the first record, presuming there is only one, otherwise use a loop while(rd.Read)
            rd.Read();
            string persName = rd.GetString(1); // second field
            // ...
        }
        else
        {
            MessageBox.Show("The ID does not exist.");
        }
    }
}

You can also use ExecuteScalar 您也可以使用ExecuteScalar

public void searchPersId(string persId)
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand myCommand = new SqlCommand("SELECT persName FROM Customers  WHERE persId = @persId", conn);
        myCommand.Parameters.AddWithValue("@persId", persId);

        object personName = myCommand.ExecuteScalar();

        if(!string.IsNullOrEmpty(personName.ToString()))
        //if (textBox1.Text = myCommand) //I dont know how to compare the values of textbox with myCommand..
        {
            //Show values (persId and persName) in two textBoxes in a new form. 
            textBox2.Text = personName.ToString();
        }
        else
        {
            MessageBox.Show("The ID does not exist.");
        }
    }

First you have to Execute the command. 首先,您必须执行命令。

SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);



if (dr.HasRows)
{
    // ... if it has rows then you know it match
} 
else 
{
   // ... data doesn't exists
}

Then you can compare the result. 然后,您可以比较结果。

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

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