c# - 如何在 c# 中从我的数据库中获取信息(没有 SqlConnection-Class)
问题描述
你好 Stackoverflow 社区,我是 C# 和数据库的新手,我有以下问题:
我正在编写一个 Windows 窗体应用程序,其中一个窗体是登录屏幕,用户可以在其中将他的名字输入到文本字段中。我想检查该名称是否已存在于我的数据库中,并且不知道如何将文本字段信息移交给数据库。
我看过一些教程,但它们都在使用SqlConnectionClass
. 我通过 Visual Studio 界面(查看 > 其他窗口 > 数据源)将我的项目与数据库连接起来。
你能告诉我如何解决这个问题吗?
解决方案
我不知道你的窗体设计,但我希望下面的代码会有所帮助:)
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);
}
}
推荐阅读
- java - 从外部类更改 TabbedPane 索引
- google-apps-script - 使用从谷歌表格中提取的数据从模板创建谷歌文档文档
- android - 集成使用 Kotlin Coroutines 依赖问题构建的库模块
- python - 使用 PySoundFile,如何获取字节缓冲区?
- r - 如何在 R 中更改 mrset 的列名?
- r - 如何编写一个 for 循环来从包含 NA 的数据集中计算模型残差?
- java - 我的 Exomedia 代码在某些设备上不起作用,我该如何解决?
- php - 如何将从数据库中随机选择的图像放置在页面上的随机位置?
- python - python中用于检测Python中缺失值的用户定义函数?
- java - Android 应用程序客户端与 java 服务器的相互 TLS