C#,SQL求解

yinliuwang 2011-08-23 11:13:15
string strSql = "";
strSql = "select stuNum from preInfo ";

SqlConnection conn = new SqlConnection("server=.;database=RoomclassLayout;uid=sa;pwd=000000");
conn.Open();
SqlCommand comm = new SqlCommand(strSql, conn);

string mystuNum = comm.ExecuteScalar().ToString();


在以上代码中要判断从数据库提取出的信息strSql是否存在改怎么判断?
谢谢各位啦。。。。
...全文
100 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangdaowu5 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yinliuwang 的回复:]
引用 5 楼 happy09li 的回复:
引用 3 楼 hexytxl 的回复:
判断用户名在数据中是否存在

C# code

string name = Request.QueryString["name"];
string sql = string.Format("select name from Users where name='{0}'", name);
SqlCon……
[/Quote]

Console.write("显示的字符");
ColinMelody 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ohkuy 的回复:]

C# code

//sql语句就有问题
string strSql = "";
strSql = "select Count(stuNum) from preInfo "; //注意了

SqlConnection conn = new SqlConnection("server=.;database=RoomclassLayout;uid=sa;pwd=000000");
co……
[/Quote]


读了好几遍,终于明白了。我想这哥们所说的,就应该是LZ想要的答案吧。~
ohkuy 2011-08-23
  • 打赏
  • 举报
回复

//sql语句就有问题
string strSql = "";
strSql = "select Count(stuNum) from preInfo "; //注意了

SqlConnection conn = new SqlConnection("server=.;database=RoomclassLayout;uid=sa;pwd=000000");
conn.Open();
SqlCommand comm = new SqlCommand(strSql, conn);

int mystuNum = Convert.ToInt32(comm.ExecuteScalar()); //注意了


if(mystuNum>0)
{
MessageBox.Show("这条数据存在!");
}
else
{
MessageBox.Show("这条数据不存在存在!");
}

Change_L 2011-08-23
  • 打赏
  • 举报
回复
你是winform程序吧。Request是web的是向客户端写出内容!建议看一下基础
yinliuwang 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 happy09li 的回复:]
引用 3 楼 hexytxl 的回复:
判断用户名在数据中是否存在

C# code

string name = Request.QueryString["name"];
string sql = string.Format("select name from Users where name='{0}'", name);
SqlConnection conn = new S……
……
[/Quote]

提示出错:当前上下文中不存在名称“Request”
怎么改啊?多谢。。。
暗尘掩月 2011-08-23
  • 打赏
  • 举报
回复
string strSql = "";
strSql = "select stuNum from preInfo ";

SqlConnection conn = new SqlConnection("server=.;database=RoomclassLayout;uid=sa;pwd=000000");
conn.Open();
SqlCommand comm = new SqlCommand(strSql, conn);

object obj= comm.ExecuteScalar();


if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;//不存在
}
else
{
return obj;
}
熙风 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hexytxl 的回复:]
判断用户名在数据中是否存在

C# code

string name = Request.QueryString["name"];
string sql = string.Format("select name from Users where name='{0}'", name);
SqlConnection conn = new S……
[/Quote]

+1
萧炎 2011-08-23
  • 打赏
  • 举报
回复
if(comm==null)
{
不存在
}
else
{
存在
}
赢在执行 2011-08-23
  • 打赏
  • 举报
回复
判断用户名在数据中是否存在

string name = Request.QueryString["name"];
string sql = string.Format("select name from Users where name='{0}'", name);
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Student;User ID=sa;password=123");
conn.Open();
SqlCommand command = new SqlCommand(sql,conn);
SqlDataReader reader = command.ExecuteReader();
try
{
if (reader.Read())
{
Response.Write("ok");//存在
}
}
catch (Exception)
{
Response.Write("fail");//不存在
}
finally {
conn.Close();
}
  • 打赏
  • 举报
回复
LZ发帖前先预览一下内容,说火星语呢?
hxw452918603 2011-08-23
  • 打赏
  • 举报
回复
不明白你要做什么事情

111,093

社区成员

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

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

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