2012-02-15 155 views
0

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

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

+0

在MSDN上查找“SqlCommand”和“TextBox.Text”的文档。如果您有关于您的代码的具体问题,请回去。 – Blorgbeard 2012-02-15 15:39:03

回答

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

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

+0

我有一个关于你的代码上面的问题,如何将比较结果放在这个代码中'if(textBox1.Text.Equals(value)) { //在datagrid这里显示比较结果 }'到datagridview中?谢谢。 – 2013-01-14 17:08:50

相关问题