在单个文本框中搜索不同的数据类型值 [英] search different datatype value in single textbox

查看:87
本文介绍了在单个文本框中搜索不同的数据类型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码在datagridview中搜索

i have using this code for searching in datagridview

dataGridView1.Rows.Clear();
con.Open();
SqlCommand cmd = new SqlCommand("select * from enquery where sname='" + textBox1.Text + "' or contectno='" + textBox1.Text + "' or courese='" + textBox1.Text + "'or remark='" + textBox1.Text + "' or edate='" + textBox1.Text + "' or id='" + textBox1.Text + "' or fudate='" + textBox1.Text + "' or fustatus='" + textBox1.Text + "' or cancel='" + textBox1.Text + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
object[] row = { dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7], dr[8] };
dataGridView1.Rows.Add(row);
}
con.Close();



如果sql中的所有字段都是相同的数据类型但是如果这些字段是不同的数据类型,示例sname是varchar contectno是bigint所以它显示异常,任何人都可以帮我缩短这个问题。我只有一个文本框,用户可以搜索任何字段。


this code is working if all the field in sql are same datatype but not working if those field are different data type , example sname is varchar contectno is bigint so it show exception , can anybody help me short out this problum.i have only one textbox and user can search by any field.

推荐答案

你只能使用 nvarchar()数据类型我会接受字母和数字......





即你的数据库中的 convert(nvarchar(max),textbox)
u can use only nvarchar() datatype I t will accept both alphabets and numerics..


i.e convert(nvarchar(max),textbox) in your db..


这篇关于在单个文本框中搜索不同的数据类型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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