且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在所有列中搜索文本框文本,并从列中显示其对应的值...

更新时间:2023-12-01 11:01:46

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;