提示suggest

1  数据库 汉字字母的转换函数

 

2  改变数据源 然后重新绑定下

 

create table tb1
(
id 
integer,
name 
varchar(20)
)

 

 

 

代码
GO
create   function   [dbo].f_GetPy(@str   nvarchar(4000)) 
returns   nvarchar(4000
as 
begin 
declare   @strlen   int,@re   nvarchar(4000
declare   @t   table(chr   nchar(1)   collate   Chinese_PRC_CI_AS,letter   nchar(1)) 
insert   into   @t(chr,letter) 
    
select   '吖 '''   union   all   select   '八 '''   union   all 
    
select   '嚓 '''   union   all   select   '咑 '''   union   all 
    
select   '妸 '''   union   all   select   '发 '''   union   all 
    
select   '旮 '''   union   all   select   '铪 '''   union   all 
    
select   '丌 '''   union   all   select   '咔 '''   union   all 
    
select   '垃 '''   union   all   select   '嘸 '''   union   all 
    
select   '拏 '''   union   all   select   '噢 '''   union   all 
    
select   '妑 '''   union   all   select   '七 '''   union   all 
    
select   '呥 '''   union   all   select   '仨 '''   union   all 
    
select   '他 '''   union   all   select   '屲 '''   union   all 
    
select   '夕 '''   union   all   select   '丫 '''   union   all 
    
select   '帀 ''' 
    
select   @strlen=len(@str),@re= ' ' 
    
while   @strlen> 0 
    
begin 
        
select   top   1   @re=letter+@re,@strlen=@strlen-1 
            
from   @t   a   where   chr <=substring(@str,@strlen,1
            
order   by   chr   desc 
        
if   @@rowcount=0 
            
select   @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1 
    
end 
    
return(@re
end

 


 

 

 

 

扩展方法连数据库 执行语句

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace db
{
  
public static   class Extend
    {
      
public static DataTable ExecuteToDataTable(this string strsql)
      {
          
string StrConn = "Data Source=.;user=sa;password=123456;Initial Catalog=master";
          SqlConnection Connection 
= new SqlConnection(StrConn);
          
if (Connection.State != ConnectionState.Open)
              Connection.Close();
          Connection.Open();

          SqlCommand cmd 
= new SqlCommand();
          cmd.CommandText 
= strsql;

          SqlDataAdapter Adapter 
= new SqlDataAdapter(cmd.CommandText, Connection);
         DataSet ds 
= new DataSet();
      
          Adapter.Fill(ds);
          Connection.Close();
          
return ds.Tables[0];
      }

    }
}

 

 

 

from : textBox  Combox

 

代码
    public partial class Form1 : Form
    {

        DataTable dt 
= null;

        
public Form1()
        {
            InitializeComponent();

            comboBox1.DisplayMember 
= "Name";
            comboBox1.ValueMember  
= "Id";
}
        
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         
        }
        
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dt 
= FindByName(textBox1.Text.Trim());
            comboBox1.DataSource 
= dt;
            comboBox1.DroppedDown 
= true;
        }

        
public DataTable  FindByName(string goodsName)
        {
            
string sql = string.Format("select * from tb1 where Name like '%{0}%' or dbo.f_GetPy(Name) like '{0}%' ", goodsName);
            
return sql.ExecuteToDataTable();
        }

 

 

posted @ 2010-07-26 23:38  Ry5  阅读(232)  评论(0编辑  收藏  举报