首页 > 解决方案 > 如何在 c# 中从我的数据库中获取信息(没有 SqlConnection-Class)

问题描述

你好 Stackoverflow 社区,我是 C# 和数据库的新手,我有以下问题:

我正在编写一个 Windows 窗体应用程序,其中一个窗体是登录屏幕,用户可以在其中将他的名字输入到文本字段中。我想检查该名称是否已存在于我的数据库中,并且不知道如何将文本字段信息移交给数据库。

我看过一些教程,但它们都在使用SqlConnectionClass. 我通过 Visual Studio 界面(查看 > 其他窗口 > 数据源)将我的项目与数据库连接起来。

你能告诉我如何解决这个问题吗?

标签: c#databaseformswinformssqlconnection

解决方案


我不知道你的窗体设计,但我希望下面的代码会有所帮助:)

SqlConnection conn = new SqlConnection(@"here goes your connection string"); // select your data source goto properties and you may see your connection string over there
        SqlDataAdapter sda = new SqlDataAdapter("select count(*) from your_table_name where username ='" + textBox1.Text + "' and password='" + textBox2.Text + "'", conn);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows[0][0].ToString() == "1")
        {
            this.Hide();
            Form2 f = new Form2();
            f.Show();
        }
        else
        {
            MessageBox.Show("please enter correct username and password", "alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

推荐阅读