一个简单的C#问题

面试精华 2012-01-09 11:04:01
高手们,我想从控制台输入一跳数据,但是输入的不能和数据库里用户有吻合的数据,怎么判断?
...全文
253 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
面试精华 2012-01-10
  • 打赏
  • 举报
回复
我要先输入就需要一个SQLCommand操作数据,要查询就还要以个SqlCommand,这样做貌似不对吧···
狗血大熊猫 2012-01-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 q107770540 的回复:]
输入后 去数据库查询判断一下:

select 1 from table1 where name=@name
[/Quote]
+1
EnForGrass 2012-01-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 q107770540 的回复:]

输入后 去数据库查询判断一下:

select 1 from table1 where name=@name
[/Quote]
Tim出来额,恭喜V5
cjh200102 2012-01-10
  • 打赏
  • 举报
回复
楼上简洁明了
人称猪 2012-01-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qq190679152 的回复:]
引用 9 楼 thebesttome 的回复:
if(查询数据库中是否有数据) 若有 select count(1) from a where
{
return false
}
else
{
插入数据
}
好清晰


StringBuilder SB = new StringBuilder();
string sql = string.Format("select * ……
[/Quote]
面试精华 2012-01-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 thebesttome 的回复:]
if(查询数据库中是否有数据) 若有 select count(1) from a where
{
return false
}
else
{
插入数据
}
[/Quote]


StringBuilder SB = new StringBuilder();
string sql = string.Format("select * from qqq where Name='{0}'",Name);
SqlCommand comm = new SqlCommand(sql,conn);
SqlDataReader da = comm.ExecuteReader();
if(da.Read()){
if (Name.Equals(da["Name"].ToString()))
{

Console.WriteLine("用户名已注册!!!");
}
}
else
{
SB.AppendFormat("insert into qqq values('{0}','{1}')", Name, Pwd);
SqlCommand co = new SqlCommand(Convert.ToString(SB), conn);

Console.WriteLine("用户名不相同!");

}


是这样?
thebesttome 2012-01-10
  • 打赏
  • 举报
回复
if(查询数据库中是否有数据) 若有 select count(1) from a where
{
return false
}
else
{
插入数据
}
kiba518 2012-01-10
  • 打赏
  • 举报
回复
SQLCommand 处理完有返回值的
wz673744509 2012-01-10
  • 打赏
  • 举报
回复
先查询,后判断!
q107770540 2012-01-09
  • 打赏
  • 举报
回复
输入后 去数据库查询判断一下:

select 1 from table1 where name=@name

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧