using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public string Str;
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            int length = 1;
            int count = 1;
            while(count>0)
            {
            Str = textBox1.Text.ToString();
            string Str1 = Str.Substring(0, length);
            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:/Dict.mdb;User ID=admin;Password=;Jet OLEDB:Database Password=123456";
            OleDbConnection objConnection = new OleDbConnection(strConnection);
            objConnection.Open();
            string StrCon = "select count(*) from dict where Han like '" + Str1 + "%'";
            OleDbCommand objCommand = new OleDbCommand(StrCon, objConnection);
            count = Convert.ToInt32(objCommand.ExecuteScalar());
            length = length + 1;
            if (length > Str.Length)
            {
                listBox1.Items.Add(Str1);
                break;
            }
            else
            {
                string Strsy = Str.Substring(Str1.ToString().Length, Str.ToString().Length);
                listBox2.Items.Add(Strsy);
            }            }
            
        }
    }
}我的这段程序的目的是在数据库里查询textbox控件里的字符串(比如输入的字符串是“给国家和社会造成巨大损失”,我的程序就是要先查第一个字“给”,如果库里有这个字,就接着查“给国”,如果库里有,就接着查“给国家”比如查到“给国家和社会造”,数据库里没有这个词,那么将“给国家和社会”这个字符串写入listBox1,把“造成巨大损失”写入listBox2)请教大家我的这段代码应该怎么改才符合我的要求,谢谢!

解决方案 »

  1.   

    存储过程写把.
     select * from XX where XX like %'给'%
    ......
    select * from XX where XX like %'给国'%
      

  2.   


    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Data.OleDb; namespace WindowsApplication3 

        public partial class Form1 : Form 
        { 
            public string Str; 
            public Form1() 
            { 
                InitializeComponent(); 
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                int length = 0; 
                int count = 1; 
                
                string Str = textBox1.Text.ToString(); 
                string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:/Dict.mdb;User ID=admin;Password=;Jet OLEDB:Database Password=123456"; 
                OleDbConnection objConnection = new OleDbConnection(strConnection); 
                objConnection.Open(); 
                OleDbCommand objCommand = null;            while(count > 0) 
                {
                     length = length + 1;   
                     string Str1 = Str.Substring(0, length);
                     string StrCon = "select count(1) from dict where Han like '" + Str1 + "%'"; 
                     objCommand = new OleDbCommand(StrCon, objConnection); 
                     count = Convert.ToInt32(objCommand.ExecuteScalar));                          
                }             listBox1.Items.Clear();
                listBox2.Items.Clear();            listBox1.Items.Add(Str.SubString(0, length - 1)); 
                listBox2.Items.Add(Str.SubString(length - 1));
            } 
        } 

      

  3.   


    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Data.OleDb; namespace WindowsApplication3 

        public partial class Form1 : Form 
        { 
            public string Str; 
            public Form1() 
            { 
                InitializeComponent(); 
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                int length = 1; 
                int count = 1; 
                while(count>0) 
                { 
                Str = textBox1.Text.ToString(); 
                string Str1 = Str.Substring(0, length); 
                string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:/Dict.mdb;User ID=admin;Password=;Jet OLEDB:Database Password=123456"; 
                OleDbConnection objConnection = new OleDbConnection(strConnection); 
                objConnection.Open(); 
                string StrCon = "select count(*) from dict where Han like '" + Str1 + "%'"; 
                OleDbCommand objCommand = new OleDbCommand(StrCon, objConnection); 
                count = Convert.ToInt32(objCommand.ExecuteScalar()); 
                length = length + 1; 
                if (count >0) 
                { 
                    listBox1.Items.Add(Str1); 
                } 
                else 
                { 
                    string Strsy = Str.Substring(Str1.ToString().Length, Str.ToString().Length); 
                    listBox2.Items.Add(Strsy); 
                    break; 
                }             } 
                
            } 
        } 
      

  4.   

    string StrCon = "select count(*) from dict where Han like '%" + Str1 + "%'"; 
    是这个意思吗?