如何逐个数字地搜索文本框输入的数字 [英] How to search a textbox entered number digit by digit

查看:82
本文介绍了如何逐个数字地搜索文本框输入的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索一个文本框,从后面开始逐个数字地输入数字,就像使用数组一样,然后在gridview中显示结果。



数字是6173456 ...

现在按钮点击它应首先搜索整个no即6173456,然后...

如果找到了确定...在gridview中显示...否则先查找五个不是617345 ......而且喜欢明智......



我尝试过的事情:



使用System;

使用System.Data;

使用System.Configuration;

使用System。 Web;

使用System.Web.Security;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Web.UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.Net;

使用System.Net.Sockets;

使用System.Data.Odbc;

使用System.Net.Mail;

使用System.Text;

使用System.IO;

使用System.Drawing;

使用System.Collections.Generic;

使用System.Linq;



public partial class _Default:System。 Web.UI.Page

{

DataSet ds = new DataSet();

DataSet ds1 = new DataSet();

DataSet ds2 = new DataSet();

protected void Page_Load(object sender,EventArgs e)

{



}



private void bind()

{

int [] array = new int [50];

char [] sep = {'0','1','2','3','4','5','6','7' ,'8','9'};



String [] numbers = TextBox1.Text.Split(sep);

// char [] sample = TextBox1.Text.ToCharArray();

// int i = Convert.ToInt32(TextBox1.Text);

// int [] i = Convert.ToInt32(TextBox1.Text).ToCharArray();

for(int i = 0;我< 7; i ++)

{

// array [i] = int.Parse(sample [i] .ToString());

// array [i] = Int32.Parse(numbers [i]);

array = array = TextBox1.Text.Split()。选择(h => Int32.Parse(h))。ToArray ();

// DataSet ds = new DataSet();

OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings [connect1]。ConnectionString);

OdbcCommand cmd = new OdbcCommand(选择前缀为前缀,目的地为目的地,费率为来自分配的费率,其中prefix ='+ array [i] +',con);

con.Open();

OdbcDataAdapter da = new OdbcDataAdapter(cmd);

da.Fill(ds);

con。关闭();







if(ds.Tables [0] .Rows.Count> 0)

{

GridView1.DataSource = ds;

GridView1.DataBind();



}



}



}












protected void Button1_Click(object sender,EventArgs e)

{

bind();

}

}

I want to search a textbox entered number digit by digit from back like using array and then show that results in gridview.

Like number is 6173456...
now on button click it should first search the whole no i.e 6173456,then...
if found ok...show it in gridview...otherwise find first five no's 617345....and like wise so on....

What I have tried:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using System.Data.Odbc;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;

public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{

}

private void bind()
{
int[] array = new int[50];
char[] sep = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

String[] numbers = TextBox1.Text.Split(sep);
//char[] sample = TextBox1.Text.ToCharArray();
//int i = Convert.ToInt32(TextBox1.Text);
// int[] i = Convert.ToInt32(TextBox1.Text).ToCharArray();
for (int i = 0; i < 7; i++)
{
//array[i] = int.Parse(sample[i].ToString());
//array[i] = Int32.Parse(numbers[i]);
array = array = TextBox1.Text.Split().Select(h => Int32.Parse(h)).ToArray();
//DataSet ds = new DataSet();
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
OdbcCommand cmd = new OdbcCommand("select prefix as Prefix , destination as Destination, rates as Rates from allrates where prefix = '" + array[i] + "'", con);
con.Open();
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
da.Fill(ds);
con.Close();



if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();

}

}

}






protected void Button1_Click(object sender, EventArgs e)
{
bind();
}
}

推荐答案

对于初学者,不要这样做。永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。



一种方法是使用子字符串,因为Split会删除匹配的字符,因此您将获得一个空字符串数组比任何更短的东西。

For starters, don't do it like that. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

One way to look at this is to use a substring, because Split removes the matching characters, so you will get an array of empty strings rather than anything shorter.
public static string[] GetShorterNumbers(string numbers)
    {
    int len = numbers.Length;
    string[] result = new string[len];
    for (int i = 0; i < len; i++)
        {
        result[i] = numbers.Substring(0, len - i);
        }
    return result;
    }

然后您可以选择如何执行此操作:在获得匹配之前依次为每个字符串发出SQL命令,或者使用OR子句执行所有操作立刻,并采取最长的结果。

Then you have a choice how to do this: either issue SQL commands for each string in turn until you get a match, or use an OR clause to do them all at once, and take the longest result.


这篇关于如何逐个数字地搜索文本框输入的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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