如何将文本框值与c#中的sql数据库值进行比较?

时间:2012-02-15 15:37:03

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

如何将文本框值与c#中的sql数据库值进行比较?

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

1 个答案:

答案 0 :(得分:2)

这是一个代码示例,可以帮助您解决此问题。当然,您可以根据需要尽可能地加以理解,但它会为您提供基础知识 - 根据您的问题提供的数据。

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

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