C#连接MYSQL问题,帮忙看一下。

tiandulan 2012-05-24 07:40:50
MySQLConnection conn = null;

conn = new MySQLConnection(new MySQLConnectionString("localhost", "bishe", "root", "sa").AsString);

conn.Open();

string h11 = "select * from table1 where id = '"+uid+"'";
MySQLCommand commn = new MySQLCommand(h11, conn);

int count = (int)commn.ExecuteScalar();
if (count == 1)
{
MessageBox.Show("存在该账号!");
}
else
{
MessageBox.Show("不存在该账号。");
this.Close();
}
commn.ExecuteNonQuery();



出现问题:
int count = (int)commn.ExecuteScalar();未将对象引用设置到对象的实例。
如何解决啊。
...全文
145 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
蛤十一 2012-05-24
  • 打赏
  • 举报
回复
id为abc的数据是不是不止一条?你把这个sql语句放到数据库里执行以下,看一下结果
足球中国 2012-05-24
  • 打赏
  • 举报
回复
int count =commn.ExecuteScalar()==null?0: (int)commn.ExecuteScalar();
tiandulan 2012-05-24
  • 打赏
  • 举报
回复
可以运行了,但是匹配的结果不对啊…数据库里有id:abc,登录时id输入abc,但是结果显示的不存在该用户…什么情况?[Quote=引用 9 楼 pig357 的回复:]
int count = int.Parse(commn.ExecuteScalar().Tostring());
[/Quote]
pig357 2012-05-24
  • 打赏
  • 举报
回复
int count = int.Parse(commn.ExecuteScalar().Tostring());
tiandulan 2012-05-24
  • 打赏
  • 举报
回复
id就是主键[Quote=引用 5 楼 ak_47_a 的回复:]
数据库里要设置主键,你看看你数据库里的查询的该表有没有设置主键。
[/Quote]
tiandulan 2012-05-24
  • 打赏
  • 举报
回复
按你说的修改后,这个问题解决了但又出现新的问题了…
还是这一行代码int count = (int)commn.ExecuteScalar();系统表示:指定的转换无效。…[Quote=引用 3 楼 wtnu200 的回复:]
string h11 = "select count(*) from table1 where id = '"+uid+"'";
[/Quote]
tiandulan 2012-05-24
  • 打赏
  • 举报
回复
id是表的一列[Quote=引用 1 楼 的回复:]
id是关键字吗?
[/Quote]
亡魂葬爱 2012-05-24
  • 打赏
  • 举报
回复
数据库里要设置主键,你看看你数据库里的查询的该表有没有设置主键。
wtnu200 2012-05-24
  • 打赏
  • 举报
回复
不存在时,没有存在的行,所以你是取不到数据的,也没所谓转化为int,
wtnu200 2012-05-24
  • 打赏
  • 举报
回复
string h11 = "select count(*) from table1 where id = '"+uid+"'";
zhujiawei7 2012-05-24
  • 打赏
  • 举报
回复
可能是commn = null造成的,检查你的commn是否成功实例化
EnForGrass 2012-05-24
  • 打赏
  • 举报
回复
id是关键字吗?

110,530

社区成员

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

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

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