【问题标题】:How to compare textbox value with the sql database value in c#?如何将文本框值与 C# 中的 sql 数据库值进行比较?
【发布时间】:2012-02-15 15:37:02
【问题描述】:

c#中如何比较文本框值和sql数据库值?

我是初学者,我必须做一个项目。我只知道如何将 sql 数据库与 c# 项目连接起来。 并告诉我任何教程、链接或任何可以帮助我的东西。

【问题讨论】:

  • 在 MSDN 上查找 SqlCommandTextBox.Text 的文档。试一试,如果您对代码有具体问题,请回来。

标签: sql-server-2008 c#-4.0


【解决方案1】:

这里有一个代码示例,可以帮助您解决这个问题。当然,您可以根据需要对此进行修饰,但它会为您提供基础知识——鉴于我从您的问题中获得的数据。

        if (string.IsNullOrEmpty(textBox1.Text))
        {
            MessageBox.Show("Please enter a value into the text box.");
            this.textBox1.Focus();
            return;
        }

        SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
        connectionStringBuilder.DataSource = ".";
        connectionStringBuilder.InitialCatalog = "TEMP";
        connectionStringBuilder.IntegratedSecurity = true;

        SqlConnection connection = new SqlConnection(connectionStringBuilder.ToString());
        SqlCommand command = new SqlCommand("SELECT Column1 FROM TableA WHERE PKColumn = 1", connection);
        connection.Open();
        string value = command.ExecuteScalar() as string;
        connection.Close();

        if (textBox1.Text.Equals(value))
        {
            MessageBox.Show("The values are equal!");
        }
        else
        {
            MessageBox.Show("The values are not equal!");
        }

如果您对这个问题有其他一些细节,我可能会给您一个更具体的例子。

【讨论】:

  • 我对你上面的代码有疑问,如何将这段代码if (textBox1.Text.Equals(value)) { //show comparison result in datagrid here }中的比较结果放到datagridview中?谢谢。
猜你喜欢
  • 2017-03-11
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
相关资源
最近更新 更多