当前位置:编程学习 > C#/ASP.NET >>

求:ASP.net(c#)验证SQL数据库表里的字段是否存在代码,我是新手

下面是我的连接数据库的代码 :
public static SqlConnection conn_sql()
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = "Data Source=PC-20090404IVED;Initial Catalog=chen;Persist Security Info=True;User ID=zhangpy;Password=123123";
cnn.Open();
return cnn;
}
public static DataTable GetData(string sql)
{
SqlDataAdapter da = new SqlDataAdapter(sql, conn_sql());
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}

public static long ModiData(string sql)
{
SqlCommand cmm = new SqlCommand(sql, conn_sql());
long rows = cmm.ExecuteNonQuery();
return rows;
}
现在我在网页上建立一个按钮,当点击这个按钮的时候执行验证我指定textBox里的内容在数据库表里的USERNAME字段里是否存在,如果存在,提示该用户已经存在,如果不存在则提示可以注册。
现在我想求这个按钮下的这段验证的代码。(textbox ID是TB_user,数据库是用SQL建立的,数据库名称:chen,数据库里的表名称是:sujubiao)
追问:你的代码不行诶~
答案:
你去读去系统表:
USE [MDSystemDataBaseTwo] --我自己的数据库,改些成你的数据库名字
select * from syscolumns where object_name(id) = '表名字'--把表名字替换成你的表名字

这个语句返回的是 '表名字' 中的所有字段,有了所有字段,判断存在什么的 很简单了
希望对你有帮助
SqlConnection cnn = conn_sql();
SqlCommand cmm = new SqlCommand("select count(*) from sujubiao where USERNAME='" + TB_user.Text + "'", cnn);
if(Convert.ToInt32(cmm.ExecuteScalar()) != 0)
{
//存在,提示用户
ClientScript.RegisterStartupScript(GetType(), "info", "alert('用户已存在!')", true);
}

上一个:在ASP的html页面中怎么用代码形式来设置提交按钮,提交文档,提交服务器
下一个:asp.net 代码中写 para[0].Direction=ParameterDirection.Output;数据库中要怎么写;就是想读取那个值

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,