c#中如何判断文本框的数据已经存在在数据库中

来源:百度知道 编辑:UC知道 时间:2024/09/25 01:20:07
如何判断 textbox1.text中的内容是否存在 在表ABC中 如在则不执行 下面的操作
大哥们,我是小白,越详细越好 ,有好的例子最好啦

添加一个CustomValidator组件
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
string tid = args.Value;
SqlConnection con = new SqlConnection(); //连接数据库
con.ConnectionString = SqlDataSource1.ConnectionString;
con.Open();
//查询添加的教师记录的教师编号语句
string sql = "select * from teacher where tid='" + tid + "'";
SqlCommand com = new SqlCommand(sql, con);
//判断符合条件的记录的数目
int count = Convert.ToInt16(com.ExecuteScalar());
if (count > 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
按照上述的代码照葫芦画瓢就可以了。

将 textbox1.text中的内容到表ABC中查一下!看看返回有没有行就可以了啊

你得明确是想问数据库连接方面的,还是SQL语句方面的,还是如何在TEXTBOX1上触发查询方法。

string sql= "select id,字段 from ABC where 字段=字段" ;
用一个sqlco