如何在C#中从数据库中获取信息(没有SqlConnection-Class)

时间:2018-06-22 08:34:09

标签: c# database forms winforms sqlconnection

您好Stackoverflow社区,我是C#和数据库的新手,但我遇到以下问题:

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

我看到了一些教程,但是它们都与SqlConnectionClass一起使用。  我通过Visual Studio界面(视图>其他Windows>数据源)将项目与数据库连接起来。

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

1 个答案:

答案 0 :(得分:0)

我对您的窗口表单设计一无所知,但我希望以下代码会有所帮助:)

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);
        }
    }