在所有列中搜索文本框文本,并从列中显示其对应的值... [英] searching a text box text in all column and showing its corresponding value from column...

查看:63
本文介绍了在所有列中搜索文本框文本,并从列中显示其对应的值...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......

我在编程方面比较大.因此,请轻松,清楚地回答我..

我创建了一个小的Windows应用程序.在我的SQL数据库表中,有两列..名字和姓氏.具有两个文本框和一个按钮的形式.我的问题是,当我在第一个文本框中输入文本并单击按钮时,我必须查询包含输入文本的哪一列,如果它在FirstName列中,则对应的LastName必须显示在第二个文本框中,并且反向也..

在此先谢谢您..

hi....

I am bigner in programming. So please answer me easly and clearly..

I created a small windows application. In my SQL db table two columns are there.. FirstName and LastName. In form having two text boxes and one button. My question is when i am entering a text in first textbox and clicking the button at that time i have to serch which column contains the entered text and if it is in FirstName column, then the correspond LastName have to display in the second textbox and the reverse also..

Thanks in advance..

推荐答案

CREATE PROCEDURE [dbo].[GetDetails]
 (
  @firstName VARCHAR(50)
 ,@lastName VARCHAR(50)
 )
AS

BEGIN

DECLARE @sql AS VARCHAR(100)
SET @sql = 'SELECT FirstName , LastNme FROM YOURTABLE WHERE '
IF @firstName !=''
    SET @sql = @sql + ' firstname LIKE %'+ @firstName + '%'


 IF @lastName !=''
    SET @sql = @sql + ' lastname LIKE %'+ @lastName  + '%'
END


创建此过程以返回指定的行:
Create this procedure to return the specified row:
Create Procedure GetFullName
@Name  nVarchar(50)
As
  Select FirstName,
         LastName
         From  Table_Name
         Where FirstName = @Name Or LastName = @Name


然后执行该操作,如果有一行,则可以检查用户是否输入了firstName,因此textBox2.Text将为lastName,也可以相反.


then execute that, and if there was a row so you can check if the user, entered firstName so textBox2.Text will be lastName and also the reverse is possible.

string firstName = string.Empty, lastName = string.Empty;

SqlConnection connection = new SqlConnection("write connectionString here");

SqlCommand command = new SqlCommand(connection);
command.Text = "GetFullName";
command.CommandType = CommandType.StoreProcedure;

try
{
   command.Connection.Open();
   SqlDataReader reader = command.ExecuteReader();

   if (reader.Read())
   {
       firstName = (string)reader["FirstName"];
       lastName = (string)reader["LastName"];
   }
   else
       MessageBox.Show("No match found");
   reader.Close();
}
catch { }
finally
{
   if (command.Connection.State == ConnectionState.Open)
       command.Connection.Close();
}

textBox2.Text == textBox1.Text == firstName ? lastName : firstName;


这篇关于在所有列中搜索文本框文本,并从列中显示其对应的值...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆