相关文章推荐
腼腆的企鹅  ·  SQL ...·  2 月前    · 
大方的柚子  ·  python ...·  9 月前    · 
大鼻子的筷子  ·  linux ...·  10 月前    · 

我想实现这样一个功能:
在我用Windows Forms窗体界面验证用户登录信息时,想从我SQLServer服务器的数据库中的一张数据表查询有没有这个用户的相关信息,如果有,则在消息框中提示有这个人;如果没有,则在消息框中说没有这个人。
注:使用的SQLServer版本为2019,使用的代码编辑器为Visual Studio 2019,在Visual Studio中已连接此服务器。 253369-%E6%8D%95%E8%8E%B7.png

非常感谢帮助。

Translating your question into English

Query the data table in SQLServer in C#

I want to implement such a feature:
When I use the Windows Forms interface to verify a user's login information, I want to query a data table in my SQLServer server database to see if there is information about this user, and if so, I will prompt this person in the message box; If not, say in the message box that there is no such person.
Note: The SQLServer version used is 2019, the code editor used is Visual Studio 2019, and this server is connected to Visual Studio.

Hi @KerryZY

first this is an English spoken/written forum, it would be nice if you could write your question in English.

second - my translator said - you want to implement a Form and want to get some information from this SQL Server...

For example you could use this tutorial for connecting your application to SQL Server and grep those information:
https://www.c-sharpcorner.com/UploadFile/5d065a/tutorial-1sql-server-database-connection-in-window-form/

If this is not your plan, then tell us more details about your question/problem.

I don't know you, your knowledge and experience, and regarding your initial text, "just" described the goals you want to achieve... I don't know how much you already have finished, so a good starting point is to connect to the SQL Server; this is where the tutorial is starting, and that's the reason I posted the link.

If you are looking for more details on how to write that function querying data from that SQL Server after the connection is established, then Jiales' answer might be more appropriate.

Hi @KerryZY , Welcome to Microsoft Q&A.

Please refer to the same question I replied in Stack overflow.

Simple login and verification

Edit:

private void Button1_Click(object sender, EventArgs e)   
    //Personal test database  
    string myconn = "your conn";  
    SqlConnection conn = new SqlConnection(myconn);  
    string sql= $"select * from Test.dbo.demoAccount where userid=@UserId";  
        conn.Open();  
        SqlCommand sqlCommand = new SqlCommand(sql, conn);  
        sqlCommand.Parameters.Add("@UserId", SqlDbType.VarChar, 8).Value = AccountTb.Text.Trim();      
        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();  
        if (sqlDataReader.HasRows) //Satisfy the user name , enter the next interface  
            MessageBox.Show("There is this person.");  
           MessageBox.Show("There is no such person");  
        conn.Close();  
    catch (Exception o)  
        MessageBox.Show(o.ToString());  

Best Regards,
Jiale

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.